While loop with OR trouble
Jun 7, 2012 at 10:39pm UTC
I am still pretty new to c++, and Ii am struggling with the OR and AND operators. I am trying to build a loop into my functions so that it only accepts certain input. I have read on various sites, but nothing is clear as to what I am doing. I am having trouble with the functions at the bottom for my menus. What am I doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
//function for car selection menu
int car_menu()
{
int car_selection;
while ((car_selection < 0) || (car_selection > 4))
{
cout << "\nPlease enter a valid vehicle selection:" ;
cout << "\n1 - Avenger ($22,100)\n" ;
cout << "2 - Volare ($25,350)\n" ;
cout << "3 - LaSalle ($27,800)\n" ;
cout << "4 - Coronet ($30,257)\n" ;
cout << "0 - Exit\n" ;
cout << "Enter your choice: " ;
cin >> car_selection;
return car_selection;
}
}
//function for options menu
int option_menu()
{
int option_selection;
while ((option_selection < 0) || (option_selection > 5))
{
cout << "\nPlease enter a valid option selection :" ;
cout << "\n1 - Moonroof (+$1000)\n" ;
cout << "2 - Heated mirrors/rear glass (+$357)\n" ;
cout << "3 - Automatic transmission (+$850)\n" ;
cout << "4 - 4.7 liter V8 engine (+$1,250)\n" ;
cout << "5 - Heated leather seats (+$350)\n" ;
cout << "0 - No other options\n" ;
cout << "Enter your choice: " ;
cin >> option_selection;
return option_selection;
}
}
//function for region
int region_choice()
{
int region_selection;
while ((region_selection < 0) || (region_selection > 3))
{
cout << "\nPlease enter a valid tax selection:" << endl;
cout << "1 - Region 1 (6.5)\n" ;
cout << "2 - Region 2 (7.0)\n" ;
cout << "3 - Other\n" ;
cout << "Enter your choice: " ;
cin >> region_selection;
cout << endl;
return region_selection;
}
}
Last edited on Jun 8, 2012 at 11:11am UTC
Jun 8, 2012 at 1:02am UTC
Can you please use the code formatting tag to format your code.
There's quite a lot going on there, I'll look at one function right now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int car_menu()
{
int car_selection;
while ((car_selection < 0) || (car_selection > 4))
{
cout << "\nPlease enter a valid vehicle selection:" ;
cout << "\n1 - Avenger ($22,100)\n" ;
cout << "2 - Volare ($25,350)\n" ;
cout << "3 - LaSalle ($27,800)\n" ;
cout << "4 - Coronet ($30,257)\n" ;
cout << "0 - Exit\n" ;
cout << "Enter your choice: " ;
cin >> car_selection;
return car_selection;
}
}
The logic's needs attention.
First of all, car_selection is used to control the while loop, but it's uninitialised.
Then you return at the end of the loop, so you never actually go around the loop ever.
Topic archived. No new replies allowed.