Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Need help, been working all day on this.
Closed Thread
Old 01-15-2006, 11:31 PM   #1 (permalink)
 
True Techie

Join Date: Jul 2004

Posts: 235

crazybeans

Unhappy Need help, been working all day on this.

Ok, my problem is a rather simple one (I think) but can't seem to find what I need online. I am writing a fitness program. The function I am having trouble with asks the user to enter 1 or 2 for male or female. I have it set up so if they enter >2 or <1 that it will simply loop, clear screen and ask question again. This part works fine, the problem is if the user enters "m" accidently the program freaks out and continues looping. How can I prevent the user from entering anything but 1 or 2. Is there a work-around for this problem?


Code:


// Assess your expenditure
// Ask user if male or female and calculate RMR dependant on given formula
// Store variables to be used in Step4
int Step2()
{
cout << "Enter your sex:\n\n";
cout << " [1] Male\n";
cout << " [2] Female\n\n";
cout << "Enter selection: ";
cin >> Sex;
if (Sex == 1 || Sex == 2)
{
if (Sex == 1)
{
system("CLS");
cout << "Enter your weight: ";
cin >> Weight;
cout << "\nEnter your Height (in inches): ";
cin >> Height;
cout << "\nEnter your age: ";
cin >> Age;
RMR = 66 + (13.7 * (Weight / 2.2)) + (5 * (Height * 2.54)) - (6.8 * Age);
return 0;
}
if (Sex == 2)
{
system("CLS");
cout << "Enter your weight: ";
cin >> Weight;
cout << "\nEnter your Height (in inches): ";
cin >> Height;
cout << "\nEnter your age: ";
cin >> Age;
RMR = 655 + (9.6 * (Weight / 2.2)) + (1.8 * (Height * 2.54)) - (4.7 * Age);
return 0;
}
}
else
{
system("CLS");
Step2();
return 0;
}
}
__________________
Come visit a new technology and digital image arena website where you may compete in monthly image contests and chat about technology. t3ch.l33t is currently searching for members to help manage different areas of the site. t3ch.l33t Image Arena Home

http://img.photobucket.com/albums/v3...ch_l33tsig.jpg
crazybeans is offline  
Old 01-16-2006, 03:19 PM   #2 (permalink)
 
Newb Techie

Join Date: Sep 2004

Posts: 13

sypher04

Default

if( sex != 1 && sex != 2 )
//return to the beginning of the loop
sypher04 is offline  
Old 01-16-2006, 03:20 PM   #3 (permalink)
 
Junior Techie

Join Date: Jun 2005

Posts: 94

ever_thus

Default

declare Sex as a char array and use the following code.

cin<<Sex;
if (atoi (Sex) == 1){
//etc...
}
else if (atoi (Sex) == 2){
//etc...
}else
//etc...

atoi converts a char array into an integer. Since cin is expecting characters it won't go crazy when it receives them.

Btw, you should put the your code in a loop while (Sex != 1 && Sex != 2). Avoid having functions call themselves (recursion) whenever you can.
ever_thus is offline  
Old 01-16-2006, 09:47 PM   #4 (permalink)
 
Newb Techie

Join Date: Aug 2005

Posts: 22

Cache

Default

It would actually be safer and easier to use std::string, as opposed to a char array.

if (Elstingo == "1")
Cache is offline  
Old 01-24-2006, 10:07 PM   #5 (permalink)
 
True Techie

Join Date: Jul 2004

Posts: 235

crazybeans

Default

Thanks alot for the help, I looked into this and this is what I found:

double atof(const char* p); // convert p[] to double ("alpha to floating")
double strtod(const char* p, char** end); // convert p[] to double ("string to double")
int atoi(const char* p); // convert p[] to int, assuming base 10
long atol(const char* p); // convert p[] to long, assuming base 10
long strtol(const char* p, char** end, int b); // convert p[] to long, assuming base b

I'm currently incorporating this new information into my code, I have also removed references to recursion and replaced them with while statements.
__________________
Come visit a new technology and digital image arena website where you may compete in monthly image contests and chat about technology. t3ch.l33t is currently searching for members to help manage different areas of the site. t3ch.l33t Image Arena Home

http://img.photobucket.com/albums/v3...ch_l33tsig.jpg
crazybeans is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On