Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > What's Wrong with this C Program?

What's Wrong with this C Program?
Thread Tools
Senior User
Join Date: Mar 2001
Location: Toronto, Ontario, Canada
Status: Offline
Reply With Quote
Dec 6, 2001, 02:12 PM
 
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
Reply With Quote
Dec 6, 2001, 02:43 PM
 
what error do you get?
     
Fresh-Faced Recruit
Join Date: Nov 2001
Location: London, UK
Status: Offline
Reply With Quote
Dec 6, 2001, 02:46 PM
 
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
Reply With Quote
Dec 6, 2001, 03:04 PM
 
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
Reply With Quote
Dec 6, 2001, 03:51 PM
 
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
Reply With Quote
Dec 6, 2001, 05:35 PM
 
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
     
Kestral  (op)
Senior User
Join Date: Mar 2001
Location: Toronto, Ontario, Canada
Status: Offline
Reply With Quote
Dec 6, 2001, 08:57 PM
 
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
Reply With Quote
Dec 7, 2001, 02:30 AM
 
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
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:59 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2