Help with exam question

So thankfully i get multiple submissions on my midterm exam,
We were to right a program where it asked the user for a letter and that letter would be assigned to one of the 50 US states. And Letters that had multiple states ie A you would also be prompted for number and say #2 was alaska and so on for all 50 states, but the problem with my program is that it only picks the "a" states for the numbers 1-4. Any help would be greatly appreaciated

// Course Code: CSC114-651
// Submission: Exam 1
// Due Date: 10/13/20
// Author: Gordon Massey
// Description: Write a program that asks the user to enter a single letter.
// Your job is to match that letter to one of the 50 US states and print out the state name.


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


int main()

{
char letter; // using character to represent the states
int number; // number to let the user choose within states that share the same letter


cout << "Enter the first letter of your state." << endl; //first output by the program
cin >> letter; //users first letter
cout << "Enter A Number" << endl; //program asking user for number
cin >> number; //users selected number

if (letter == 'a' or 'A'){ // states that start with a
if (number == 1){
cout << "Alabama" << endl;
}else{
if (number ==2){
cout << "Alaska" << endl;
}else{
if (number ==3){
cout << "Arizona" << endl;
}else{
if (number ==4){
cout << "Arkansas" << endl;
}else{
if (letter == 'C' or 'c'){ //states that start with c
if (number == 1){
cout << "California" << endl;
}else{
if (number ==2){
cout << "Colorado" << endl;
}else{
if (number ==3){
cout << "Connecticut" << endl;
}else{
if (letter == 'D' or 'd'){ //states that start with d
if (number == 1){
cout << "Delaware" << endl;
}else{
if (letter == 'F' or 'f'){ //states that start with f
if (number == 1){
cout << "Florida" << endl;
}else{
if (letter == 'G' or 'g'){ //states that start with g
if (number == 1){
cout << "Georgia" << endl;
}else{
if (letter == 'H' or 'h'){ //states that start with h
if (number == 1){
cout << "Hawaii" << endl;
}else{
if (letter == 'I' or 'i'){ //states that start with i
if (number == 1){
cout << "Idaho" << endl;
}else{
if (number ==2){
cout << "Indiana" << endl;
}else{
if (number ==3){
cout << "Illinois" << endl;
}else{
if (number ==4){
cout << "Iowa" << endl;
}else{
if (letter == 'K' or 'k'){ //states that start with k
if (number == 1){
cout << "Kansas" << endl;
}else{
if (number ==2){
cout << "Kentucky" << endl;
}else{
if (letter == 'L' or 'l'){ //states that start with L
if (number == 1){
cout << "Louisiana" << endl;
}else{
if (letter == 'M' or 'm'){ //states that start with M
if (number == 1){
cout << "Maryland" << endl;
}else{
if (number ==2){
cout << "Massachusetts" << endl;
}else{
if (number ==3){
cout << "Michigan" << endl;
}else{
if (number ==4){
cout << "Minnesota" << endl;
}else{
if (number ==5){
cout << "Misissippi" << endl;
}else{
if (number ==6){
cout << "Missouri" << endl;
}else{
if (number ==7){
cout << "Montana" << endl;
}else{
if (letter == 'N' or 'n'){ //states that start with n
if (number == 1){
cout << "Nebraska" << endl;
}else{
if (number ==2){
cout << "Neveda" << endl;
}else{
if (number ==3){
cout << "New Hampshire" << endl;
}else{
if (number ==4){
cout << "New Jersey" << endl;
}else{
if (number ==5){
cout << "New Mexico" << endl;
}else{
if (number ==6){
cout << "New York" << endl;
}else{
if (number ==7){
cout << "North Carolina" << endl;
}else{
if (number ==8){
cout << "North Dakota" << endl;
}else{
if (letter == 'O' or 'o'){ //states that start with o
if (number == 1){
cout << "Ohio" << endl;
}else{
if (number ==2){
cout << "Oklahoma" << endl;
}else{
if (number ==3){
cout << "Oregon" << endl;
}else{
if (letter == 'P' or 'p'){ //states that start with p
if (number == 1){
cout << "Pennsylvania" << endl;
}else{
if (letter == 'R' or 'r'){ //states that start with r
if (number == 1){
cout << "Rhode Island" << endl;
}else{
if (letter == 'S' or 's'){ //states that start with s
if (number == 1){
cout << "South Carolina" << endl;
}else{
if (number ==2){
cout << "South Dakota" << endl;
}else{
if (letter == 'T' or 't'){ //states that start with t
if (number == 1){
cout << "Tennessee" << endl;
}else{
if (number ==2){
cout << "Texas" << endl;
}else{
if (letter == 'U' or 'u'){ //states that start with u
if (number == 1){
cout << "Utah" << endl;
}else{
if (letter == 'V' or 'v'){ //states that start with v
if (number == 1){
cout << "Vermont" << endl;
}else{
if (number ==2){
cout << "Virginia" << endl;
}else{
if (letter == 'W' or 'w'){ //states that start with w
if (number == 1){
cout << "Washington" << endl;
}else{
if (number ==2){
cout << "West Virginia" << endl;
}else{
if (number ==3){
cout << "Wisconsin" << endl;
}else{
if (number ==4){
cout << "Wyoming" << endl;

return 0;
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}







Systematic indentation can be a powerful tool to show logic in code.
Your code looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (letter == 'a' or 'A'){ // states that start with a
    if (number == 1){
        cout << "Alabama" << endl;
    }else{
        if (number ==2){
            cout << "Alaska" << endl;
        }else{
            if (number ==3){
                cout << "Arizona" << endl;
            }else{
                if (number ==4){
                    cout << "Arkansas" << endl;
                }else{
                    if (letter == 'C' or 'c'){ //states that start with c
                        if (number == 1){
                            cout << "California" << endl;
                        }else{

However, should it be more like:
IF A
      handle numbers for A
ELSE IF C
      handle numbers for C
ELSE IF D
while the code is a mess, the problem is easy:
you expect the compiler to read human logic statements. It cannot.
'a' is true to the compiler.
if (blah == 'A' || 'a') is true. because 'a' is true.
what you want to say is
if(blah == 'A' || blah == 'a')
which is a totally different statement. This problem is on nearly every if, however..

consider
blah = toupper(blah); //now you only need to check 'A' because 'a' would be converted.
whether you toupper or fix it as is, notepad++ and similar editors with a macro would fix this in 10 seconds. eg find 'or' erase until the ')' would work if you used toupper, while inserting the blah == in there would work if you do it the hard way. May also work to replace "or" with "or letter ==" for all instances.

also you can very likely make this code much smaller, but I will leave you to it from here -- you may not have enough tools to do better yet.

you also misspelled nevada.

finally, I am paranoid and would never put my name on the web like that. Think about it and make your own decisions.
Last edited on
@OP, yes it's a good idea to go back and edit your post and remove the personal (and course) info.

You need to use the advice you have already been given and learn about arrays, strings and loops. ( The first character in a string str is str[0] )

Here's a framework that might be useful in showing you how (one way) of tackling this problem. There is quite a difference. It makes the computer do most of the work without your repetitive code. But, you get 1/10 just for the monumental effort.

You could have written a few lines and then tested/debugged them before writing the long slab of repetitive errors despite the plan not being the best. At least you wouldn't have spent so much time without much benefit.

You'll have to do the second part - input the index number, and the state in the array at that index number is the one you want.

Don't forget to find out from the tutorials and reference material here how toupper() works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;

int main()
{
    // SETUP ALL THE NAMES
    std::string states[]
    {
        "Alabama","Alaska","Nevada","New Hampshire","New Jersey",
        "New Mexico","New York","West Virginia","Wisconsin","Wyoming"
    };
    
    // CALCULATE HOW MANY STATES
    int no_of_states = sizeof(states)/sizeof(string);
    cout << no_of_states << '\n';
    
    // ENTER THE SEARCH CHARACTER
    char initial;
    cout << "Please enter initial letter of state: ";
    cin >> initial;
    
    // FIND THE STATES
    for(int i = 0; i < no_of_states; i++)
    {
        if(states[i][0] == initial)
            cout << states[i] << '\n';
    }
    
    return 0;
}



10
Please enter initial letter of state: W
West Virginia
Wisconsin
Wyoming
Program ended with exit code: 0


https://www.cplusplus.com/doc/tutorial/arrays/
@OP,
what are you allowed to use?
A simple solution would be to use std::multimap
http://www.cplusplus.com/reference/map/multimap/multimap/
Registered users can post here. Sign in or register to post.