This question already has an answer here:
- How to make cin take only numbers 2 answers
The c++ compiler I've been using crashes whenever a letter is put into a equation. it'd be so much easier if an IF statement would just stop letters from getting to the equation. i tried this as a test from a program that I thought that would stop letters but for some reason letters still get through.
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
using namespace std;
int
main ()
{
char choice;
bool run = true;
while (run)
{
do
{
int plz;
bool done = false;
{
while (!done)
{
cout << "work plz ";
cin >> plz;
if (plz = < 0 || plz > 0)
cout << "work";
done = true;
else
{
cout << "error " << plz << " is not a number" << endl;
done = false;
}
}
}
cout << "Would you like to try again?(Y/N)";
cin >> choice;
cout << endl;
choice = tolower (choice);
}
while (choice != 'n' && choice != 'y');
if (choice == 'n')
{
run = false;
}
else if (choice == 'y')
{
run = true;
}
else
{
choice != 'n' && choice != 'y';
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire