gpt4 book ai didi

c++ - 编写程序以便为 if 语句分配适当的编号

转载 作者:行者123 更新时间:2023-11-27 22:53:38 25 4
gpt4 key购买 nike

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

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

最佳答案

找到匹配的行星名称后,您需要中断循环,否则循环会继续搜索,下一个名称将不匹配,因此 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 ;-)
}
}

关于c++ - 编写程序以便为 if 语句分配适当的编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35236112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com