943,723 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7947
  • C RSS
Sep 21st, 2006
1

Turbo C help

Expand Post »
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.
Similar Threads
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
Sep 21st, 2006
0

Re: Turbo C help

>>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() .
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 22nd, 2006
1

Re: Turbo C help

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 22nd, 2006
1

Re: Turbo C help

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.
Last edited by Colin Mac; Sep 22nd, 2006 at 2:30 pm.
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
Sep 22nd, 2006
0

Re: Turbo C help

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.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main (void)
  6. {
  7. char buffer [BUFSIZ] ;
  8. float years, days;
  9. printf ("Please type your age in years: ");
  10. fgets (buffer, BUFSIZ, stdin) ;
  11. if ( buffer [strlen (buffer)] == '\n')
  12. buffer [strlen(buffer)] = '\0' ;
  13. years = atof (buffer) ;
  14. days = years * 365;
  15. printf ("\nYou are %.0f days old.\n", days);
  16. getchar () ;
  17.  
  18. return 0 ;
  19. }

Hope it helped,bye.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 11th, 2010
0
Re: Turbo C help
i am trying to install turboc++ software in our windows7 (64bit) but i'm unable to install and have some problem so please i kindly request to you please help me via <<Snipped>>
Last edited by adatapost; Dec 12th, 2010 at 12:49 am. Reason: Snipped. Do not ask anyone (member or moderator) for help by email or PM or Contact No.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
upendra cse is offline Offline
1 posts
since Dec 2010
Dec 12th, 2010
0
Re: Turbo C help
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Dec 12th, 2010
0
Re: Turbo C help
@Colin mac .
They are different functions used to get user inputs similar to scanf and getche() which your sir suggested..

U can google to know more about functions like getchar() getch() fgets() ..
Reputation Points: 48
Solved Threads: 16
Junior Poster
Shankye is offline Offline
168 posts
since Nov 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: GCC compile output
Next Thread in C Forum Timeline: simpson rule





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC