制作程序,以便if语句被分配正确的编号

Making program so that if statement is assigned proper number

本文关键字:分配 编号 语句 程序 以便 if      更新时间:2023-10-16

我试图做到这一点,以便在数组中找不到行星名称时,它会将代码变量设置为 -1,但它似乎总是使变量 -1 无论如何。 如果我没有 else 语句,程序工作正常,但开关的默认语句不起作用。 我该如何做到这一点,以便在行星名称输入错误时代码变量获得正确的值。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
//declare arrays
string planetNames[8] = { "MERCURY", "VENUS", "EARTH", "MARS", "JUPITER", "SATURN", "URANUS", "NEPTUNE" };
double gravity[8] = { 0.37, 0.78, 1, 0.38, 2.64, 1.16, 1.07, 1.21 };
//declare variable
string name = " ";
double weight = 0.0;
string planet = " ";
double finalWeight = 0.0;
int code = 0;
//explain program to user
cout << "In this program you will enter your first and last name, then your weight, and finally the planet that you want to know your weight on" << endl << endl;
//get input
cout << "What is your first and last name: ";
getline(cin, name);
cout << "How much do you weigh: ";
cin >> weight;
cout << "Which planet would you like to know how much you weigh on: ";
cin >> planet;
//capitalize planet name
transform(planet.begin(), planet.end(), planet.begin(), toupper);
while (weight != -1)
{
    for (int x = 0; x < 8; x++)
    {
        if (planet == planetNames[x])
            code = x; //THIS IS WHERE THE PROBLEM IS!!!!!!!!!
        else
            code = -1;
        //end if
    }//end for
    //calculate and display weights
    switch (code)
    {
    case 0:
        finalWeight = weight * gravity[code];
        cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
        break;
    case 1:
        finalWeight = weight * gravity[code];
        cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
        break;
    case 2:
        finalWeight = weight * gravity[code];
        cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
        break;
    case 3:
        finalWeight = weight * gravity[code];
        cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
        break;
    case 4:
        finalWeight = weight * gravity[code];
        cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
        break;
    case 5:
        finalWeight = weight * gravity[code];
        cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
        break;
    case 6:
        finalWeight = weight * gravity[code];
        cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
        break;
    case 7:
        finalWeight = weight * gravity[code];
        cout << "The weight of " << name << " on " << planetNames[code] << " would be " << finalWeight << "lbs" << endl;
        break;
    default:
        cout << "Invalid planet name" << endl;
    }//end switch
    cout << endl;
    cout << "How much do you weigh(-1 to end the program): ";
    cin >> weight;
    cout << "Which planet would you like to know how much you weigh on: ";
    cin >> planet;
    transform(planet.begin(), planet.end(), planet.begin(), toupper);
}//end while

system("pause");
return 0;

}

找到

匹配的行星名称后,您需要break循环,否则循环会继续搜索,并且下一个名称将不匹配,因此code设置为 -1。

另外:为了效率和清晰度:在循环之前设置code = -1,并且仅在匹配时设置为x

code = -1;
for (int x = 0; x < 8; x++)
{
    if (planet == planetNames[x])
    {
        code = x; //THIS IS WHERE THE PROBLEM IS!!!!!!!!!
        break; // And now it's gone ;-)
    }
}

这很好。例如,如果键入 MARS,则代码将获得值 3。但是在循环的下一步中,您将再次将其设置为 -1 ;)找到结果时中断循环。

建议:将这部分代码拆分为新函数。