Can't read program output
So I'm learning C++ today and I've come across a little problem that I can't seem to solve. When I write a program to input information from a user, the program closes right after the user inputs the information and does not display the inputed information.
So for example, here's the code:
#include <iostream>
#include <string>
using namespace std;
int main() {
//Declar vairables
char letter = 'A';
int integer = 0;
float dec = 0.0f;
cout << "Enter a letter: ";
cin >> letter;
cout << "Enter an integer: ";
cin >> integer;
cout << "Enter a float number: ";
cin >> dec;
cout << endl;
//Output what user entered
cout << "letter: " << letter << endl;
cout << "integer: " << integer << endl;
cout << "float number: " << dec << endl;
}
N开发者_如何学运维ow when I run this, the console asks me to:
- Input a letter
- Input an integer
- Input a float number
After this though, the program instantly closes without displaying the output that this segment of code is supposed to do:
//Output what user entered
cout << "letter: " << letter << endl;
cout << "integer: " << integer << endl;
cout << "float number: " << dec << endl; }
This has me scratching my head as there are no compiling errors, and the code is ripped straight out of C++ Programming for Games by eInstitute Publishing.
Your program is exiting. As the command line was opened via the execution of your process, it closes when your process exits. You can add a call to cin
at the end of the Main function in order to pause until enter is pressed, something like:
cout << "Press any key to close the program";
char c;
cin >> c;
This is what usually happens when you run your program in Debug mode. Your program executes the final couts and prints them then immediately jumps to the return 0; statement. You must ask for input as a final action to prevent the program from instantly closing.
To avoid this, you can add this at the end of your program:
char c;
std::cin>>c;
This will simulate a pause.
If you're on Windows, you can also call system("PAUSE"); although this is not cross-platform.
You're probably running it from your IDE directly, that's why it happens. At the end of your program have another input statement, this will force the program to pause and you'll see the screen.
I could pin point the problem better if the acual code was written in question but i think the problem is taht you are trying to compile with visual studio and you are tying to debug your project(you press f5) i guess if you press ctrl+f5 that problem would be solved,(it make your program to run without debugging and also make it wait for a key until program is finnished), the other way is to add another cin
at the end of your program so you can see what was written before the console closes.
Hm, it seems like this finally gets a little old-fashioned - on Windows, on Linux it's certainly still quite popular, and I would definitely recommend it: as long as your programs don't have any GUI, run them from a permanently open terminal window! This has several benefits, being able to read the program output after it has terminated is one of them.
Hmm, try inserting a
#include<stdlib.h>
in your directives and a
System.("Pause");
at the very end of your program. It closes right away simply because it either crashed, giving you that "send error" window, or it just simply finished and the program is done. Ed's right though, another cin
call would do the trick as well.
Thing is, after displaying your lines to the console, all the application does is exit. You way you can revert this is putting an extra line of command below the rest of your code, like:
int main() {
// ...your code
char buffer[100];
cin>>buffer;
}
Another, better way to do this, is using the system
API and calling pause (if you are running this on windows). Take a look here to know more about the API.
#include <stdio.h>
#include <stdlib.h>
// your includes, NO DUPLICATES!
int main() {
// ...your code
system("pause");
}
精彩评论