What am I doing wrong?

Oct 17, 2011 at 12:20am
.,,
Last edited on Nov 1, 2011 at 12:41am
Oct 17, 2011 at 5:08am
use 'toupper' on abrev before your if statements.

http://www.cplusplus.com/reference/clibrary/cctype/toupper/
Oct 17, 2011 at 5:14am
There is one other thing I noticed. Your program is always going to output state, even if you enter an invalid abbreviation.
Oct 17, 2011 at 1:51pm
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]);
}


Oct 17, 2011 at 3:13pm
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.
Oct 18, 2011 at 12:44am
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.
Oct 18, 2011 at 12:52am
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
Oct 18, 2011 at 11:28pm
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 Oct 18, 2011 at 11:59pm
Topic archived. No new replies allowed.