C++ while循环上的编译错误
我无法编译代码,因为代码的最后一部分我希望程序询问用户是否愿意继续并再次使用该程序。任何帮助都将不胜感激我是初学者谢谢! #包括 使用名称空间stdC++ while循环上的编译错误,c++,compilation,C++,Compilation,我无法编译代码,因为代码的最后一部分我希望程序询问用户是否愿意继续并再次使用该程序。任何帮助都将不胜感激我是初学者谢谢! #包括 使用名称空间std //function prototype void input(double &feet, double &inches); void conversion(double feet, double &inches, int &meters, double ¢imeters); void output(
//function prototype
void input(double &feet, double &inches);
void conversion(double feet, double &inches, int &meters, double ¢imeters);
void output(int meters, double centimeters);
void main() //Input Function
{
//declaring variables
int meters; //declaring meters
double feet, //declaring feet
inches, //declaring inches
centimeters; //declaring centimeters
//loop
{
input(feet, inches); //function
conversion(feet, inches, meters, centimeters); //function
output(meters, centimeters); //function
}
}
void input(double &feet, double &inches)
{
cout << "Enter number of feet: ";
cin >> feet;
while (feet < 0) {
cout << "ERROR. Please enter a positive value for feet.";
cout << "\nEnter number of feet: ";
cin >> feet;
}
cout << "Enter number of inches: ";
cin >> inches;
while (inches < 0) {
cout << "ERROR. Please enter a positive value for inches.";
cout << "\nEnter number of inches: ";
cin >> inches;
}
}
void conversion(double feet, double &inches, int &meters, double ¢imeters)
{
inches += 12 * feet;
centimeters = inches * 2.54;
meters = centimeters / 100;
centimeters -= meters * 100;
}
void output(int meters, double centimeters)
{
char menu;
cout << meters << " meter(s) and " << centimeters << " centimeters " << endl;
cout << "Continue(Y/N) ";
cin >> menu;
} while (menu == 'Y' || menu == 'y');
return 0;
}
//函数原型
无效输入(双英尺、双英寸);
空隙转换(双英尺、双英寸和英寸、整数和米、双英寸和厘米);
空隙输出(整数米,双厘米);
void main()//输入函数
{
//声明变量
int米;//声明米
双足,//双足
英寸,//声明英寸
厘米;//表示厘米
//环路
{
输入(英尺、英寸);//函数
转换(英尺、英寸、米、厘米);//函数
输出(米,厘米);//函数
}
}
无效输入(双英尺、双英寸和英寸)
{
cout>脚;
而(英尺<0){
缩脚;
}
不能>英寸;
而(英寸<0){
cout英寸;
}
}
空隙转换(双英尺、双英寸和英寸、整数和米、双英寸和厘米)
{
英寸+=12*英尺;
厘米=英寸*2.54;
米=厘米/100;
厘米-=米*100;
}
无效输出(整数米,双厘米)
{
字符菜单;
cout正如一条评论所指出的。您缺少do{
我还添加了一个cin.ignore();
do{
cout << meters << " meter(s) and " << centimeters << " centimeters " << endl;
cout << "Continue(Y/N) ";
cin >> menu;
cin.ignore(); //i think if you press enter it will add white space.
} while (menu == 'Y' || menu == 'y');
do{
您是否需要一个do while
循环。您缺少do
。我也尝试过,但可能是做错了。我的do while循环返回值有两个错误与函数类型不匹配,并且“output”“void”函数返回一个值。这就是我执行do while循环void输出的方式(整数米,双厘米){char menu;难道你不应该在void
函数中使用返回0;
。这样做有效!唯一的问题是当你输入“Y”而不是允许你重新输入英尺和英寸进行转换时,它只会重新打印以前的结果。我如何修复它?仍然运行相同的:(@jackblack,如果有效,请将其标记为解决方案。此外,阅读此内容可了解有关do while循环的更多信息
do{
input(feet, inches); //function
conversion(feet, inches, meters, centimeters); //function
output(meters, centimeters); //function
cout << "Continue(Y/N) ";
cin >> menu;
} while (menu == 'Y' || menu == 'y');