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:39:42 -0500
- Organization: istation.com
No, it's not an Xcode issue. It behaves the same way using Microsoft Visual
C++ 2003.
I can't comment on the Ivor Horton book, since I haven't looked at a
beginning C book in 15 years ;-)
Part of your comment is correct. Scanf does halt the for loop while waiting
for input.
I can tell you that the code as written won't work.
For that matter, if I take some time, knowing that Horton writes for Apress
now that Wrox has been purchased by them, I can download the code for the
book, and looking at the code from the website:
/* Program 4.6 The almost indefinite loop - computing an average */
#include <stdio.h>
#include <ctype.h> /* For tolower() function */
void main()
{
char answer = 'N'; /* Records yes or no 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("\nThis program calculates the average of"
" any number of values.");
for( ;; ) /* Indefinite loop */
{
printf ("\nEnter a value: "); /* Prompt for the next value */
scanf(" %lf", &value); /* Read the next value */
total += value; /* Add value to total */
++count; /* Increment count of values */
/* check for more input */
printf("Do you want to enter another value? (Y or N): ");
scanf(" %c", &answer ); /* Read response Y or N */
if( tolower(answer) == 'n' ) /* look for any sign of no */
break; /* Exit from the loop */
}
/* output the average to 2 decimal places */
printf ("\nThe average is %.2lf\n", total/count );
}
... I can see a few differences.
If you look at the calls to scanf that Horton uses, you'll see he put a
space in front of the format codes in the string. This will cause the scanf
routine to grab whatever dross is there. It makes it work, in any event. You
left out the spaces.
I'd avoid scanf if I were you. I got that advice in college eons ago, and
it's still good. If you can guarantee a format for the input stream (i.e. if
you're parsing stdout from a program, not stdin from a user...) it might be
acceptable and useful, but you'd still be better off using other methods.
Anyway -
Hope you have fun learning C. His Java book was pretty decent.
Bill
-----Original Message-----
From: email@hidden
[mailto:email@hidden] On Behalf Of Isaac Rivera
Sent: Sunday, July 11, 2004 2:13 PM
To: Creed Erickson
Cc: email@hidden
Subject: Re: Basic Xcode C Lang question...
On Jul 11, 2004, at 3:03 PM, Creed Erickson wrote:
> You're trusting scanf to do what you think it should.
Should it?
>
>> printf("Do you want to enter another value? (Y or N): ");
>> scanf("%c", &answer);
>
> This fragment is picking up the newline character placed in the input
> buffer when entering the value.
It doesnt in the previous scanf in the program, why would it here?
This code is exactly reproduced from the Beginning C, 3rd edition by
Ivor Horton book. I know, I know, books have errata. But several of the
examples on chapter 4 revolve around this logic (scanf halting the
execution of a fot loop), Knowing that Mr. Horton knows well how C
should behave, specially a basic io function like scanf, I wonder if
this is a mac implementation issue or what?
Isaac
_______________________________________________
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.