By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Program closes immediately even with getchar()

P: 7
EDIT: turns out it was not even the code... It was just that when I compiled the program I was actually debugging... I just needed to find the "run without debugging option"!!

I am using Microsoft Visual Studio and whenever I use scanf or getchar the program always closes immediately, and i do not get to see the result of the program. I usually have to use two getchar() at the end and its really annoying. I was wondering if there was a way to get around that. The specific program is below and even with three getchar()s it closes immediately... Also the answer is not correct I am not sure why. The program is supposed to ignore the text and equal sign and just print the number back, but when the program displays the result for a second before it closes, i can tell it is not the number I entered. If you could help with that too, that would be amazing. I am using C not C++ by the way.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int num;
  6.  
  7.     printf("Type some text, followed by an equal sign and an integer.\n");
  8.     while (getchar() != '=')
  9.         scanf("%d", &num);
  10.     printf("The number entered was %d.", num);
  11.  
  12.     getchar();
  13.     getchar();
  14.     getchar();
  15.     return 0;
  16. }
  17.  
Jan 4 '17 #1

✓ answered by weaknessforcats

The scanf return value is not tested. scanf returns the number of items successfully retrieved. In this case scanf should return 1. If not, scanf aborts and you have a problem. The data is left in the input buffer for the getchar() to trip over.

Plus the input buffer may have been overrun. Check for ferror. Or it may have reached the end of data. Check feof.



My suggestion is to enter exactly what the program expects and not worry about parsing input data. This is always onerous and should be left to do last, if at all.

In this case, I would hard-code the input data in main() at get the program working with that. After it works try various hard-coded strings and verify the program still works.

Finally, replace the hard-code with the scanf and re-do all your test scenarios to verify the code still works using scanf.

Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
The scanf return value is not tested. scanf returns the number of items successfully retrieved. In this case scanf should return 1. If not, scanf aborts and you have a problem. The data is left in the input buffer for the getchar() to trip over.

Plus the input buffer may have been overrun. Check for ferror. Or it may have reached the end of data. Check feof.



My suggestion is to enter exactly what the program expects and not worry about parsing input data. This is always onerous and should be left to do last, if at all.

In this case, I would hard-code the input data in main() at get the program working with that. After it works try various hard-coded strings and verify the program still works.

Finally, replace the hard-code with the scanf and re-do all your test scenarios to verify the code still works using scanf.
Jan 5 '17 #2

P: 7
Thank you for the explanation! That makes sense. Could you also explain in depth as to why I have to use getchar at the end of programs sometimes? I know it has to do with scanf being used, but I dont quite understand. I have a friend who is also trying to learn c programming, but she never has to use getchar at the end of her programs. Thank you again!
Jan 5 '17 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Your program just executes when you start it. Unless there is a pause of some sort at the end all you see is a black flash. That flash is your console screen. You have to stop execution somehow so you can read the screen. Some sort of keyboard action is convenient since the program must wait until you press enter.

Another trick is to just let the program run but write into a log file along the way. After execution is complete you can read the log file using a standard tool program.

Secret Visual Studio Tip:

Visual Studio has two kinds of builds: release and debug. Debug is the default. It has code to support the debugger so you don't want to ship this to a customer. You just use it for debugging.

If you start the program using "start without debugger" rather than just "start", Visual Studio will pause the program at the end with "press any key to continue...".

So you don't need that final getchar().

BUT this only works using Visual Studio.
Jan 6 '17 #4

P: 7
Thank you! The start without debugging option completely solved it!!
Jan 8 '17 #5

Post your reply

Sign in to post your reply or Sign up for a free account.