0

I'm making a program that uses a while loop in C++. Here is my code so far:

    int userInput = 0;
    while (userInput != 'Z')
    {
        cout << "Please enter the homework score: ";
        cin >> userInput;
        homeworkScores.push_back(userInput);
        if (userInput == 'Z') {
            userInput = 'Z';
        }
    }

The problem im having is whenever I type Z, the loop keeps printing "Please enter the homework score: " over and over without stopping. I've defined homeworkScores as a vector earlier in the code. How can I make it stop the loop once userInput == 'Z'? Thanks in advance!

liil
  • 37
  • 4
  • 4
    How about making `userInput` `char` instead of `int`? – nada Oct 15 '19 at 13:54
  • 1
    `cin >> userInput` reads an `int`, not a `char` as `'Z'` is. – Quentin Oct 15 '19 at 13:54
  • if i make it a char will it still be pushed back in the vector homeworkScores (which is a int vector)? – liil Oct 15 '19 at 13:57
  • You should catch up on how to convert a char to an int and the other way around. – nada Oct 15 '19 at 13:59
  • 3
    What on earth is supposed to be the point of `if (userInput == 'Z') { userInput = 'Z'; }`? – Jesper Juhl Oct 15 '19 at 14:00
  • I was trying to set it to the char Z so it wouldn't run again because of the while(userInput != 'Z') – liil Oct 15 '19 at 14:04
  • So, the normal operation is that the user enters integers which are pushed into homeworkScores, but when the user inputs a "Z" instead of an integer, the loop should exit, is that correct? – Johnny Johansson Oct 15 '19 at 14:12
  • ```while(cin >> userInput)``` would exit once you output anything other than an integer. 'Z', 'A', 'qwerty' and others. Would that suit your needs? – Dan Ganea Oct 15 '19 at 14:13

2 Answers2

0

A simple way to exit a loop is by using the break statement.

if (userInput == 'Z') {
            userInput = 'Z';
            break;
        }

Other ways would be to set your exit condition to resolve as false, which I think is causing some issues for you.

EDIT: As @Ganea Dan Andrei noted, reading a char from cin into an integer will cause the cin::fail() to return true. This can be reset by calling cin.clear(), which will allow you to make further inputs.

userInput is an integer, and so 'Z' would have to equal the ASCII equivalent of its char value, which is 90. The way you're doing it should shouldn't work. Instead, try making userInput a char, and then convert it to an integer so you can push it back into your vector. This might look like:

char userInput = '';
    while (userInput != 'Z')
    {
        cout << "Please enter the homework score: ";
        cin >> userInput;
        homeworkScores.push_back(userInput - '0'); //Are you sure you want to push a 'Z'?
        if (userInput == 'Z') {
            userInput = 'Z';
            break;
        }
        userInput = ''; // Reset your input so it doesn't keep getting pushed new values
    }

What happens here is userInput - '0' is subtracting the ASCII values of your chars, and leaving you with their integer value. There are other ways to do this, but this is a commonly used way.

alteredinstance
  • 587
  • 3
  • 17
  • Hey, while your solution works and (as you noted) is not a great idea, you should also notice that it has many (possible) bugs or unexpected behaviours at least: Input of "12" leads to 2 grade entries: 1 and 2, input of other characters is valid input: aBcZ results in 49 points, 28 points, 51 points and an end of the program. – Ketzu Oct 15 '19 at 14:20
  • His solution __shouldn't__ work, as reading a char into an int with std::cin would result in std::cin.fail() returning true, not actually convering the char variable. – Dan Ganea Oct 15 '19 at 14:22
  • 1
    Thanks, just added a line to clear the input after each entry. Some of the logic I kept because we just can't know what his exact use cases are, but yeah, I agree the logic here seems wonky. – alteredinstance Oct 15 '19 at 14:23
  • Even if you clear the input, this assumes all grades are between '0' and '9' which is often not the case. – Dan Ganea Oct 15 '19 at 14:25
  • @GaneaDanAndrei I thought so too at first, but they changed userInput to char (without me noticing at first), so it should work. – Ketzu Oct 15 '19 at 14:26
  • I'll add an explanation for the cin::fail(), thanks. As for the '0'-'9' inputs, I'm leaving it up to him to sanitize his inputs - that's not really within the scope of the question. – alteredinstance Oct 15 '19 at 14:27
  • 1
    @Ketzu I meant the original ```This should work, but is considered bad form. ``` comment. Which was incorrect. – Dan Ganea Oct 15 '19 at 14:27
0

The problem you are facing, is that cin is trying to read an integer, but you provide a character in the input. cin will only ask for another input, once the input is empty, you can try this by supplying more than one integer to your code:

Please enter the homework score: 2 27 12 8

will input all four numbers and print "Please enter the homework score: " 4 additional times.

If you provide it with a character, it will not remove it from the input, so whenever "cin" is reached, it will see "Z" in the input, and continue.

You can use answers like provided here How to catch invalid input in c++? for your input sanitation, but it will not make "Z" work as a stop.

The easiest way is to chose an invalid score like -1 instead of Z, end on any invalid input or try to read a string on failure to read an int.

Ketzu
  • 660
  • 1
  • 7
  • 12