制作程序,以便if语句被分配正确的编号
Making program so that if statement is assigned proper number
我试图做到这一点,以便在数组中找不到行星名称时,它会将代码变量设置为 -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 ;)找到结果时中断循环。
建议:将这部分代码拆分为新函数。
相关文章:
- 将数组的地址分配给变量并删除
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- 函数中堆分配的效果与缺少堆分配的情况
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 获取字符串的长度并将其分配给数组
- 将地址分配给本地指针后,公共对象的变量将消失
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 我在二维向量中是否正确分配了内存
- 正在尝试重载二进制搜索树分配运算符
- GlobalAlloc而不是其他分配方法
- 自定义先决条件对移动分配运算符有效吗
- 为什么在为其分配编号后无法显示我的字符串值?
- 未分配阵列上的编号
- 为什么VS2015 MFC中的相同内存泄漏导致内存分配编号不断更改
- _CrtSetBreakAlloc使用内存内容,而不是分配编号
- 制作程序,以便if语句被分配正确的编号