-3

I have looked at similar questions for C++ programs but, even when I apply the fixes, my command window is closing after I input my value ("Enter a grade"). Using Visual Studio 2017.

 #include <iostream>

int main()
{
    int grade =0;
    std::cout << "Enter a grade (1-100): ";
    std::cin >> grade;

    if (grade >= 70)
        std::cout << "\nPass\n";
    else
        std::cout << "\nFail\n";
    std::cin.get();
    return 0;
}

Could this error be causing it?

Loaded 'C:\Program Files\Norton 360\NortonData\22.9.1.12\Definitions\BASHDefs\20170616.003\UMEngx86.dll'. Cannot find or open the PDB file.

I resolved all my other pdb file errors but don't know how to get rid of this one and also don't know if it's what's causing the issue. I think these errors are new since I installed Visual Studio 2015 to get rid of a debugging error; I was previously running this program just fine.

britt
  • 1
  • 1
  • 1
    ***Cannot find or open the PDB file.*** This is normal / nothing to be concerned about. The message is telling you that you don't have debug symbols for your antivirus. You won't need that unless you want to debug your AV. – drescherjm Jun 20 '17 at 00:50

2 Answers2

1

std::cin.get() >> grade; reads one character from cin and shifts it right by the value stored in grade, which is 0, then throws away the result. Change that to std::cin >> grade; and it will work much better.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0

There are a few options. Usually when you have a console program, you run it from an already running console, so you don't need to pause the program. The program just prints its output and exits, while the console you were already running stays put.

To keep your console window open here, you can use this function to pause the program until the user presses enter. I wouldn't use it too often though, it's not a good programming habit to invoke system calls liberally like this. Why "system" is evil

system("pause");

Another thing you could do is write a loop that prompts the user to enter an option to exit, like so.

char input;

do {
    std::cout << "Enter 'q' to exit" << std::endl;
    std::cin >> input;
} while (input != 'q');

This way the only way for your program to exit will be to enter the right input.


This example is pretty simplistic, but it works. When you need to validate user input, like verifying the user entered an integer, etc., I create a buffer, write the user input to the buffer, compare the input to the buffer, and if the two are equivalent, accept the input. If you wanted to make this example a little stronger, you could make sure the user entered only a char value like so:

do {
    std::cout << "Enter 'q' to exit" << std::endl;
    std::cin >> input;

    if (std::cin.fail()) {
        std::cin.clear(); // Clear the 'input fail' flag
        std::cin.ignore(50, '\n');
    }
} while (input != 'q');

This is a more robust way of ensuring fault tolerance in your program. You can check out this question for more details. cin clear, ignore