uppercase = lowercase

hi, im just wondering when i type in a letter, how do i make it so that it will recognize that the upper case letter is the same as the lowercase of it.
i.e.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
int main()
{
char letter;
cout << "A or a = apple/nB or b = banana\nand so on...";
cin>> letter;
cout << "You enter " << letter << " and that letter stand for blah blah blah" << endl;
return 0;
}


so in the output, it will recognize that lowercase a will equal to uppercase A
Last edited on
I'm only a newbie, but this is what I do. The code below makes the upper case entered into a lower case.
And then the opposite is also shown using toupper.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;


int main()
{
char letter;
cout << "A or a = apple/nB or b = banana\n";
cin>> letter;
letter = tolower (letter);
cout << "You enter " << letter << " and that letter stand for blah blah blah" << endl;


cout << "A or a = apple/nB or b = banana\n";
cin>> letter;
letter = toupper (letter);
cout << "You enter " << letter << " and that letter stand for blah blah blah" << endl;

cin.ignore().get();
return 0;
}

closed account (Lv0f92yv)
The method above is one way to do it, though I think for simple equality checks, a simple if statement should suffice:

1
2
if ( letter == 'a' || letter == 'A' )
...


Also, it may be helpful to remember uppercase and lowercase letters in ascii are always exactly 32 decimal values apart:

http://www.asciitable.com/ for reference (man ascii) if on linux

a = 97
A = 97-32=65

b = 98
B = 98-32 = 66

etc

so the following would also work:

if ( letter == 97 || letter == 65 ) //if letter is an 'a' or 'A'

Cheers.
i see. well i tried that and it works. but that is using an if statement. i tried using it in a switch and case statement, and it doesn't seem to works. i seem to get the "error C2196: case value '1' already used" error when using it

i.e.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main ()
{
    char letter;
    cout << "A or a = apple/nB or b = banana\nand so on..."<< endl;
    cout << "Please enter a character code for the item: ";
    cin >> letter;
    switch (letter)
{
    case 'a' || 'A':
    cout << "You enter " << letter << " and that letter stand for blah blah blah" << endl;
    break;
    case 'b' || 'B':
    cout << "You enter " << letter << " and that letter stand for blah blah blah" << endl;
    break;
}
return 0;
}


any ideas on how do i make it so that it will work in a switch statement and not in a if statement? and even if i don't use case 'a' || 'A': the program will only recognize that i only used a specific letter for that certain case. so using case 'a' || 'A': the program seem to end without saying the cout in case 'a' or 'A'
Last edited on
You're misunderstanding the or operator.

|| compares the values on the left and right of it. If either are nonzero, the result is 1 (true), otherwise the result is 0 (false).

'a' and 'A' are both nonzero.

So:

1
2
3
4
5
6
case 'a' || 'A':  //  the first thing this does it look at ||

  'a' || 'A'   // <-  this evaluates to 1 (both 'a' and 'A' are nonzero)

// therefore you're left with this:
case 1:


your case 'b' || 'B' does the same thing and therefore is another case 1:, which is why you're getting that error.


The solution here is to create multiple cases:

1
2
3
4
case 'a':
case 'A':
  // stuff here
  break;
Last edited on
closed account (Lv0f92yv)
To elaborate a bit on the case examples used above:

Disch's technique involves 'fall-through' - the behavior that every case statement that is true for the switch will be executed until a 'break' is hit. If either case 'a or case 'A' is the case, the //stuff here will be executed.
here is an example of the code i made:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
using namespace std;
int main ()
{
    char code,respone;
    cout << "Car Code Letters\n";
    cout << "s or S = Suv\n"
            "v or V = Van\n"
            "t or T = Truck\n" << endl;
    cout << "Please enter a character code for the car: ";
    cin >> code;
    switch (code)
    {
    case 's':
    case 'S':
       cout << "";
       break;
    case 'v':
    case 'V':
       cout << "";
       break;
    case 't':
    case 'T':
       cout << "";
       break;
    default:  cout << "Invalid data\n";
    system ("pause");
    return 0;
    }

    cout << "Is it a new car? (Enter Y or y for Yes and N or n for No) ";
    cin >> respone;
    switch (respone)
    {
    case 'y':
    case 'Y':
           switch (code)
            {
            case 's':
            case 'S':
            cout << "A new SUV will costs $200 per month.\n";        
            break;
            case 'v':
            case 'V':
            cout << "A new Van will costs $150 per month.\n";      
            break;
            case 't':
            case 'T':
            cout << "A new Truck will costs $100 per month.\n";       
            break;
            }
    case 'n':
    case 'N':
        switch (code)
        {
        case 's':
        case 'S':
        cout << "An old SUV will costs  $100 per month.\n";      
        break;
        case 'v':
        case 'V':
        cout << "An old Van will costs $75 per month.\n";     
        break;
        case 't':
        case 'T':
        cout << "An old Truck will costs $50 per month.\n";    
        break;
        }
    default:  cout << "Invalid data\n";
    }
 system ("pause");
 return 0;
}


