SRL Archive - C/C++ Help and Tutorials

C++ Flag-controlled-loops


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?


#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

2011-09-29 18:32:47

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

2011-09-29 18:37:47

From: Frement
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

2011-09-29 18:41:35

From: Overtime
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

2011-09-29 18:46:54

i want something i can use in an if statement.

Frement

2011-09-29 18:48:19

if (isdigit(string_variable[0])) { //do something }

Overtime

2011-09-29 18:55:55

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 = &#8216; &#8216;; bool correct = false;
while( !correct ) {
// get user input for gender
if( (gender != &#8216;M&#8217;) && (gender != &#8216;F&#8217;) ) {
cout << &#8220;Incorrect value&#8221; << endl;
} else {
correct = true;
}

sf411

2011-09-29 19:17:03

Do you mean something like this:

if (age.fail()){

blah;
blah;
}


I think you could also do:

if (!age){

blah;
blah;
}

Frement

2011-09-29 19:23:25

//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

2011-09-29 19:30:46

Yea we aren't that advanced in our class yet. Im looking for something more basic.

Frement

2011-09-29 19:31:57

From: Overtime
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

2011-09-29 19:51:46

Give this a go:

if (!(cin >> age))
{

cin.clear();
cin.ignore(2000, '\n');

}
else
{

correct = true;
cout << "Thanks for your input!" << endl;
}

HyperSecret

2011-09-29 19:54:37

From: sf411
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

2011-09-29 20:04:18

From: HyperSecret
^^ 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

2011-09-29 20:12:04

From: sf411
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

2011-09-30 02:28:58

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

2011-09-30 02:58:34

	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.