Re: Basic Xcode C Lang question...
Re: Basic Xcode C Lang question...
- Subject: Re: Basic Xcode C Lang question...
- From: Isaac Rivera <email@hidden>
- Date: Sun, 11 Jul 2004 15:49:49 -0400
Stephan...
I had just figured the space issue. It was hard to read on the book,
and its not explained anywhere so far that there should be one.
Thanks
That solves the problem.
I am not relying on scanf. I am just trying to learn C Lang. That's
what the book exercise is. I was just trying to understand what was
wrong, not what the best solution is.
Again Thanks.
Isaac
On Jul 11, 2004, at 3:36 PM, Stephan Burlot wrote:
Isaac,
Add
fpurge( stdin );
after
scanf("%lf", &value);
But I second the other answers which says you should avoid relying on
getc.
BTW, googling on your first comment in the code (Program 4.6 -- The
almost indefinite loop - computing an average:), gives that you've
incorrectly typed your code:
the line
scanf("%c", &answer);
should be
scanf(" %c", &answer);
Note the space before "%c".
Stephan
Le 11 juil. 04, ` 20:42, Isaac Rivera a icrit :
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.