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;
}
Now 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.