What am I doing wrong?

.,,
Last edited on
use 'toupper' on abrev before your if statements.

http://www.cplusplus.com/reference/clibrary/cctype/toupper/
There is one other thing I noticed. Your program is always going to output state, even if you enter an invalid abbreviation.
the tolower and toupper return integer and they take integer as argument. you cannot convert whole string abrev at a time. Instead you can do the following

1
2
3
for(int i = 0; abrev[i]!='\0'; i++){
    abrev[i] = toupper(abrev[i]);
}


1. Use Bibek's code to convert string to uppercase.

2. Use a switch statement rather than all those nested if statements.

3. Put the cout << State << endl; inside the switch statement rather than at the end. and Default: cout << "Invalid abbreviation."<<endl;. Otherwise, like Meerkat said it will always ouput State, regardless of whether or not it is valid or invalid.
It was actually Maese that had said that but anyway. I would pretty much agree with stridexr, but depending on how you want it to work, you might want to put most of it within a while loop, so that if there is an invalid abbreviation it takes you back to the start to try another input, if that's the case, you could keep cout << State << endl; at the end, just make sure it's after the while loop. That said, you might not want your program to work like this, in which case, stridexr is spot on.
closed account (zb0S216C)
stridexr wrote:
Use a switch statement rather than all those nested if statements. (sic)

No, only integral types (including char and excluding floating-points types) can be used with the switch.

Wazzak
Formwork wrote:
No, only integral types (including char and excluding floating-points types) can be used with the switch.


My bad, I tend to mix up C++ with C# sometimes. Thanks for pointing that out Formwork :))

Anyway, back to the problem:
You could do it with Maps, but I think the most efficient way to do it in your case (especially if this is a school assignment, which it probably is and you are not allowed to use Maps), is to create two parallel string arrays in which the location of the abbreviation and its state reside in the same location in their respective arrays.

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
33
34
35
36
37
#include<iostream>
#include<string>
using namespace std;

int main()
{
    const int SIZE = 5;
    string abbreviation[SIZE] = {"AK","AZ","NC","NM","IL"};
    string state[SIZE] = {"Alaska", "Arizona", "North Carolina", "New Mexico", "Illinois"};
    string input;
    
    bool found=false;
    
    cout<<"Please enter the following state abbreviations: AK, AZ, NC, NM, or IL: ";
    cin >> input;
    
    for(int i = 0; input[i]!='\0'; i++)
    {
    input[i] = toupper(input[i]);
    }
    
    for(int j=0; j<SIZE; ++j)
    {
            if(abbreviation[j] == input)
            {
                cout<< state[j] << endl;
                found = true;
                break;
                               
            }
            
    }
    if(!found)
    cout<<"Invalid abbreviation."<<endl;
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.