My loop will not exit propery


So I have switch case loop where I must use 10 and 99. 10 is one of my cases and 99 is how you exit the loop. However when the user enters 10 or 99 it reads 1 or 9 and goes to cases 1 or 9 instead.

This is my main function

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
int main()
{
  // Use these variables to test.
  // SIZE could change so make sure your code works with different sizes.
  const int SIZE = 80;
  char ca[SIZE];
  char * pc = ca;
  int fPrints = 0;
  int bPrints = 0;
  int lengthChecks = 0;
  int length = 0;
  // Your code below
  // =========================
  
 char selection = 'z';
       while(selection != '99')
       {
         std::cout << "[1] Test ReadString" << std::endl;
         std::cout << "[2] Test GetStringLength" << std::endl;
         std::cout << "[3] Test PrintString" << std::endl;
         std::cout << "[4] Test PrintStringBackwards" << std::endl;
         std::cout << "[5] Test FindIndexOfCharacter" << std::endl;
         std::cout << "[6] Test GetValueAtIndex" << std::endl;
		 std::cout << "[7] Test MakeUpperCase" << std::endl;
		 std::cout << "[8] Test MakeLowerCase" << std::endl;
		 std::cout << "[9] Test GetIntegerValueOfIndex" << std::endl;
		 std::cout << "[10] Test PrintHexValueAtIndex" << std::endl;
         std::cout << "[99] Quit" << std::endl;
         std::cout << "Selection: ";
         std::cin >> selection;
         std::cin.ignore();
         std::cout << std::endl;

         switch(selection)
          {
           case '1':
                ReadString(pc, SIZE);
                break;
           case '2':
			   
               {
             lengthChecks += 1;
			 length += 1;
			 GetStringLength(ca,&lengthChecks);
             std::cout << "Length[" << length << "]=" << lengthChecks << "\n" << std::endl;
			 
             break;
           }
          
         case '3':
           {
              fPrints +=1;
                std::cout << "Forward["<<fPrints<<"]=";
                PrintString(pc);
                std::cout << std::endl;
                break;
          }
		 case '4':
                bPrints += 1;
                std::cout << "Backwards[" << bPrints << "]=";
                PrintStringBackwards(pc);
                std::cout << std::endl;
                break;
         case '5':
           {
			   char value;
			   int index=0;
			   
			   std :: cout << "[";
			   PrintString(pc);
			   std :: cout << "]" << std :: endl;
			   std::cout << "Get index of what? :";
			   std :: cin >> value;
			   index = FindIndexOfCharacter(pc, value);
			   std::cout << " Location of [" << value << "] = "; 
			   std :: cout << index << std :: endl;
             break;
           }
        case '6':
          {
			  
            int index = -1;
            char newChar = ' ';
            char * cPtr;
             std::cout << "What index?: ";
             std::cin >> index;
             std::cout << "New char?: ";
             std::cin >> newChar;
             cPtr = GetValueAtIndex(pc, index);
             *cPtr = newChar;
            break;
           }
		case '7':
			{
				std::cout << "\n";
				std::cout << "Testing function MakeUpperCase()...\n"
					<< "=============================\n";
				MakeUpperCase(pc);
				std::cout << std::endl;
				break;
			}
		case '8':
			{
				
				std::cout << "\n";
				std::cout << "Testing function MakeLowerCase()...\n"
					<< "=============================\n";
				MakeLowerCase(pc);
				std::cout << std::endl;
				break;
			}
		case '9':
			{
				lengthChecks += 1;
				int i;
				GetStringLength(ca,&lengthChecks);
				std :: cout << "What Index (0-" << lengthChecks << ")?:";
				std :: cin >> i;
				std::cout << "Integer Value of "<< pc[i] << " is " << GetIntegerValueOfIndex(pc, i) << "\n\n";
			break;
			}
		case '10':
			{
				lengthChecks += 1;
				int i;
				GetStringLength(ca,&lengthChecks);
				std :: cout << "What Index (0-" << lengthChecks << ")?:";
				std :: cin >> i;
				std :: cout << "\n";
				std::cout << "Integer Value of "<< pc[i] << " is " << PrintHexValueAtIndex << "\n\n";
			break;
		 }
        case '99':
			std :: cout << "\nGoodbye\n";
           break;
		   break;
         default:
			 std :: cout << " Invalid Selection \n\n";
           break;
         }
      }
  
  // =========================
  // Your code above

  std::cout << "Press ENTER";
  std::cin.get();
  return 0;
}



Last edited on
hi,

make selection an int then remove the single quotes around the cases.

case 99:

A char only hold 1 char.

Cheers
Last edited on
oh thank you
Topic archived. No new replies allowed.