C ++ Planet Program Loop Skipping

时间:2014-02-16 01:54:52

标签: c++

我正在编写一个程序,要求用户输入他们的名字,体重和星球,以了解他们在给定星球上的重量。该程序工作正常,但它第二次跳过名称输入。我需要一些关于如何解决这个问题的建议。

#include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
//declare variables and arrays
string planets[8] = {"mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune"};
double gravities[8] = {0.37, 0.78, 1.00, 0.38, 2.64, 1.16, 1.07, 1.21};
double weight = 0.0;
string planetChoice = " ";
string name = " ";

//give the user information and instructions
cout << "~~~~~~~~~~~~~~~~~~~~~Find your weight on different planets!~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "This program calculates what your weight would be on various planets in " << endl;
cout << "our solar system. To use this program, enter your full name, weight, and the " << endl;
cout << "planet when prompted." << endl;
cout << endl;
cout << "Choice of planets:" << endl;
cout << "Mercury, Venus, Earth, Mars, Jupiter," << endl;
cout << "Saturn, Uranus, and Neptune" << endl;
cout << endl;


cout << "Full name (first and last) (\"exit\" to stop): ";
getline (cin, name);

while (name != "exit")
{
    cout << "Enter your weight: ";
    cin >> weight;

    cout << "Choose your planet: ";
    cin >> planetChoice;
    transform(planetChoice.begin(), planetChoice.end(), planetChoice.begin(), tolower);

    for (int x = 0; x < 8; x++)
    {
        if (planetChoice == planets[x])
        {
            weight = weight * gravities[x];
            switch(x)
            {
                case 0:
                    cout << name << ", Your weight on the plannet closest to the sun (Mercury) would be: " << weight << "You'd be light as a feather!" <<endl;
                    break;
                case 1:
                    cout << name << ", your weight on Venus would be: " << weight << "You'd be a bit lighter!" << endl;
                    break;
                case 2:
                    cout << name << ", your weight on your home planet (Earth) is: " << weight << endl;
                    break;
                case 3:
                    cout << name << ", your weight on the red planet (Mars) would be: " << weight << endl;
                    break;
                case 4:
                    cout << name << ", your weight on the largest planet (Jupiter) would be: " << weight << endl;
                    break;
                case 5:
                    cout << name << ", your weight on the planet with rings (Saturn) would be: " << weight << endl;
                    break;
                case 6:
                    cout << name << ", your weight on The Bull's Eye Planet (Uranus) would be: " << weight << "Your weight would be close to normal" << endl;
                    break;
                case 7:
                    cout << name << ", your weight on the ice cold planet of Neptune would be: " << weight << endl;
                    break;
                default:
                    cout << "Invalid planet name" << endl;
                    break;
            }//end switch
        }//end if
    }//end for  
    cout << "Full name (first and last) (\"exit\" to stop): ";
    getline (cin, name);
}//end while

system("pause");
return 0;
}//end of main function

0 个答案:

没有答案