0

I can't understand why this while loop isn't stopping once the end condition is matched?

The idea is the user inputs a 5 digit integer and can type anything else to terminate the input process. But

bool noerror(true);
int n(0),part2temp;
vector<int> part2;

while(noerror){
    cout << "Please enter the 5 integer (x to stop)" << endl;
    cin >> part2temp;
    part2.push_back(part2temp);
    n++;

    if (cin.fail()||part2temp>99999||part2temp<10000){
        cout << "End condition matched" << endl;
        cin.clear();
        cin.ignore(10);
        noerror=(false);
    }
}
cout << "escaped loop" << part2[n] << endl;

I get the output to the screen from the IF part of the loop when I type in x for example but for some reason changing the bool value does not terminate the loop and the text "escaped loop" is never shown on screen.

Can anyone tell me what I'm doing wrong?

4
  • Several things: first, indexes in a vector go from [0...n), so part2[n] indexes one beyond the end. And second, you don't need to keep track of the number of elements in the vector, std::vector does this for you; use part2.size() to get the value. And third, supposing the vector non-empty, part2.back() is the simplest way of accessing the last element. Commented Feb 18, 2013 at 9:26
  • What end condition (you have three) is not meeting your expectations? All of them?? Commented Feb 18, 2013 at 9:27
  • Also, you should check for failure before accessing part2temp (i.e. before the push_back). (And you should probably define part2tmp in the loop, and not outside of it.) Commented Feb 18, 2013 at 9:28
  • You can set a breakpoint in a debugger, in the if block, and when hit you can step to see what happens, and also examine the values of all relevant variables. Commented Feb 18, 2013 at 9:29

3 Answers 3

3

There is undefined behaviour as the part2[n] is going one beyond the bounds of the vector, and may be the reason the output of the following line of code never appears:

cout << "escaped loop" << part2[n] << endl;

giving the impression the loop does not exit. If the loop executes just once then n == 1 and there will be one element only in the vector named part2, which means only part2[0] is valid. To confirm this use vector::at() and wrap it in a try{}catch(std::out_of_range&) block:

try
{
    cout << "escaped loop" << part2.at(n) << endl;
}
catch (std::out_of_range const&)
{
    std::cerr << "Access out of range\n";
}

To correct, confirm the vector is not empty() and use either [n - 1] or vector.back().

Sign up to request clarification or add additional context in comments.

3 Comments

Ive removed that part from the code as it wasn't necessary, thanks for pointing it out though, im getting mixed up with programming languages..
@user2054823, it is unclear to me if the problem is now resolved? BTW, unless noerror is required elsewhere you can just break from the while inside the if and noerror variable is no longer required.
@hmjd Don't recommend using break to an inexperienced programmer. Globally, it's almost never a good idea. A better solution would be to create a function for read with prompt, and loop on that: something like while ( getlineWithPrompt( prompt, line ) ) (where getlineWithPrompt returns std::cin).
2

Why are you using cin.ignore()? As written, cin.ignore() will only return after having read 10 characters. Do you really want the user to have to enter 10 characters after the input (before he enters a new line)? If your input is line oriented, the usual procedure is to use std::getline to read the entire line, then parse it using std::istringstream; alternatively, you can use something like std::cin.ignore( MAX_INT, '\n' );, which will ignore up to the next new line.

2 Comments

I've fixed the problem, it turns out it didn't like: cin.ignore(10); and was never actually getting to the line where the bool was set to false. Im not sure why this was a problem but it was solved by changing it to: cin.ignore(10,'\n'); If anyone could explain that to me i would be grateful, thanks for all the help so far!
@user2054823 As I said in my answer, cin.ignore(10) will not return until it has been able to read 10 characters (or it encounters end of file). And cin.ignore( 10, '\n' ); will still ignore at most 10 characters; if the user enters 20 blanks after the number, you probably won't get the desired effect.
0

Change

cin.ignore(10);

to

cin.ignore(10,'\n');

then the loop exits, try it!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.