in these codes, the 2nd default: cout << "Invalid data\n"; line 69, show up, regardless of the case statement, but the first one doesn't. From what i understand, default statement should only show up if the case statement does not fit the requirement.
another problem is when it asked me to enter a letter twice, "t,T,s,S,v or V" and "n or N", the output will show
i.e.
An old Truck will costs $50 per month.
Invalid data


but if i input "t,T,s,S,v or V"" and "Y or y"
the output will be like this:

A new Truck will costs $100 per month.
An old Truck will costs $50 per month.
Invalid data


so my question is,
1. how come the 2nd default statement show up when the case is present
2. how come case 'y': case 'Y': show up with case 'n': case 'N': but if case 'n': case 'N': is used, only case 'n': case 'N': will be shown even though it was a switch in a switch statement. i even followed the example in this thread: http://www.cplusplus.com/forum/beginner/8116/ and it works, but from adding in my stuffs, it doesnt. what exactly is wrong?
closed account (Lv0f92yv)
First, check your placement of that last default clause. It is outside of the switch I think you intended it for, causing it to be executed when you don't want it to be.

In general, your switch statements are somewhat complex. I might suggest a design change, and create a separate method to handle different case switch statements. You could do something like:

getCar(); //ask for and figure out what car they want

case 's':
case 'S':
determineAge( 'S' );
..etc handle other car cases

the determineAge function will ask user if it's new or old, and depending on the answer to that (using a switch), can call a third function with both parameters: calcCost( 'S', 'N' ) where S is what type of car it is and N is new or bool value to determine if the car is new or not.

This third function would then switch on the parameters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void calcCost( char carType, bool isNew )
{
	if ( isNew )
	{
		switch ( carType )
		{
		case 's':
		case 'S':
			cout << "A new SUV will cost...\n";
			//... //other types of cars
		}
	}
	else
	{
		switch ( carType )
		{
		case 's':
		case 'S':
			cout << "An old SUV will cost...\n";
			//... //other types of cars
		}
	}
}


Something like that would be an alternative... Let me know if that is more difficult to follow.

Also, try to get away from using system("anything"); in your programs. This is depenant on the OS you are using (not portable), and is actually quite resource intensive (calls that involve OS level functionality to operate usually are). There are many more reasons why not to do this, Duoas made a good post about this in another thread if you want to search.

Instead, try using getchar(); to tell your program to wait for another character to be sent to the input stream.

Last edited on
I should had mentioned that I'm require to use a switch statement to determine car fee. using the void methods, I ended up going back to using switch in switch statement and end up with the same switch in a switch problems.
I'll tried using your method again and let you know if I actually work something out.
closed account (Lv0f92yv)
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
38
39
40
41
42
cout << "Type of car?\n";
	cin >> letter;
	cout << "is it new?\n";
	cin >> newcar;

	switch ( newcar )
	{
	case 'y':
	case 'Y':
		switch ( letter )
		{
		case 's':
		case 'S':
			cout << "A new SUV will cost...\n";
			break;
		case 't':
		case 'T':
			cout << "A new truck costs...\n";
			break;
		default:
			cout << "Invalid car code\n";
		}
		break;
	case 'n':
	case 'N':
		switch ( letter )
		{
		case 's':
		case 'S':
			cout << "An old SUV will cost...\n";
			break;
		case 't':
		case 'T':
			cout << "An old truck costs...\n";
			break;
		default:
			cout << "Invalid car code\n";
		}
		break;
	default:
		cout << "Invalid age code\n";
	}


The key here is the placement of the break statements after the inner switches. This prevents fall-through from happening after one of the cases is hit.

My solution requires both the type and age of the car be entered before any validity checking happens. You can easily rewrite it to take input before moving on to the switch statements.
comparing your model and my switch in switch model, the only problem that explained why my program was running more than 2 lines was a simple missing "break;" after line 51. well this solved everything. THANKS
Topic archived. No new replies allowed.