RE: Basic Xcode C Lang question...
RE: Basic Xcode C Lang question...
- Subject: RE: Basic Xcode C Lang question...
- From: "Bill Lowrey" <email@hidden>
- Date: Sun, 11 Jul 2004 14:17:34 -0500
- Organization: istation.com
Looks to me like the call to
scanf("%lf",&value);
isn't reading the newline, so when the call to
scanf("%c", &answer);
is made, answer contains the newline (i.e. answer == 10).
This is one of the many reasons I hate scanf(). Relying on the user to type
formatted input is, to say the least, iffy.
I realize you didn't have anything to do with this code, but it's a good
thing to remember!
FWIW, I'd fix this by using an alternate means of parsing the input, such as
fgets( stdin, ... );
Hope this helps you out!
- Bill
-----Original Message-----
From: email@hidden
[mailto:email@hidden] On Behalf Of Isaac Rivera
Sent: Sunday, July 11, 2004 1:42 PM
To: email@hidden
Subject: Basic Xcode C Lang question...
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.
_______________________________________________
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.