在while循环上编译错误

时间:2017-11-02 04:00:03

标签: c++ compilation

我无法获得要编译的代码,因为代码的最后一部分我希望程序询问用户是否希望继续并再次使用该程序。任何帮助将不胜感激我是初学者谢谢!     #include     使用namespace std;

//function prototype
void input(double &feet, double &inches);
void conversion(double feet, double &inches, int &meters, double &centimeters);
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 &centimeters)
{
    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;
}

1 个答案:

答案 0 :(得分:0)

正如一条评论所说的那样。您遗失了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{
        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');