Basic Xcode C Lang question...
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.