0

my code here simply won't allow the user to type their input with the std::cin if the user types in a letter the first time and will be put into an infinite loop on the while loop due to the int remaining as wrong. Any help is appreciated to finding out how to make it allow the user to type in their input with the second std::cin is appreciated.

if (Current == 1) {
    std::cout << "Text" << "\n\n";
    std::cin >> Current;
    while (Current != 2 && Current != 3) {
    try {
    if (Current == 2 || Current == 3) {
        std::cout << "Correct" << "\n\n";
        }
        else {
             throw 505;
        }
    }
    Catch (...) {
         std::cout << "Wrong" << "\n\n";
    }
    std::cin >> Current;
    }

For reference Current by default is 1.

  • Did you by accident fetch any input before? If that one was illegal the stream state is broken – a thing you never check! – Aconcagua Oct 25 '22 at 11:03
  • 2
    Please have a look at [mre] – your example isn't as lacking e.g. the definition of `current`. `Catch` with capital letter is not a C++ keyword! – Aconcagua Oct 25 '22 at 11:04
  • 4
    `if (Current = 1)`? Do you perhaps meant `if (Current == 1)`? – Some programmer dude Oct 25 '22 at 11:04
  • 1
    `cin` remebers the error and doesn't read anything more until the condition is cleared. [How to handle wrong data type input](https://stackoverflow.com/questions/10349857/how-to-handle-wrong-data-type-input) – BoP Oct 25 '22 at 11:04
  • sorry at the start with if (Current = 1) is meant to be if (Current == 1) – Reiss Hamilton Oct 25 '22 at 11:10
  • I recommend you read a whole line into a string (`std::string`, not a `char` array). Then attempt to convert the input if needed. Put into a separate function. Then do range-validation separately (preferably in its own function as well). – Some programmer dude Oct 25 '22 at 11:36

1 Answers1

0

Thank you for the help I have been able to fix the error by using std::cin.ignore(1000, '\n'); This is what the new code looks like

if (Current == 1) {
        std::cout << "Would you like to go to the Blistering Fog - 2 or the Daring Desert - 3" << "\n" << "or use your rocket boots - 10" << "\n\n";
        std::cin >> Current;
        while (Current != 2 && Current != 3 && Current != 9 && Current != 10) {
            try {
                if (Current == 1) {
                    std::cin >> Current;
                }
                else {
                    throw 505;
                }
            }
            catch (...) {
                std::cout << "Location Invalid, Would you like to go to the Blistering Fog - 2 or the Daring Desert - 3" << "\n" << "or use your rocket boots - 10" << "\n\n";
                std::cin.clear();
                std::cin.ignore(1000, '\n');
                Current = 1;
            }
        }

I just wish I didn't need to use the std::cin.ignore as it seems to have its issues.

  • Are you just playing around with exceptions? Because they aren't needed here at all. In contrast you do not check the stream state! I'm not clear about your intention for setting current to 1 – what, if the user enters that? I think more appropriate would be a loop like: `for(;;) { if(std::cin >> current) { /* evaluate current, exit the loop on valid input */ } else { /* invalid input, clear stream as you did within catch */ } }` – Aconcagua Oct 25 '22 at 14:20
  • Evaluation might look like: `switch(current) { case 2: /* ... */ break; /* other cases */ default: continue; /* the loop! */ } break; /* the loop; on invalid input the continue in the default clause prevents getting here! */`. – Aconcagua Oct 25 '22 at 14:23
  • `std::cin.ignore(std::numeric_limits::max(), '\n')` would actually be the correct way to flush the input stream, though admitted, unlikely that there are indeed more than 1000 characters in… – Aconcagua Oct 25 '22 at 14:29