1
votes

I'm learning C++, so I don't fully understand what's going on with my code here, but from what I've been able to glean, it seems like it could be some kind of buffer issue.

#include <stdio.h>
#include <vector>
#include <iostream>
#include <typeinfo>

using namespace std;

bool stopRun = true;
int height, average, total, count;
vector <int> heights;

int main ()
{
    while (stopRun)
    {
        cout << "Enter a height, or 'end' to quit: ";
        cin >> height;
        if (typeid(height).name() == "i")
        {
            heights.push_back(height);
            cout << heights[0];
            count++;
        }
        else if (typeid(height).name() == "i")
        {
            cout << "\nPlease enter an integer: ";
            continue;
        }
        if (count == 5)
        {
            stopRun = false;
        }
    }
    for (int i = 0; i < heights.size(); i++)
    {
        total += heights[i];
        cout << "\nTotal: " << total;
    }
    return 0;
}

For some reason, this code will continuously output: "Enter a height, or 'end' to quit: ". In an earlier version, it would output: "Enter a height, or 'end' to quit: Please enter an integer: ".

What I think is going on is that my "cin >> height;" line is pulling in the output from "Please enter an integer: " and treating it as my input, which identifies it as not being of type integer, which starts the infinite loop.

How do I clear the input buffer so that it doesn't bring in cout statements? Or is that even the issue I'm experiencing here?

Thanks in advance!

4
cin >> height; If you enter something that is not an integer, cinwill be in an error state. You need to clear the error and consume the bad input. Why would we call cin.clear() and cin.ignore() after reading input? - Johnny Mopp
Another option is to read the input as a std::string and convert to int with std::stoi() - Johnny Mopp
What's the purpose of the typeid's? They do not tell you anything about whether cin successfully read an integer. Besides, isn't the condition in the if and else if -branches the same? - Timo

4 Answers

0
votes

I suggest to catch the string. If string is not "end" then convert to number inside try/catch

0
votes

you can use this function at the start of your program fflush(stdin). It will clear your input buffer.

0
votes

You are attempting to read an int and a string in the same line of code. I suggest you use getline() to read the input and try to convert the string to int.

std::string input;
while (heights.size() != 5) {
    cout << "Enter a height, or 'end' to quit: ";
    if (std::getline(cin, input)) {
        if (input == "end") break;
        try {
            heights.push_back(std::stoi(input));
        }
        catch (std::invalid_argument e) {
            cout << "\nPlease enter an integer: ";
        }
    }
}
0
votes
if (string(typeid(height).name()) == "i")

What you had wrong was the comparison of a pointer and string. Since typeid(height).name() returns a pointer to a c-string with the name for the object.