| | |
Turbo C help
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2006
Posts: 327
Reputation:
Solved Threads: 22
Hi, I'm a beginner and just started Turbo C
I wrote this program in the editor
main()
{
float years, days;
printf ("Please type your age in years:");
scanf ( "%f", &years);
days = years * 365;
printf ("You are%.1f days old.\n", days);
}
I then run the program and it asks me my age in years. I input my age and when I press enter to tell me my age in days it goes from the DOS window back to the turbo C editor, i then press Alt+ F5 to go back to the DOS screen and it then tells me my age in days
So the program is working but my question is-
Why does it jump back to the turbo c window when I press enter to enter my age? Is this wrong?
Thanks.
I wrote this program in the editor
main()
{
float years, days;
printf ("Please type your age in years:");
scanf ( "%f", &years);
days = years * 365;
printf ("You are%.1f days old.\n", days);
}
I then run the program and it asks me my age in years. I input my age and when I press enter to tell me my age in days it goes from the DOS window back to the turbo C editor, i then press Alt+ F5 to go back to the DOS screen and it then tells me my age in days
So the program is working but my question is-
Why does it jump back to the turbo c window when I press enter to enter my age? Is this wrong?
Thanks.
>>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 <Enter> key from the keyboard buffer. You must flush the input buffer or else the program won't wait for the next getch() .
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 <Enter> 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; }
Last edited by Ancient Dragon; Sep 21st, 2006 at 8:43 pm.
Better than using
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.
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.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
Join Date: Sep 2006
Posts: 327
Reputation:
Solved Threads: 22
Thanks, I'm using turbo C because that's what we are going to use in college.
I asked my lecturer today and he told me because the getche() function was missing like you said which I had forgotten about so I rewrote it like below
and it worked the way I wanted it to.
main()
{
float years, days;
printf ("Please type your age in years:");
scanf ( "%f", &years);
days = years * 365;
printf ("You are%.1f days old.\n", days);
getche();
}
I'm not fully sure what those extras that you added are for, haven't came across them yet so I'm not going to worry too much for now.. .
Thanks for all your help.
I asked my lecturer today and he told me because the getche() function was missing like you said which I had forgotten about so I rewrote it like below
and it worked the way I wanted it to.
main()
{
float years, days;
printf ("Please type your age in years:");
scanf ( "%f", &years);
days = years * 365;
printf ("You are%.1f days old.\n", days);
getche();
}
I'm not fully sure what those extras that you added are for, haven't came across them yet so I'm not going to worry too much for now.. .
Thanks for all your help.
Last edited by Colin Mac; Sep 22nd, 2006 at 2:30 pm.
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.
Hope it helped,bye.
Take a look at this code iif you are really interested in learing something new.
C Syntax (Toggle Plain Text)
#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.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
![]() |
Similar Threads
- Program a Calculator Using Turbo C (C)
- Numbers in Turbo C++ (C++)
- Image read and write operations in turbo C (C)
- My Turbo Archive...will it help? (Search Engine Optimization)
- What are turbo c function? (Computer Science)
- turbo c++ errors (C++)
Other Threads in the C Forum
- Previous Thread: segmented error??
- Next Thread: what is the use of "pOldPen" here?
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory drawing dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab windows.h






