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 > Problems with C/C++ in Project builder

Problems with C/C++ in Project builder
Thread Tools
Junior Member
Join Date: Sep 2002
Location: Evanston, Illinois
Status: Offline
Reply With Quote
Oct 7, 2003, 09:18 PM
 
I am having problems with Project builder.


NOTE:
I am only doing work with the C++ tool, and just began programming

When it hit Build and Run

The screen does not display my printf.

After I enter two number it displays it but not before. Why does it do that?

here is the code


#include <stdio.h>

int main (void)
{
/* Variables */

int num1, num2, sumnum;


/* Statements */

printf("Please input two whole numbers: \n");

scanf("%d %d" , &num1, & num2);

sumnum = num1 + num2;

printf("The sum is %d", sumnum);

return 0;
}


Here is the result: The two scan int had to be entered before the program would run the first printf. WHY? I want the program to printf that i must enter the numbers before I have to.


quote:
1
2
Please input two whole numbers:
The sum is 3
Test has exited with status 0.
That's just my opinion. What do I know, I'm just a college student.
     
Senior User
Join Date: Sep 2000
Location: UK
Status: Offline
Reply With Quote
Oct 8, 2003, 05:43 AM
 
Maybe you should use the cout<< function. This works for me. I'm also a newbie, and so if someone could say whether printf works in C++, and if so if the implementation is different, I'd be really pleased.

Thanks,

David
     
Dedicated MacNNer
Join Date: Sep 2001
Location: San Jose CA
Status: Offline
Reply With Quote
Oct 8, 2003, 02:36 PM
 
Hi,

Try replacing:

printf("Please input two whole numbers: \n");

with:

fprintf(stdout,"Please input two whole numbers: \n");
fflush(stdout);

This should solve your problem. By default output (STDOUT) is buffered and won't be displayed until the buffer is full. If you want output to be displayed immediately you need to flush the buffer manually.

--ranga
     
Junior Member
Join Date: Sep 2002
Location: Evanston, Illinois
Status: Offline
Reply With Quote
Oct 8, 2003, 04:06 PM
 
Thanks ranga

works great now
That's just my opinion. What do I know, I'm just a college student.
     
   
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 07:14 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