【发布时间】:2015-02-10 07:54:47
【问题描述】:
我在第一个菜单中使用了 for 循环,但我决定添加一个二级菜单选项以消除混乱。
当我添加第二个菜单时,for 循环搞砸了一切,所以我将其删除。我只是希望程序能够在用户输入菜单列表中不存在的数字时说“数字无效,请输入正确的数字”。
代码如下...
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double mi, ki, F, C;
double lbs, kg, ft, m;
int choice, choice2 = 0;
cout<<" MENU ONE " << endl;
cout<<"*************************************" << endl;
cout<<"|1.) Convert miles to kilometers |" << endl;
cout<<"|2.) Convert kilometers to miles |" << endl;
cout<<"|3.) Convert Fahrenheit to Celsius |" << endl;
cout<<"|4.) Convert Celsius to Fahrenheit |" << endl;
cout<<"|5.) FOR MORE CONVERSIONS |" << endl;
cout<<"*************************************" << endl;
cin>> choice;
if (choice >= 1 && choice < 5){
switch (choice)
{
case 1:
cout<< "Now enter the amount of miles you wish to convert: " <<endl;
cin>> mi;
cout<< "The answer is: " << mi / 0.62137 <<" Kilometers" <<endl;
break;
case 2:
cout<<"Now enter the amount of Kilometers you want to convert: " << endl;
cin>> ki;
cout<< "The answer is: " << ki * .62137 <<" Miles " <<endl;
break;
case 3:
cout<<"Now enter the degree in Fahrenheit: " << endl;
cin>>F;
cout<< "The answer is: " << (F-32)/1.8 <<" degrees Celsius "<< endl;
break;
case 4:
cout<<"Now enter the degree in Celsius: " << endl;
cin>>C;
cout<< "The answer is: " << (C*1.8)+32 <<" degress Fahrenheit " << endl;
break;
}
}
//********************************************************************************************************//
else if (choice == 5)
{
cout<<" MENU TWO " << endl;
cout<<"*********************************" << endl;
cout<<"|1.) Convert Pounds to Kilograms|" << endl;
cout<<"|2.) Convert Kilograms to Pounds|" << endl;
cout<<"|3.) Convert Feet to Meters |" << endl;
cout<<"|4.) Convert Meters to Feet |" << endl;
cout<<"|********************************" << endl;
cin>>choice2;
}
if (choice2 >= 1 && choice2 < 5){
switch (choice2)
{
case 1:
cout<<"Now enter the weight in pounds: " <<endl;
cin>>lbs;
cout<<"The answer is: " << lbs / 2.2046 <<" kilograms " << endl;
break;
case 2:
cout<<"Now enter the weight in kilograms: " << endl;
cin>>kg;
cout<< "The answer is: " << kg * 2.2046 <<" pounds "<< endl;
break;
case 3:
cout<<"Now enter the number of feet to be converted to 'M': " <<endl;
cin>>ft;
cout<<"The answer is: " << (ft / 3.20808) << " meters "<< endl;
break;
case 4:
cout<<"Now enter the number of meters to be converted to 'FT': " <<endl;
cin>>m;
cout<<"The answer is: " << (m * 3.20808) << " feet "<< endl;
break;
return 0;
}
}
return 0;
}
【问题讨论】:
标签: c++