我用else if语句做错了什么?
What am I doing wrong with the else if statements
程序告诉我它需要一个带有else if语句的语句。我是新来的,在我上大学的CS课程之前,我正在努力学习c++代码。提前感谢!
#include <iostream>
#include <string>
using namespace std;
string color;
string Blue;
string Green;
string Brown;
int age;
int main()
{
cout << "what is the color of your eyes ? (use capitalization)" << endl << "colors to choose from are " << endl << "Blue" << endl << "Green" << endl << "Brown";
cin >> color;
if (color == Blue); {
cout << "you are an intelligent person " << endl;
system("pause");
}
else if (color == Green); {
cout << " you are a peaceful person " << endl;
system("pause");
}
else if (color == Brown); {
cout << "you are a normal go get 'em person " << endl;
system("pause");
}
cin.ignore();
cin.get();
return 0;
}
你的问题是括号后面有分号。:
if (color == Blue); {
cout << "you are an intelligent person " << endl;
system("pause");
}
应该是这个
No semi colon here
v
if (color == Blue) {
cout << "you are an intelligent person " << endl;
system("pause");
}
其余的else-if
也是一样还有,正如其他人提到的,您需要指定Blue, Green和Brown是什么。或者这样做:
const string Blue = "Blue";
const string Green = "Green";
const string Brown = "Brown";
或者:
if (color == "Blue") { //Note the ""
cout << "you are an intelligent person " << endl;
system("pause");
}
你在不属于分号的地方放了分号。您还需要为用于比较的字符串提供值:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string color;
string Blue = "Blue";
string Green = "Green";
string Brown = "Brown";
int age;
cout << "what is the color of your eyes ? (use capitalization)" << endl << "colors to choose from are " << endl << "Blue" << endl << "Green" << endl << "Brown";
cin >> color;
if (color == Blue)
{
cout << "you are an intelligent person " << endl;
system("pause");
}
else if (color == Green)
{
cout << " you are a peaceful person " << endl;
system("pause");
}
else if (color == Brown)
{
cout << "you are a normal go get 'em person " << endl;
system("pause");
}
cin.ignore();
cin.get();
return 0;
}
您需要删除if和else if语句后面的错误分号,如下所示:
if (color == Blue) {
cout << "you are an intelligent person " << endl;
system("pause");
}
else if (color == Green) {
cout << " you are a peaceful person " << endl;
system("pause");
}
else if (color == Brown) {
cout << "you are a normal go get 'em person " << endl;
system("pause");
}
在c++中,可以将;
视为语句结束符。
像if (color == "Blue");
这样的东西本身就是一个语句。但是它没有做任何事情,因为;
终止了if
语句。
此外,还可以使用大括号{
和}
对语句进行分组。实际上,这对于限制大括号内声明的变量的作用域非常有用。您将在适当的时候发现这对程序稳定性的积极影响。
长话短说,一旦修复了可疑的字符串比较(您需要像我上面所做的那样对字面量使用引号字符),您的代码在语法上是完全有效的。这就是它编译的原因。(尽管一个好的编译器应该警告你空的if
控制块)。但是这并不是您想要的:您需要删除所有包含if
的行上多余的;
。
除了额外的分号之外,您还没有实际初始化字符串值,因此它们都是空字符串。您需要在开头添加:
Blue = "Blue";
Green = "Green";
Brown = "Brown";
您的变量Blue
, Green
和Brown
是空字符串变量。
string Blue; // empty string with name Blue
string Green; // empty string with name Green
string Brown; // empty string with name Brown
首先,您需要删除错误的分号,并且需要使用带内容的字符串。您可以省略变量并使用字符串字面值:
if (color == "Blue") {
或者您需要在字符串中填充一些数据:
std::string Blue("Blue");
// ...
if (color == Blue); {
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 我在查找和字符串上做错了什么?
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- 插入排序:我做错了什么?