2016-08-01 101 views
-2

我该怎么做if..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; 
} 

[示例]这是我想拿出

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

这个'for(if)'究竟是什么? – DimChtz

+0

['std :: find'](http://en.cppreference.com/w/cpp/algorithm/find)? – NathanOliver

+0

,因为我不知道我要使用if..else还是做什么,我输入'for(if)'@DimChtz – Vadamadafaka

回答

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

可以使用地图,以节省您的行列,这将在后面帮你按名称访问您的行列真的很容易。 您还可以保留一个布尔变量,指示当前用户输入是否存在于您的列表中,并用它来决定用户是否应该得到他/她的答案,或者再次提示输入大学名称。

下面是展示一个例子主要功能:

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

bool可变has确定由用户输入的大学名称是否阵列college[]或不存在。如果找到匹配项,则has的值更改为true

相关问题