infinite loop troubles

im trying to write code for my previous boss but can not seem to get the program to be "idiot" proof.
im wanting to have multiple checks so that when someone inputs 'a' into a float (count) it will give me an error so it will allow the user to go back and fix mistakes before it throws the rest of the program off.

program runs fine until it hits the check to see if the value input was a char then it goes into the while loop and outputs "ERROR...." infinitly
shouldnt the !cin be able to tell if the input was of an invalid type or is there something im missing?
also have tried !count to see if that would give desired results and no luck
ive been able to test char to see if they are a certain letter and if an input file is valid but im unable to apply the same logic to test to see if the input is a float or not
here is code snip of the problem

{variable types predefined at begining of main
float count;
string item;
}

cout<< "Enter in the amount of " <<item<< " in store"<<endl;
cin>>count;
while(!cin)
{
cin.clear();
cout<<"ERROR!!! Please enter the correct amount of "<<item<<endl;
cin>>count;
}
Last edited on
Use this code instead:

#include <iostream>
using namespace std;

int main()
{

char* item = " something ";

int count;


cout << "Enter in the amount of " << item << " in store"<<endl;
cin >> count;

while ((cout<<"Enter amount: ") && !(cin>>count)){
cout<<"Invalid Input!"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'n');
}



cout << "good input "<<endl;

system("pause");
}
and system("pause") isn't really a good thing to include in your code but here it can hold your program for you to see if the input was good.
lmao thanks for the help i should have used the cin.ignore(200,'n') that was a very rookie mistake works perfect thanks to the help
no problem. everyone makes mistakes)
yea im using it (system("pause")) to get echo prints but its saving everything to a file so that way i can help my old boss keep a inventory of critical items at his store. very basic code but will be able to give him a good estimate for his food cost to help with bonuses
Topic archived. No new replies allowed.