Post down your code please.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
the cctype header has a function called isdigits(...). You can use that
to validate whether a string contains integer. There are also isspace(..)
among other things in that library. Check out the library , here
Here is an example :
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
string phoneNumber = "777777777";
bool pass = true;
for(int i = 0; i < phoneNumber.size(); i++)
{
if( isdigit(phoneNumber[i]) )
pass = true;
else
{
pass = false;
break;
}
}
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
a way you can do this would be to have a void check function inside a while loop in your main function.
void CheckInput(char[80], bool*);
int main()
{
char[80] cake;
bool good = false;
while (!good)
{
cout << "please enter a cake: "
cin >> cake;
CheckInput(cake, &good);
}
//...
}
void CkeckInput(char[80] cake, bool* good)
{
// run a switch checking cake agiants the valid choices
// if the check succeds then set good to true otherwies do nothing
}
im not sure if you are using pointers though so if you are not this solution wont work.
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
a way you can do this would be to have a void check function inside a while loop in your main function.
void CheckInput(char[80], bool*);
int main()
{
char[80] cake;
bool good = false;
while (!good)
{
cout << "please enter a cake: "
cin >> cake;
CheckInput(cake, &good);
}
//...
}
void CkeckInput(char[80] cake, bool* good)
{
// run a switch checking cake agiants the valid choices
// if the check succeds then set good to true otherwies do nothing
}
im not sure if you are using pointers though so if you are not this solution wont work.
Sincecake is a character array, the following instruction will allow an array overflow (in case there are more than 80 characters entered):
cin >> cake; ,
you can prevent this by for example using cin.getline(cake, 80);
or cin >> setw(80) >> cake; (but for this one you'll need to include theiomanip-header)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
yeah that is true tux. i normally use getline but this was a really quick and dirty example. i would hope the O.P. would use good programing techniques in his code.
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
on this line
while ((sort != 'chocolate') && (sort != 'carrot') && (sort != 'custard') && (sort != 'fruit') && (sort != 'coffee')
you are using single quotes for the words. you need to use double quotes.
while ((sort != "chocolate") && (sort != "carrot") && (sort != "custard") && (sort != "fruit") && (sort != "coffee")
also since this thread is solved i would suggest starting a new one if you have any other problems.
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189