DEV Community

Weeaboo420
Weeaboo420

Posted on

2

C++ stuck inside while-loop?

I am trying to learn C++ and so far it has been fairly straightforward. I have been following a course on YouTube and I'm about halfway through.

So I decided that I wanted to make a BMI calculator, simple enough right? I decided I wanted to use while loops when reading different values from the user and it works for the most part. The problem comes when the user enters something other than a number, the program gets stuck inside an if-statement and doesn't seem to be able to break out and I don't know how to handle it.

In a language like Python or even C#, while-loops work as I expect them to but in c++ they seem to be somewhat different or maybe there is something strange with using cin, I am still pretty new c++.

Here's my code:

#include <iostream>
#include <conio.h>

using namespace std;


int main()
{
    //Keep track of the data we need and the
    //data we want to calculate later
    float bmi = 0;
    float weight = 0;
    float height = 0;

    //Keep track of which while loop we should
    //be inside.
    //0 = weight loop,
    //1 = height loop
    int loopCount = 0;


    while(loopCount == 0)
    {   
        cout << "Enter your weight in kgs: ";
        cin >> weight;

        if(weight <= 0)
        {
            cout << loopCount << " ERROR: Please enter a valid weight in kgs" << endl;
        }

        else
        {
            loopCount = 1;
        }         
    }


    while(loopCount == 1)
    {    
        cout << endl;
        cout << "Press any key to continue...";
        getch();

        return 0;
    }

    return 0;

}

To clarify, when a user inputs a number everything works fine. When the user doesn't input a number the program freaks out and it gets stuck printing the error message over and over again. I tried using cin.fail() but that produced the same results so I am really clueless here.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (2)

Collapse
 
bhupesh profile image
Bhupesh Varshney 👾 • Edited

Does this work ?

cout << "Enter your weight in kgs: " << flush;
if (!(cin >> weight)) {
    cout << "You did not enter a correct number!" << endl;
    // Leave the program, or do something appropriate:
}

Also make sure to add the #help tag to reach more visibility

Collapse
 
nanarayanan profile image
nanarayanan

Based on en.cppreference.com/w/cpp/io/basic...

Your program needs to clear the failure and skip the bad input.
cin.clear(); // unset failbit
cin.ignore(std::numeric_limitsstd::streamsize::max(), '\n'); // skip bad input

Your program needs to include limits (#include ) too.

I would suggest you pls go through further details given in the link above.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay