Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 01-28-2010, 09:35 AM   #1 (permalink)
brightmatter
Recruit
 
Join Date: Nov 2009
Location: Idaho
Posts: 9
brightmatter is on a distinguished road
printf only displays after 'main' has resolved?

I am using the most up-to-date eclipse IDE for C/C++. I have the below code. It seems simple enough. My problem is that no printf will display on the console until the main has finished (return 0). This means I get a prompt in console asking for input before I get the message telling me what to do. I fill in a number like 10 and I see 10 is now displayed in stdout followed by the first and second printf. Can anyone explain why this is? What am I doing wrong?
Code:
#include <stdio.h>

int main(void) {
	int n = 0; /* The number of fibonacci numbers we will print */
	int i = 0; /* The index of fibonacci number to be printed next */
	int left = 1; /* The value of the (i)th fibonacci number */
	int right = 1; /* The value of the (i+1)th fibonacci number */
	int temp = 1; /* The value of the (i+2)th fibonacci number */

                printf("Enter New Index: ");
		scanf("%d", &n);
		printf("Index: %d", n);
		if (n < 0 || n == 0) {
			printf("\nFibonacci index: %d\n", -1);
		}
		if (n == 1) {
			printf("\nFibonacci index: %d\n", 1);
		}
		for (i = 0; i < (n - 1); i++) {
			temp = right;
			right = left + temp;
			left = temp;
		}
		printf("\nFibonacci index: %d\n", left);

	return 0;
}
brightmatter is offline   Reply With Quote
Old 01-28-2010, 02:27 PM   #2 (permalink)
redhead
Super Moderator
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,876
redhead is on a distinguished road
You need to flush stdout ie:
Code:
...
  printf("Enter New Index: ");
  fflush(stdout);
...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
When echoing a variable it displays literally CWitt PHP 2 03-26-2009 01:42 PM
printf or equivilent text formatting. Rotkiv Java 1 10-25-2008 02:10 AM
OPOS : Displays Incorrect Alignment Happyarimaan Platform/API C++ 1 07-31-2008 03:09 AM
main page CaN Opener Feedback 2 07-09-2003 05:30 PM
PC Hardware: Displays bdl Lounge 9 05-09-2003 07:57 PM


All times are GMT -8. The time now is 03:59 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting