i have a problem with this code. Everything seems to be working fine but when you type something like this: 123aaaddfa123 then program will accept this as double value. Any ideas to avoid this problem? Thank you.
int main ()
{
const char MAX = 5;
cout << "Weight of your fishes (maximum 5)" << endl;
double fishes[MAX];
char count = 0;
while (count < MAX)
{
cout << count + 1 << ". fish: " << endl;
if (cin >> fishes[count])
count++;
else {
cin.clear();
if (cin.get() == 'q') //no more fishes = quit
break;
while (cin.get() != '\n') //clearing cin buffer
continue;
}
}
if (count > 0) {
double sum = 0;
for (int i = 0; i < MAX; ++i) {
sum += fishes[i];
}
cout << "Avarage weight of your " << (int)count << " fishes is: " << sum / count << " KG" << endl;
}
}