>>Why does it jump back to the turbo c window when I press enter to enter my age? Is this wrong?
because you didn't tell the program to do anything else. If you want it to wait so that you can see what's on the screen, put a getch() at the end so that it will wait for keybord input.
After calling scanf() with an integer, the scanf() function does not remove the key from the keyboard buffer. You must flush the input buffer or else the program won't wait for the next getch() .
int main()
{
float years, days;
printf ("Please type your age in years:");
scanf ( "%f", &years);
getchar(); // remove '\n' from keyboard buffer
days = years * 365;
printf ("You are%.1f days old.\n", days);
getchar();
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Better than using scanf which is a pretty complicated function which leaves the input stream dirty try out the alternative functions fgets along with atoi to take the input from user. For the function prototypes look here: http://www.cplusplus.com
And why are you using the age old Turbo Compiler? If you can, try to install a better IDE as well as a compiler.
The list of all the new compilers and free IDE can be found in the sticky at the top of the C++ forum in "Starting C".
Hope it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Well here is the general techinique used to avoid the pitfalls of scanf.
Take a look at this code iif you are really interested in learing something new.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char buffer [BUFSIZ] ;
float years, days;
printf ("Please type your age in years: ");
fgets (buffer, BUFSIZ, stdin) ;
if ( buffer [strlen (buffer)] == '\n')
buffer [strlen(buffer)] = '\0' ;
years = atof (buffer) ;
days = years * 365;
printf ("\nYou are %.0f days old.\n", days);
getchar () ;
return 0 ;
}
Hope it helped,bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343