• 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
Re: Basic Xcode C Lang question...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Basic Xcode C Lang question...


  • Subject: Re: Basic Xcode C Lang question...
  • From: Stephan Burlot <email@hidden>
  • Date: Sun, 11 Jul 2004 21:36:08 +0200

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.


  • Follow-Ups:
    • Re: Basic Xcode C Lang question...
      • From: Isaac Rivera <email@hidden>
References: 
 >Re: Setting up Subversion.. (From: Lance Drake <email@hidden>)
 >Basic Xcode C Lang question... (From: Isaac Rivera <email@hidden>)

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