• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Basic Xcode C Lang question...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Basic Xcode C Lang question...


  • Subject: Basic Xcode C Lang question...
  • From: Isaac Rivera <email@hidden>
  • Date: Sun, 11 Jul 2004 14:42:18 -0400

Hello Xcoders...

I have run into a small issue while trying to learn C Language using
Xcode 1.2, perhaps one of you might know the answer...

I have typed the exercise code below, which compiles fine, but when I
run it it does not behave as expected. First I am prompted to "Enter a
value:". So far so good. I do and press enter and now is when things go
a bit funny.

Instead of getting the prompt "Do you want to enter another value? (Y
or N): ", I get "Do you want to enter another value? (Y or N): "
immediately followed by a line break and "Enter a value: ". From this
point on all prompts are identical until I type "N" at which point
program gives the correct output of "The average is "... and exits.

The result is the same whether I compile in Xcode or use cc or gcc in
the terminal. So you know, I do the later by typing "gcc -o prog46
prog46.c" and then "./prog46" at the prompt.

Is this a GCC 3.5 issue?

What am I doing wrong?

Isaac Rivera

//  Program 4.6 -- The almost indefinite loop - computing an average:
#include <stdio.h>
#include <ctype.h>

int main () {

	char answer = 'N';		//  records Y or N to continue the loop
	double total = 0.0;		//  total of values entered
	double value = 0.0;		//  value entered
	int count = 0;			//  number of values entered

	printf("\n\n** This program calculates the average of any number of
values **\n");

	//  indefinite loop
	for(;;)
	{
		printf("\nEnter a value: ");
		scanf("%lf", &value);
		total += value;
		++count;

		printf("Do you want to enter another value? (Y or N): ");
		scanf("%c", &answer);

		if(tolower(answer) == 'n')
			break;
	}

	//  output the average:
	printf("\nThe average is %.2lf\n\n", total/count);

     return 0;
}
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: Basic Xcode C Lang question...
      • From: Stephan Burlot <email@hidden>
    • RE: Basic Xcode C Lang question...
      • From: "Bill Lowrey" <email@hidden>
References: 
 >Re: Setting up Subversion.. (From: Lance Drake <email@hidden>)

  • Prev by Date: Re: Setting up Subversion..
  • Next by Date: Re: Basic Xcode C Lang question...
  • Previous by thread: Re: Setting up Subversion..
  • Next by thread: RE: Basic Xcode C Lang question...
  • Index(es):
    • Date
    • Thread