tolower() not working correctly
Jun 3, 2015 at 9:03pm UTC
Could someone please fill me in to why my tolower() is not working throughout my program?
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
// Input: Gets the necessary letters from user.
// Processing: Calcultes what color the user is typing in before its completely
// typed out
// Output: Outputs the digit that the color stands for.
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>
using namespace std;
const int RED_DIGIT = 2;
const int ORANGE_DIGIT = 3;
const int YELLOW_DIGIT = 4;
const int VIOLET_DIGIT = 7;
const int WHITE_DIGIT = 9;
const int BROWN_DIGIT = 1;
const int BLACK_DIGIT = 0;
const int BLUE_DIGIT = 6;
const int GRAY_DIGIT = 8;
const int GREEN_DIGIT = 5;
int main()
{
char idColor; // first character
char idColorTwo; // second character
char idColorThree; // third character
//Gets input from user
cout << "Enter the first letter: " ;
cin >> idColor;
cout << endl;
tolower(idColor);
switch (idColor) {
case 'r' :
cout << "Red stands for digit " << RED_DIGIT << "." << endl;
break ;
case 'o' :
cout << "Orange stands for digit " << ORANGE_DIGIT << "." << endl;
break ;
case 'y' :
cout << "Yellow stands for digit " << YELLOW_DIGIT << "." << endl;
break ;
case 'v' :
cout << "Violet stands for digit " << VIOLET_DIGIT << "." << endl;
break ;
case 'w' :
cout << "White stands for digit " << WHITE_DIGIT << "." << endl;
break ;
case 'b' :
cout << "Enter the second letter: " ;
cin >> idColorTwo;
cout << endl;
tolower(idColorTwo);
//nested switch statement
switch (idColorTwo) {
case 'r' :
cout << "Brown stands for digit " << BROWN_DIGIT << "." << endl;
break ;
case 'l' :
cout << "Enter the third letter: " ;
cin >> idColorThree;
cout << endl;
tolower (idColorThree);
//nested switch statement
switch (idColorThree){
case 'u' :
cout << "Blue stands for digit " << BLUE_DIGIT << "." << endl;
break ;
case 'a' :
cout << "Black stands for digit " << BLACK_DIGIT << "." << endl;
break ;
default :
cout << "Unknown Error." << endl;
}
break ;
}
break ;
case 'g' :
cout << "Enter the second letter: " ;
cin >> idColorTwo;
cout << endl;
cout << "Enter the third letter: " ;
cin >> idColorThree;
cout << endl << endl;
tolower(idColorTwo);
tolower(idColorThree);
//nested switch statement
switch (idColorTwo){
case 'r' :
//nested switch statement
switch (idColorThree){
case 'a' :
cout << "Gray stands for digit " << GRAY_DIGIT << "." << endl;
break ;
case 'e' :
cout << "Green stands for digit " << GREEN_DIGIT << "." << endl;
break ;
default :
cout << "Unknown Error." << endl;
}
break ;
}
break ;
default :
cout << "Unknown Error." << endl;
}
cout << endl << endl;
system ("PAUSE" );
return 0;
}
Jun 3, 2015 at 9:08pm UTC
Do I have to do it like this, does tolower() not work within a switch statement?
switch (idColor) {
case 'r':
case 'R':
cout << "Red stands for digit " << RED_DIGIT << "." << endl;
break;
Jun 3, 2015 at 9:13pm UTC
When you use a predefined character function to change the case of the character itself, you should put it in an assignment statement. In your case, I would do the following.
idColor = tolower(idColor);
Jun 3, 2015 at 9:28pm UTC
Awesome, thank you so much!
Jun 3, 2015 at 11:40pm UTC
Or use
switch (tolower(idColor)) {
in which case the value returned by tolower() is passed directly to the switch.
mlholder's suggestion is better if you plan to check idColor multiple times.
Andy
Topic archived. No new replies allowed.