为什么编译器告诉我没有运算符匹配我的 'if' 和 'else if' 语句中的操作数类型?
Why does the compiler tell me that no operators match the operand type in my 'if' and 'else if' statements?
这段代码有什么问题?我一直在尝试,但我不知道为什么我不能在if
语句中使用=
或==
。
#include <vector>
#include <cstdlib>
#include <time.h>
using namespace std;
/* I am trying to make the program output a periodic t symbol
so then it asks the user what the element is */
void symbols(std::vector<std::string> alcalinos) {
alcalinos = { "H", "Li", "Na", "K", "Rb", "Cs", "Fr" };
std::string elemento;
cout << "Introduce el elemento correspondiente al símbolo(en minusculas) : \n";
srand(time(NULL));
int random = rand() % 7;
cout << alcalinos[random];
cin >> elemento;
if ((alcalinos = "H") && (elemento = "hidrogeno")) {
cout << "correcto\n";
}
else if ((alcalinos = "Li") && (elemento = "litio")) {
cout << "correcto\n";
}
else if ((alcalinos = "Na") && (elemento = "sodio")) {
cout << "correcto\n";
}
else if ((alcalinos = "K") && (elemento = "potasio")) {
cout << "correcto\n";
}
else if ((alcalinos = "Rb") && (elemento = "rubidio")) {
cout << "correcto\n";
}
else if ((alcalinos = "Cs") && (elemento = "cesio")) {
cout << "correcto\n";
}
else if ((alcalinos = "Fr") && (elemento = "francio")) {
cout << "correcto\n";
}
else {
cout << "incorrecto\n";
}
int main(); {
void symbols();
}
我一定心情不错,我已经添加评论指出你犯的错误
void symbols() { // no parameter for function
// local declaration of vector instead of parameter
std::vector<std::string> alcalinos = { "H", "Li", "Na", "K", "Rb", "Cs", "Fr" };
std::string elemento;
cout << "Introduce el elemento correspondiente al símbolo(en minusculas) : \n";
int random = rand() % 7;
cout << alcalinos[random];
cin >> elemento;
// compare alcalinos[random] not alcalinos
// use == not =
if ((alcalinos[random] == "H") && (elemento == "hidrogeno")) {
cout << "correcto\n";
}
else if ((alcalinos[random] == "Li") && (elemento == "litio")) {
cout << "correcto\n";
}
else {
cout << "incorrecto\n";
}
} // missing }
int main() { // no ; after main()
srand(time(NULL)); // seed RNG once in main
symbols(); // no void before function call
}
当你犯这么多错误时,这意味着你试图一次编写太多代码。如果您是初学者,请逐字逐句地一次添加一行代码。并在添加的每一行之后测试代码。这样一来,您一次只能尝试解决 一个问题。你会发现这样更容易。
这段代码有什么问题?我一直在尝试,但我不知道为什么我不能在if
语句中使用=
或==
。
#include <vector>
#include <cstdlib>
#include <time.h>
using namespace std;
/* I am trying to make the program output a periodic t symbol
so then it asks the user what the element is */
void symbols(std::vector<std::string> alcalinos) {
alcalinos = { "H", "Li", "Na", "K", "Rb", "Cs", "Fr" };
std::string elemento;
cout << "Introduce el elemento correspondiente al símbolo(en minusculas) : \n";
srand(time(NULL));
int random = rand() % 7;
cout << alcalinos[random];
cin >> elemento;
if ((alcalinos = "H") && (elemento = "hidrogeno")) {
cout << "correcto\n";
}
else if ((alcalinos = "Li") && (elemento = "litio")) {
cout << "correcto\n";
}
else if ((alcalinos = "Na") && (elemento = "sodio")) {
cout << "correcto\n";
}
else if ((alcalinos = "K") && (elemento = "potasio")) {
cout << "correcto\n";
}
else if ((alcalinos = "Rb") && (elemento = "rubidio")) {
cout << "correcto\n";
}
else if ((alcalinos = "Cs") && (elemento = "cesio")) {
cout << "correcto\n";
}
else if ((alcalinos = "Fr") && (elemento = "francio")) {
cout << "correcto\n";
}
else {
cout << "incorrecto\n";
}
int main(); {
void symbols();
}
我一定心情不错,我已经添加评论指出你犯的错误
void symbols() { // no parameter for function
// local declaration of vector instead of parameter
std::vector<std::string> alcalinos = { "H", "Li", "Na", "K", "Rb", "Cs", "Fr" };
std::string elemento;
cout << "Introduce el elemento correspondiente al símbolo(en minusculas) : \n";
int random = rand() % 7;
cout << alcalinos[random];
cin >> elemento;
// compare alcalinos[random] not alcalinos
// use == not =
if ((alcalinos[random] == "H") && (elemento == "hidrogeno")) {
cout << "correcto\n";
}
else if ((alcalinos[random] == "Li") && (elemento == "litio")) {
cout << "correcto\n";
}
else {
cout << "incorrecto\n";
}
} // missing }
int main() { // no ; after main()
srand(time(NULL)); // seed RNG once in main
symbols(); // no void before function call
}
当你犯这么多错误时,这意味着你试图一次编写太多代码。如果您是初学者,请逐字逐句地一次添加一行代码。并在添加的每一行之后测试代码。这样一来,您一次只能尝试解决 一个问题。你会发现这样更容易。