 |
 |
What's Wrong with this C Program?
|
 |
|
 |
|
Senior User
Join Date: Mar 2001
Location: Toronto, Ontario, Canada
Status:
Offline
|
|
Hi All,
Still working on Dave Mark's Programming C For the Macintosh book. I typed in the program exactly as it looked in the book, but unlike the first program, this program will not run, could someone please tell me what's wrong with it?
int main ( void )
{
int index, num, sum;
sum =0;
for ( index=1; index<=5; index++ )
{
printf ( "Enter number %d --->", index );
scanf ( "%d", %num );
sum=sum+num;
}
printf ( "The sum of these numbers is %d.",sum );
return 0;
}
|
|
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Apr 2001
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Nov 2001
Location: London, UK
Status:
Offline
|
|
It's a really long time since I did any C programming but first guess is that your second argument to scanf is wrong. Try replacing it with &num (that's ampersand followed by num). scanf takes a pointer to constant char. Hopefully when you tried to compile it your compiler spat out an error message relating to this line.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2001
Status:
Offline
|
|
This works for me in Project Builder as a Standard Tool. I made two changes - I added a fflush (stdout); to make the printf line show up, and the second argument to scanf should be &num, not %num.
#include <stdio.h>
int main ( void ) {
int index, num, sum;
sum = 0;
for ( index=1; index<=5; index++ ) {
printf ( "Enter number %d ---> ", index );
fflush (stdout);
scanf ( "%d", &num );
sum=sum+num;
}
printf ( "The sum of these numbers is %d.",sum );
return 0;
}
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Sep 2000
Location: The Rock
Status:
Offline
|
|
Yup, you need an anpersand (sp?) to scan, not a percent. I'm not sure what the fflush adds, though. It should work without it, I think.
greg
|
|
Mankind's only chance is to harness the power of stupid.
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: May 2001
Status:
Offline
|
|
Yeah, there is a bug in OSX that doesn't flush the cout/fprint buffer like one would expect. Put int an endln and it will show up.
|
|
If your computer stops responding for a long time, turn it off and then back on. - Microsoft
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Mar 2001
Location: Toronto, Ontario, Canada
Status:
Offline
|
|
Thanks for the tips y'all - yup, turns out that I had a typo there which caused the compile error. Turns out I had "%num" instead of "&num". Still getting the hang of learning C but geez, I better start typing these programs right!! lol
BTW, the program ran fine without the "fflush (stdout);".
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Mar 2001
Status:
Offline
|
|
I'm not aware of any bug in OS X regarding flushing stdout for C programs.
There are three cases when a C program is required to flush stdout that I know of:
1) A newline character \n is output to it
2) fflush(stdout) is called
3) The program terminates
So yes, the fflush(stdout) should be there; otherwise, the program is not strictly portable and may not print th line "Enter number %d --->" until after it finishes.
-Peter
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|