C++ 如何检查输入值是否与数组中的任何值匹配

C++ 如何检查输入值是否与数组中的任何值匹配,c++,arrays,c++11,g++,C++,Arrays,C++11,G++,如果……否则,我该怎么做呢 此程序应提示用户输入学院名称并输出学院排名,如果用户输入的名称不正确,程序应输出一条消息,说明输入的名称不正确 #include <iostream> #include <string> using namespace std; int main() { string college[] = {"Baylor", "Colorado", "Iowa State", "Kansas", "

如果……否则,我该怎么做呢

此程序应提示用户输入学院名称并输出学院排名,如果用户输入的名称不正确,程序应输出一条消息,说明输入的名称不正确

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string college[] = {"Baylor", "Colorado", "Iowa State",
                        "Kansas", "Kansas State", "Missouri",
                        "Nebraska", "Oklahoma", "Oklahoma State",
                        "Texas", "Texas A&M", "Texas Tech"};
    int conferenceRanking[] = {12, 11, 10, 9, 5, 8,
                                3, 2, 7, 1, 6, 4};

    for (if)
    {
        cout << "Enter the name of a Big Twelve College: " << college[count]
         << college << "\n's ranking is " << conferenceRanking[count]
         << "\n" << endl;
    }

    return 0;
}

我将使用更复杂的方法,使用一个映射来保存数据,并从命令行std::getline获取学院,因为它检测空格并将它们放入字符串中。读过名字后,它会在已经创建的地图上迭代,以找到我们正在寻找的学院。然后检查迭代器是否找到了它!你应该检查一些C++教程或者一本好书,学习C++的基础知识。 (此方法使用C++11标准,因此请确保编译器兼容)

#包括
#包括
#包括
int main()
{
标准::地图学院={
{“贝勒”,12},
{“科罗拉多”,11},
{“爱荷华州”,10},
{“堪萨斯”,9},
{“堪萨斯州”,5},
{“密苏里”,8},
{“内布拉斯加州”,3},
{“俄克拉荷马”,2},
{“俄克拉荷马州”,7},
{“德克萨斯州”,1},
{“德克萨斯A&M”,6},
{“德克萨斯理工大学”,4}
};

std::cout您可以使用一个映射来保存您的等级,这将帮助您以后通过名称访问您的等级。 您还可以保留一个布尔变量,指示当前用户输入是否存在于您的列表中,并使用它来决定用户是否应该得到他/她的答案,或者再次被提示输入某个学院名称

下面是一个示例主函数,演示了:

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

int main()
{
 std::unordered_map<std::string, int> collegeRanks = {
    {"Baylor",12},
    {"Colorado",11},
    {"Iowa State",10}
};
//etc...

bool finished = false; 
string userInput;

while(!finished){

    cout << endl << "Enter the name of a Big Twelve College: ";
    getline (std::cin,userInput);
    std::unordered_map<std::string,int>::const_iterator nameAndRank = collegeRanks.find (userInput);
    if ( nameAndRank == collegeRanks.end() ){
        // Not found :(
        finished = false;
        cout << endl << userInput << " does not exist in our database... try another college name!" << endl;
        userInput = "";
    }
    else{
        // Found :) 
        finished = true;
        cout << userInput << "'s rank is " << std::to_string(nameAndRank->second) << endl;
    }

}

return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
std::无序映射集合链接={
{“贝勒”,12},
{“科罗拉多”,11},
{“爱荷华州”,10}
};
//等等。。。
bool finished=false;
字符串用户输入;
当(!完成){

CUT

你可以简单地使用没有使用地图的数组(如果你想要的话)。请考虑下面的代码。

bool has = false; 
cout << "Enter college name\n";
string name;
cin >> name;
for(int i=0; i<12; i++){
    if(college[i].compare(name) == 0){
        has = true;
        cout << "Ranking of " << college[i] << " is " << conferenceRanking[i];
    }
}
//check if the name entered by user exists
if(has == false)
cout << "The college name entered does not exist";
bool has=false;
姓名;

for(int i=0;i这究竟是(if)
什么?因为我不确定我将使用什么,无论是for if..else还是do,而我键入了
for(if)
@DimChtz
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

int main()
{
 std::unordered_map<std::string, int> collegeRanks = {
    {"Baylor",12},
    {"Colorado",11},
    {"Iowa State",10}
};
//etc...

bool finished = false; 
string userInput;

while(!finished){

    cout << endl << "Enter the name of a Big Twelve College: ";
    getline (std::cin,userInput);
    std::unordered_map<std::string,int>::const_iterator nameAndRank = collegeRanks.find (userInput);
    if ( nameAndRank == collegeRanks.end() ){
        // Not found :(
        finished = false;
        cout << endl << userInput << " does not exist in our database... try another college name!" << endl;
        userInput = "";
    }
    else{
        // Found :) 
        finished = true;
        cout << userInput << "'s rank is " << std::to_string(nameAndRank->second) << endl;
    }

}

return 0;
}
bool has = false; 
cout << "Enter college name\n";
string name;
cin >> name;
for(int i=0; i<12; i++){
    if(college[i].compare(name) == 0){
        has = true;
        cout << "Ranking of " << college[i] << " is " << conferenceRanking[i];
    }
}
//check if the name entered by user exists
if(has == false)
cout << "The college name entered does not exist";