Home > Blockchain >  C , an if loop for an assignment
C , an if loop for an assignment

Time:11-08

I need some help trying to figure out a way to make an if statement taking the users input after a category is typed in. If the user types in string categories[0] in the cin statement, which in this case is "Number of drivers involved in fatal collisions per billion miles." I want it to display the first sentence from the worststate[0] array, which in this case will be North Dakota and South Carolina. I need help doing that for every category, to match up with the state listed, it goes in order.

#include <iostream>
using namespace std;


string worstState[7] = {
        {"North Dakota and South Carolina"},
        {"Hawaii"},
        {"Montana"},
        {"District of Columbia"},
        {"District of Columbia and Mississippi"},
        {"New Jersey"},
        {"Louisiana"},
};

void displayIntro(){
    cout << "Welcome to the Car Accident Statistic Website" << endl;
    cout << "Take a look at the following categories: " << endl;
}


 string categories[7] = {
        {"Number of drivers involved in fatal collisions per billion miles"},
        {"Percentage of drivers involved in fatal collisions who were speeding"},
        {"Percentage of drivers involved in fatal collisions who were Alcohol-Impaired"},
        {"Percentage of drivers involved in fatal collisions who were not distracted"},
        {"Percentage of drivers involved in fatal collisions who had not been involved in any previous accidents"},
        {"Car Insurance Premiums"},
        {"Losses incurred by insurance companies for collisions per insured driver"},
};

int main(){

string Input;

    displayIntro();
    cout << categories[0] << endl;
    cout << categories[1] << endl;
    cout << categories[2] << endl;
    cout << categories[3] << endl;
    cout << categories[4] << endl;
    cout << categories[5] << endl;
    cout << categories[6] << endl;

    cout << "Enter a category: ";
    cin >> Input;

    if (Input == categories....

    return 0;
}

Obviously the code isn't the best organized, which I'm still gonna work on, but I just want the user to type in the category string, and for the state to match up with that specific category in the categories array, all in order based on the way they are entered.

Wondering what the best way to go about this situation is

CodePudding user response:

You can use an unordered_map (aka hash table):

Initialize the map at the start of your program that maps all the questions to their respective answers (i.e. categories[i] to worstState[i]).

unordered_map<string, string> questionsToAnswers;

for (size_t i = 0; i < 7; i  ) {
    questionsToAnswers[categories[i]] = worstState[i];
}

When the user types a string in, you can look up the answer like this:

string s;
cin >> s;
auto itor = questionsToAnsers.find(s);
if (itor != questionsToAnswers.end()) {
    cout << itor->second;
} else {
   // question not found in map
}

CodePudding user response:

If I understand correctly if the user inputs in categories[0], you want to display worstState[0], if the user inputs in catergories[1], you want to display worstState[1], and so on. Now it looks like your input is fixed which means you can use a switch statement, however because there are a fairly large number of courses you want to consider a for loop probably would look more clean. So something like:

for(int i = 0; i < categories.len(); i  ){
    if(Input == categories[i]){
        cout << worstState[i] << endl;
    }
}
  • Related