View Full Version : C++ Flag-controlled-loops
Overtime
09-29-2011, 10:18 PM
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?
#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;
}
Frement
09-29-2011, 10:32 PM
You can use isdigit() to check if the first character is a digit, and then use atoi() to convert it to integer to be used as the age.
Overtime
09-29-2011, 10:37 PM
You can use isdigit() to check if the first character is a digit, and then use atoi() to convert it to integer to be used as the age.
not the answer im looking for.
Frement
09-29-2011, 10:41 PM
not the answer im looking for.
You can use isdigit() to check if the character is a digit or not, I gave you the answer you were looking for, and added some extra information :)
Overtime
09-29-2011, 10:46 PM
i want something i can use in an if statement.
Frement
09-29-2011, 10:48 PM
if (isdigit(string_variable[0])) { //do something }
Overtime
09-29-2011, 10:55 PM
Something simple, im in a beginner class. My professor will be like wtf. Its something ive learned before, but forgot it.
How do i get it to only accept integers?
something like this. but i want it to be ANY character. Not just "M" or "F"
char gender = ‘ ‘; bool correct = false;
while( !correct ) {
// get user input for gender
if( (gender != ‘M’) && (gender != ‘F’) ) {
cout << “Incorrect value” << endl;
} else {
correct = true;
}
sf411
09-29-2011, 11:17 PM
Do you mean something like this:
if (age.fail()){
blah;
blah;
}
I think you could also do:
if (!age){
blah;
blah;
}
Frement
09-29-2011, 11:23 PM
//Now if I type my gender as: Male, the program would accept it.
if (tolower(gender[0]) == "m" || tolower(gender[0]) == "f") {
correct = true;
}
//And heres an example for your age problem.
int age_len = (sizeof(age) / sizeof(char));
for (int i = 0; i < age_len; i++) {
if (!isdigit(age[i])) {
correct = false;
break;
}
if (i == age_len) {
correct = true;
}
}
Overtime
09-29-2011, 11:30 PM
Yea we aren't that advanced in our class yet. Im looking for something more basic.
Frement
09-29-2011, 11:31 PM
Yea we aren't that advanced in our class yet. Im looking for something more basic.
That is basic, I can't make it more basic for you :( At least I don't know how to.
sf411
09-29-2011, 11:51 PM
Give this a go:
if (!(cin >> age))
{
cin.clear();
cin.ignore(2000, '\n');
}
else
{
correct = true;
cout << "Thanks for your input!" << endl;
}
HyperSecret
09-29-2011, 11:54 PM
Give this a go:
if (!(cin >> age))
{
cin.clear();
cin.ignore(2000, '\n');
}
else
{
correct = true;
cout << "Thanks for your input!" << endl;
}
^^ I don't think you fully understand what you are even typing :p. Checking to see if something gotten off of the stream (cin) and negating it will not tell you if what they entered is valid :duh:
http://www.cplusplus.com/reference/clibrary/cstdio/getchar/
getchar() possibly?
Then use the value returned and compare it to the ascii chart values for numerical values (numbers)? This is the method mosts of my beginner classes wanted for comparing/checking to see if things were of the correct format.
sf411
09-30-2011, 12:04 AM
^^ I don't think you fully understand what you are even typing :p. Checking to see if something gotten off of the stream (cin) and negating it will not tell you if what they entered is valid :duh:
http://www.cplusplus.com/reference/clibrary/cstdio/getchar/
getchar() possibly?
Then use the value returned and compare it to the ascii chart values for numerical values (numbers)? This is the method mosts of my beginner classes wanted for comparing/checking to see if things were of the correct format.
I do not think you know what you are talking about. I just tried it and it works.
Frement
09-30-2011, 12:12 AM
Give this a go:
if (!(cin >> age))
{
cin.clear();
cin.ignore(2000, '\n');
}
else
{
correct = true;
cout << "Thanks for your input!" << endl;
}
Thats actually very clever, I would never have thought of that :) But doesn't seem to be beginner stuff :D
Daniel
09-30-2011, 06:28 AM
As aforementioned, isdigit is the most simplest possible way and is of beginner level.
Use it. You won't get marked down for doing your own research. Google "c++ check if cin is number" and check the results. :)
Overtime
09-30-2011, 06:58 AM
while (!correct)
{
cout << " Enter your age as an integer: ";
cin >> age;
cout << endl;
if (!cin.good())
{
cout << " Enter a Integer."
<< endl
<< endl;
cin.clear();
std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else
correct = true;
I like this one much better.
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.