0

I have a C++ program that takes input from the terminal, and for some reason this is producing an endless loop:

double getSideLength()
{
    cout << "Enter a side: "
double side;
cin >> side; cin.ignore( 80, '\n' );
while (side <= 0){
    cout << "Please enter a valid side. Try again: ";
    cin >> side; cin.ignore(80, '\n');
}
return side;

This produces the output:

Enter a side: invalid
Please enter a valid side. Try again:
Please enter a valid side. Try again:
Please enter a valid side. Try again:
   .... and so on. "invalid" is the only input the user made
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Chris
  • 1,037
  • 15
  • 26

4 Answers4

2

as said in operator>> documentation stream get its failbit on if "The input obtained could not be interpreted as an element of the appropriate type." That happens if you enter wrong number. So you just have to clear() cin before next input. Here is code:

double getSideLength()
{
        double side;
        cout << "Enter a side: ";
        cin >> side;
        if (!cin)
             cin.clear();
        cin.ignore( 80, '\n' );
        while (side <= 0){
                cout << "Please enter a valid side. Try again: ";
                cin >> side;
                if (!cin)
                    cin.clear();
                cin.ignore( 80, '\n' );
        }
        return side;
}
2r2w
  • 1,384
  • 1
  • 12
  • 29
1

This should fix the issues. Using !(cin >> side) will make sure that we get the correct type from cin.

double getSideLength()
{
    cout << "Enter a side: " << std::endl;
    double side = -1;
    while ( ! (cin >> side) and side <= 0)
    {
        cout << "Please enter a valid side. Try again: ";
        cin.clear();
        cin.ignore(1000, '\n');
    }

    return side;
}
shuttle87
  • 15,466
  • 11
  • 77
  • 106
0
double getSideLength()
{
    double side;
 getinput:
    cout << "Enter a side: "
    cin >> side; cin.ignore( 80, '\n' );
    if(side <= 0)
    {
        cout << "Please enter a valid side. Try again: "
        goto getinput;
    }
    return side;
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Philip Badilla
  • 1,038
  • 1
  • 10
  • 21
  • You shouldn't use goto clauses in your code. – Pochi Feb 15 '12 at 07:30
  • im sorry why? im not a c++ guru and id like to know the reason why. thanks... – Philip Badilla Feb 15 '12 at 07:32
  • 1
    @Pochi: they might be awkward to read, follow and maintain, but there is nothing *wrong* with them (even MS examples have them), the compiler will take all loops down to computed jumps anyways. – Necrolis Feb 15 '12 at 07:34
  • 1
    goto is deemed bad style. The rationale behind it is that you could accomplish the same with a while loop and `continue`s, which makes code readable. Nobody uses goto clauses in any type of presentable C++ code. @Necrolis: That defeats one of the reasons why you code in higher level languages -- abstraction. You might as well code in assembler. – Pochi Feb 15 '12 at 07:35
  • 1
    i just thought it is fine because it was a small function... but i think he is having a problem with this part of his code **cin >> side; cin.ignore(80, '\n');** can't test though – Philip Badilla Feb 15 '12 at 07:40
  • @Pochi: you didn't get my point, there is no *real reason* you cannot use `goto`, just cause the style police don't like it, doesn't mean its evil (in fact sometimes it may be required, in extreme cases, normally for flow control in VM interpreters). I do agree using it without cause is poor form however. – Necrolis Feb 15 '12 at 07:41
  • @Pochi seeing as this is just a fragment. that might just be ripped apart anyways. the logic is the major focus of posting code in an answer. the major point is that this code simply has the compiler skip the step of parsing it into the same thing,and all it will do is just replacing the word with a control character. in most cases style is subjective much like art, and when it comes down to it even "best practices" are guidelines (much like a recipe for food), and not commandments – gardian06 Feb 15 '12 at 07:48
  • I said you shouldn't, not that you can't. Yes it may be required in extreme cases (I've never encountered them). It's just a note to keep with good practice. Good practice = good code = your coworkers won't hate reading your code and your boss won't make you rewrite it. – Pochi Feb 15 '12 at 07:56
  • guys i get all your points.. thanks for the feedback! =) im learning alot here im new by the way.. – Philip Badilla Feb 15 '12 at 08:04
0

It seems that there are a lot of intricacies with cin and in general, as is explained here, it might be preferable for you to use getline instead, and then use atoi.

Additionally, this issue of an infinite loop is also discussed here. If you would like to use cin, you can do so with the input into a string variable and then convert it into an integer using atoi as is displayed in this post.

Aayush Kumar
  • 1,618
  • 1
  • 11
  • 31