Overtime
2011-09-29 18:18:19
hey guys okay so my program runs. Which is great.
But i need help with the bottom where it asks for the age.
What is the condition that i may have to set or if statement that detects a character.
For example say a user inter 'a' instead of an integer like '8'.
I want it to tell the user to enter a Integer.
How do i do that?
But i need help with the bottom where it asks for the age.
What is the condition that i may have to set or if statement that detects a character.
For example say a user inter 'a' instead of an integer like '8'.
I want it to tell the user to enter a Integer.
How do i do that?
#include "StdAfx.h"
#include<iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
char ans; // The user's answer to the question
int age; // The user's age
bool correct = false; // The boolean flag
ans = ' ';
////////////////////////////////
// Beginning of your code
// Ask the user a yes/no question and make sure the response is
// either y or n
while ( (ans != 'y') && (ans != 'n') )
{
cout << " Do you want a college degree (y/n) ";
cin >> ans;
cout << endl;
if ( (ans != 'y') && (ans != 'n') )
cout << " Incorrect Value "
<< endl
<< endl;
}
while (!correct)
{
cout << " Enter your age as an integer: ";
cin >> age;
cout << endl;
if ( !cin )
{
cin.clear();
cin.ignore(2000, '\n');
}
else
correct = true;
cout << "Thanks for your input!" << endl;
}
system("pause");
return 0;
}