Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Check entered value for zero. Infinite loop.

$
0
0
Hi guys, I am a newbie in c++ programming. I was trying to create program that will check for zero given an input.
However, I hope that when the user input a non-numeric char, it will prompt the user to enter an integer and resume the process.

I tried to do that using cin.clear() to remove unwanted input.
Here is my code:
#include <iostream>



int main()
{
int num(0);

while(true){
std::cout << "Please enter a number" <<std::endl;
while(std::cin >> num)
{
   if(num==0)
   {
      std::cout << "Is Zero" <<std::endl;
      return 0;
   }
   else
      std::cout << "Not zero" <<std::endl;

}
std::cout <<"Please enter an integer" <<std::endl;
std::cin.clear();
}
}



Whenever I try to insert an alphabet, I would enter an infinite loop
Thanks in advance

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>