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

时间:2015-03-19 08:41:21

标签: c++

#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 个答案:

答案 0 :(得分:0)

1)if (selection >= 'a' && selection <= 'h')用户可能会按下大写锁定,因此请考虑A~Z

2)(在我看来)退出应该是逻辑的开始。

3)您可以改用开关(选择)。 dafault:输入无效。再试一次。

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;
}