<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Weeaboo420</title>
    <description>The latest articles on Forem by Weeaboo420 (@weeaboo420).</description>
    <link>https://forem.com/weeaboo420</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F417191%2F73f68790-4a04-49a6-9f6a-7622032cf326.png</url>
      <title>Forem: Weeaboo420</title>
      <link>https://forem.com/weeaboo420</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/weeaboo420"/>
    <language>en</language>
    <item>
      <title>C++ stuck inside while-loop?</title>
      <dc:creator>Weeaboo420</dc:creator>
      <pubDate>Fri, 26 Jun 2020 05:06:43 +0000</pubDate>
      <link>https://forem.com/weeaboo420/c-stuck-inside-while-loop-l30</link>
      <guid>https://forem.com/weeaboo420/c-stuck-inside-while-loop-l30</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;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++.&lt;/p&gt;

&lt;p&gt;Here's my code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;conio.h&amp;gt;

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 &amp;lt;&amp;lt; "Enter your weight in kgs: ";
        cin &amp;gt;&amp;gt; weight;

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

        else
        {
            loopCount = 1;
        }         
    }


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

        return 0;
    }

    return 0;

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>loops</category>
    </item>
  </channel>
</rss>
