I began learning how to code this month and attended a c++ camp for a week, so I am familiar with all of the basics. I found these exercises, which I found very helpful. Could one look over my grading program because whenever I start debugging the program, it asks me to imput my grade, but after I type 80 for example and press enter, the program dissapears?
I used a bunch of "if statements" and depending on the value of "int grade" the "char letter_grade" changes.
You have two options, you can either run your code from the command line (Command Prompt for Windows) or you can add a way to "pause" the code at the end so it doesn't just flash away. Most people opt for the later simply because people hate the command line.
Simple add cin.ignore(); and the code will wait for you to hit a button. If one doesn't work, two might. If you want to read up on why this happens, here is a very long thread that explains all of the different options: http://www.cplusplus.com/forum/beginner/1988/ Please don't use system()
Thanks for the reply! How do you run the code from the command line (Command prompt window)? Adding the cin.ignore(); didn't work. Are you supposed to add that line anywhere or after each "if statement".
1)open program from project folder\debug\programname
2)use system("pause") if you are beginner and on windows to hold screen
3)
1 2 3 4
std::cin.ignore(); //conditional if you are inputting anything before this anywhere
//you can use flush function too and is better one
std::cout<<"Press any key to End program.....";
std::cin.get();
4)using conio.h function getch();
5)Best one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Close
{
public:
~Close()
{
std::cout<<std::endl<<"Press any key to end program";
std::cin.get(); //can use some more methods here
}
};
int main()
{
Close C;
//code here
return 0;
}