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:
Whenever I try to insert an alphabet, I would enter an infinite loop
Thanks in advance
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