473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Program closes immediately even with getchar()

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.

4 2126
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
Thank you! The start without debugging option completely solved it!!
Jan 8 '17 #5

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

Similar topics

3
by: Chris LaJoie | last post by:
We (My team and I) are currently developing an application that works fine on 3 of our dev machines, but our 4th is having a problem. It's an old P3 733Mhz with 192MB RAM and running XP Pro. It...
0
by: Kurt | last post by:
Hi, I have successfully installed (no errors) a copy of VS.NET 2003 on my machine but as soon as I try and open Visual Studio it opens and then shuts immediately. Anyone know what this problem...
0
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
5
by: Phil Galey | last post by:
VB.NET 2002 on Windows 2000 SP 3 When I start my program, the NotifyIcon appears in the tray, as it should. However, when I close the program, the NotifyIcon remains until I hover over it with...
1
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks...
4
by: ghostrider | last post by:
I ned help in figuring out to make my program display even numbers only. My for loop counts every number, but I only need to display the even ones. Can someone help me with this?
1
by: Rebeljunk | last post by:
k, so here's the problem: this is the code i've written: #include <iostream> using namespace std; int main() { int length, width;
0
by: bmeahan | last post by:
I have a c# web app on .net 1.1 framework. Currently I have a process where I can upload files (.doc, .xls, etc) to a my sql database. When trying to open the files however I'm running into an...
1
by: zartanian | last post by:
Hello and thank you in advance for looking at my post. I work for a small computer repair company and we upload files to customers machines when we remote into them. Here is what I am looking...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.