【问题标题】:how to check if the input value matches any value within an array如何检查输入值是否与数组中的任何值匹配
【发布时间】:2016-08-01 14:30:55
【问题描述】:

如果..else,do..while,我将如何做到这一点?

此程序应提示用户输入学院名称并输出学院排名,如果用户输入的名称不正确,程序应输出输入错误名称的消息。

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

[sample] 这是我想要的输出

Enter the name of a Big Twelve College: Nebraska
Nebraska's ranking is 3

【问题讨论】:

  • 这个for (if)到底是什么?
  • 因为我不确定我会使用 if..else 还是 do while,所以我输入了for (if)@DimChtz

标签: c++ arrays c++11 g++


【解决方案1】:

我会采用更复杂的方式,使用地图来保存数据并从命令行 std::getline 获取大学,因为它会检测空格并将它们放入字符串中。读取名称后,它会遍历已创建的地图以找到我们正在寻找的学院。然后它检查迭代器是否找到!您应该查看一些 C++ 教程或一本好书来学习 C++ 的基础知识。

(此方法使用 C++11 标准,因此请确保您的编译器兼容)

#include <iostream>
#include <string>
#include <map>

int main()
{
        std::map<std::string, int> colleges = {
            {"Baylor", 12},
            {"Colorado", 11},
            {"Iowa State", 10},
            {"Kansas", 9},
            {"Kansas State", 5},
            {"Missouri", 8},
            {"Nebraska", 3},
            {"Oklahoma", 2},
            {"Oklahoma State", 7},
            {"Texas", 1},
            {"Texas A&M", 6},
            {"Texas Tech", 4}
        };

        std::cout << "to end enter: exit";

        bool p = true;
        while(p){
            std::string name;

            std::cout << "Enter the name of a Big Twelve College: ";
            std::getline(std::cin, name);

            if(name == "exit"){
                    p = false;
                    break;
            }

            std::map<std::string, int>::iterator it = colleges.find(name);
            if (it != colleges.end()){
                    std::cout << it->first << "'s ranking is " << it->second << "!" << std::endl;
            }else{
                    std::cout << "No college found!, try again!" << std::endl;
            }
        }

        return 0;
}

【讨论】:

    【解决方案2】:

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

    这里是一个示例 main 函数,演示:

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

    【讨论】:

      【解决方案3】:

      您可以简单地使用您拥有的数组来完成此操作,而无需使用地图(如果您愿意的话)。考虑下面的代码。

      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判断用户输入的大学名称是否存在于数组college[]中。如果找到匹配项,则将 has 的值更改为 true

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-31
        • 2017-07-31
        • 2018-06-27
        • 1970-01-01
        • 2019-08-16
        • 1970-01-01
        相关资源
        最近更新 更多