语法错误,也想知道我是否可以使这段代码更有效率


#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main()
{
double weight, sgrav, distance, speed, pweight, distanceplanet, travel;
char selection;
string planet;
cout << "Welcome to INTERPLANETARY TRAVEL PROGRAM!" << endl;
cout << "This program enables you to find out your travel time to the planet" << endl;
cout << "you want to travel to as well as your weight on that planet." << endl;
cout << "Please enjoy the program and find the perfect planet for you!" << endl;
cout << endl
    << endl
    << "INTERPLANETARY TRAVEL MENU" << endl
    << "--------------------------" << endl
    << "a) Mercury" << endl
    << "b) Venus" << endl
    << "c) Earth" << endl
    << "d) Mars" << endl
    << "e) Jupiter" << endl
    << "f) Saturn" << endl
    << "g) Uranus" << endl
    << "h) Neptune" << endl
    << "q) quit" << endl
    <<endl
    << "Select a planet to travel to or q to quit the program: " << endl;
cin >> selection;
cout << "Please enter your weight (in lbs): " << endl;
cin >> weight;
cout << "Please enter the speed (in mile per hour) that you would like to travel at: " << endl
    << endl;
cin >> speed;
if (selection >= 'a' && selection <= 'h')
{
    if (selection == 'a')
    {
        sgrav = 0.86;
        pweight = (weight*sgrav);
        planet = "Mercury";
        distance = abs(36 - 93);
        travel = (distance * speed);
        cout << "INTERPLANETARY TRAVEL:  " << "Earth to " << planet << endl
            << "--------------------------------------------------" << endl
            << "Your weight on " << planet << ":      " << setprecision(2) << pweight << " lbs" << endl
            << endl
            << "Your travel time to " << planet << ":" << endl
            << "    In Hours: " << travel << " hours" << endl
            << "    In Days : " << (travel / 24) << " days" << endl
            << "    In years : " << (travel / 8760) << " years" << endl;
    }
    else if (selection == 'b')
    {
        sgrav = 0.86;
        pweight = (weight*sgrav);
        planet = "Venus";
        distance = abs(67 - 93);
        travel = (distance * speed);
        cout << "INTERPLANETARY TRAVEL:  " << "Earth to " << planet << endl
            << "--------------------------------------------------" << endl
            << "Your weight on " << planet << ":      " << setprecision(2) << pweight << " lbs" << endl
            << endl
            << "Your travel time to " << planet << ":" << endl
            << "    In Hours: " << travel << " hours" << endl
            << "    In Days : " << (travel / 24) << " days" << endl
            << "    In years : " << (travel / 8760) << " years" << endl;
    }
    else if (selection == 'c')
    {
        sgrav = 1.00;
        pweight = (weight*sgrav);
        planet = "Earth";
        distance = abs(93 - 93);
        travel = (distance * speed);
        cout << "INTERPLANETARY TRAVEL:  " << "Earth to " << planet << endl
            << "--------------------------------------------------" << endl
            << "Your weight on " << planet << ":      " << setprecision(2) << pweight << " lbs" << endl
            << endl
            << "Your travel time to " << planet << ":" << endl
            << "    In Hours: " << travel << " hours" << endl
            << "    In Days : " << (travel / 24) << " days" << endl
            << "    In years : " << (travel / 8760) << " years" << endl;
    }
    else if (selection == 'd')
    {
        sgrav = 0.37;
        pweight = (weight*sgrav);
        planet = "Mars";
        distance = abs(141 - 93);
        travel = (distance * speed);
        cout << "INTERPLANETARY TRAVEL:  " << "Earth to " << planet << endl
            << "--------------------------------------------------" << endl
            << "Your weight on " << planet << ":      " << setprecision(2) << pweight << " lbs" << endl
            << endl
            << "Your travel time to " << planet << ":" << endl
            << "    In Hours: " << travel << " hours" << endl
            << "    In Days : " << (travel / 24) << " days" << endl
            << "    In years : " << (travel / 8760) << " years" << endl;
    }
    else if (selection == 'e')
    {
        sgrav = 2.64;
        pweight = (weight*sgrav);
        planet = "Jupiter";
        distance = abs(483 - 93);
        travel = (distance * speed);
        cout << "INTERPLANETARY TRAVEL:  " << "Earth to " << planet << endl
            << "--------------------------------------------------" << endl
            << "Your weight on " << planet << ":      " << setprecision(2) << pweight << " lbs" << endl
            << endl
            << "Your travel time to " << planet << ":" << endl
            << "    In Hours: " << travel << " hours" << endl
            << "    In Days : " << (travel / 24) << " days" << endl
            << "    In years : " << (travel / 8760) << " years" << endl;
    }
    else if (selection == 'f')
    {
        sgrav = 1.17;
        pweight = (weight*sgrav);
        planet = "Saturn";
        distance = abs(886 - 93);
        travel = (distance * speed);
        cout << "INTERPLANETARY TRAVEL:  " << "Earth to " << planet << endl
            << "--------------------------------------------------" << endl
            << "Your weight on " << planet << ":      " << setprecision(2) << pweight << " lbs" << endl
            << endl
            << "Your travel time to " << planet << ":" << endl
            << "    In Hours: " << travel << " hours" << endl
            << "    In Days : " << (travel / 24) << " days" << endl
            << "    In years : " << (travel / 8760) << " years" << endl;
    }
    else if (selection == 'g')
    {
        sgrav = 0.92;
        pweight = (weight*sgrav);
        planet = "Uranus";
        distance = abs(1782 - 93);
        travel = (distance * speed);
        cout << "INTERPLANETARY TRAVEL:  " << "Earth to " << planet << endl
            << "--------------------------------------------------" << endl
            << "Your weight on " << planet << ":      " << setprecision(2) << pweight << " lbs" << endl
            << endl
            << "Your travel time to " << planet << ":" << endl
            << "    In Hours: " << travel << " hours" << endl
            << "    In Days : " << (travel / 24) << " days" << endl
            << "    In years : " << (travel / 8760) << " years" << endl;
    }
    else if (selection == 'h')
    {
        sgrav = 1.44;
        pweight = (weight*sgrav);
        planet = "Neptune";
        distance = abs(2793 - 93);
        travel = (distance * speed);
        cout << "INTERPLANETARY TRAVEL:  " << "Earth to " << planet << endl
            << "--------------------------------------------------" << endl
            << "Your weight on " << planet << ":      " << setprecision(2) << pweight << " lbs" << endl
            << endl
            << "Your travel time to " << planet << ":" << endl
            << "    In Hours: " << travel << " hours" << endl
            << "    In Days : " << (travel / 24) << " days" << endl
            << "    In years : " << (travel / 8760) << " years" << endl;
    }
}
else if (selection == 'q')
{
    cout << "You have chosen to quit the program. Thank you for using the program!" << endl;
}
else
{
    cout << "You have entered an invalid selection." << endl;
}
//system("PAUSE");
return 0;

对于这样一个简单的程序来说,它非常长,我想知道是否有人会知道一种更有效的方式来使用 if-else 语句输出信息,而不是复制粘贴到每种情况下。另外,我在声明双变量的行上收到语法错误,并且带有分号,我不明白为什么。

1( if (selection >= 'a' && selection <= 'h') 用户可以按下大写锁定,所以也考虑 A~Z

2((在我看来(退出应该在逻辑的开头。

3(您可以使用开关(选择(代替。错误:输入无效。再试一次。

4(将输出放在函数中并将变量传递给它,而不是重新编写相同的代码

void prn(float sgrav, int pweight, char arr[], float distance, float travel)
{
cout << "INTERPLANETARY TRAVEL:  " << "Earth to " << planet << endl
     << "--------------------------------------------------" << endl
     << "Your weight on " << planet << ":      " << setprecision(2) << pweight << " lbs" << endl
     << endl
     << "Your travel time to " << planet << ":" << endl
     << "    In Hours: " << travel << " hours" << endl
     << "    In Days : " << (travel / 24) << " days" << endl
     << "    In years : " << (travel / 8760) << " years" << endl;
}

最新更新