What's new

C & C++ Help

XieSmile

Eternal Poster
#include <iostream>
using namespace std;

int main() {

int num1;

cout << "What is your age? ";
cin >> num1;

if (num1 >= 0 && num1 <= 14) {
cout << "\nChild" << endl;
}
else if (num1 >= 15 && num1 <= 24) {
cout << "\nYouth" << endl;
}
else if (num1 >= 25 && num1 <= 64) {
cout << "\nAdult" << endl;
}
else if (num1 >= 65) {
cout << "\nSenior" << endl;
}
else {
cout << "\nPlease input valid number." << endl;
}

return 0;
}


Yung output po kasi kapag input yung any type of alphabet is Child yung lalabas ano po solution dito na kapag hindi number yung lalabas "Please input valid number"
 
Code:
while(!cin)   // Checks if input is valid type of variable declared, will loops if its false
{
    cout << "That was no integer! Please input valid number: ";
    cin.clear();
    cin.ignore();
    cin >> num1;
}

The rest of conditionals here.
|

Google data type validation.
 

Similar threads

Back
Top