Vadamadafaka Vadamadafaka - 1 year ago 66
C++ Question

how to check if the input value matches any value within an array

how am i going to do this with if..else, do..while?

this program should prompt the user to input college name and output the ranking of the college, and if the user input an incorrect name, the program should output a message that an incorrect name has been entered.

#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] This is the output what i want to come up with

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

Answer Source

I would go a little more complex way and use a map for holding the data and for getting the college from the command line std::getline, because it detects spaces and puts them in the string. After reading the name it iterates over the already created map to find the college we are looking for. It then checks the iterator if it was found or not! You should check some C++ Tutorials or a good book to learn the basics of C++.

(this approach is using C++11 standard, so be sure your compiler is compatible)

#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;
            std::string name;

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

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

            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;
                    std::cout << "No college found!, try again!" << std::endl;

        return 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download