Switch

I there a limit to cases in a switch? my number 10 does nothing, all other numbers work perfectly fine. Please help...

1
2
3
4
5
6
7
8
9
10
11
12
13
  switch (treatmentChoice){
            case 1: treatment = "Cut"; break;
            case 2: treatment = "Colour"; break;
            case 3: treatment = "Masking"; break;
            case 4: treatment = "Blow Dry"; break;
            case 5: treatment = "Highlights - Full Head"; break;
            case 6: treatment = "Highlights - Half Head"; break;
            case 7: treatment = "Highlights - T-Bar"; break;
            case 8: treatment = "Balayage"; break;
            case 9: treatment = "Kid's Cut"; break;
            case 10: treatment = "Upstyle"; break;
        }
        cout << "\n\tCost of this treatment is: " << cost(treatment);
closed account (2LzbRXSz)
Works just fine for me? I filled in the blanks for myself, but I'm running it in main (is yours a separate function? If you don't mind, could you please post your full code?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    int treatmentChoice;
    string treatment;
    
    cin >> treatmentChoice;
    cin.ignore();
    
    switch (treatmentChoice){
        case 1: treatment = "Cut"; break;
        case 2: treatment = "Colour"; break;
        case 3: treatment = "Masking"; break;
        case 4: treatment = "Blow Dry"; break;
        case 5: treatment = "Highlights - Full Head"; break;
        case 6: treatment = "Highlights - Half Head"; break;
        case 7: treatment = "Highlights - T-Bar"; break;
        case 8: treatment = "Balayage"; break;
        case 9: treatment = "Kid's Cut"; break;
        case 10: treatment = "Upstyle"; break;
    }
    cout << treatment;


(The console outputs
Upstyle
, just like it's supposed to)
Last edited on
it is a separate function, the whole code is very long. cost(treatment)
is another function searching 2 parallel arrays. it works for every other option, except 10. I checked spelling like 100 times.
probably it depends upon cost() function.
cost() function works just fine for options 1-9
ok.
The problem must be elsewhere in the code. How do you assign 10 to treatmentChoice? Are you sure treatmentChoice has the value 10 right before the switch?
Last edited on
I get the number from users input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
unsigned short treatmentChoice;
 cout << "\n\tChose treatment:\n\t1 - Cut\n\t2 - Colour\n\t3 - Masking\n\t4 - Blow Dry\n\t5 - Highlights - Full Head"
             << "\n\t6 - Highlights - Half Head\n\t7 - Highlights - T Bar\n\t8 - Balayage\n\t9 - Kid's Cut\n\t10 - Upstyle\n"
             << "\tChoice: ";
        cin >> treatmentChoice;
        cin.ignore();
        switch (treatmentChoice){
            case 1: treatment = "Cut"; break;
            case 2: treatment = "Colour"; break;
            case 3: treatment = "Masking"; break;
            case 4: treatment = "BlowDry"; break;
            case 5: treatment = "Highlights - Full Head"; break;
            case 6: treatment = "Highlights - Half Head"; break;
            case 7: treatment = "Highlights - T-Bar"; break;
            case 8: treatment = "Balayage"; break;
            case 9: treatment = "Kid's Cut"; break;
            case 10: treatment = "Upstyle";break;
        }
        cout << "\n\tCost of this treatment is: " << cost(treatment)
             << "\n\tIs that ok?\n"
             << "\tY/N: ";

Works fine for me... Can you show us the cost function?
closed account (2LzbRXSz)
I ran the code and it works fine. Looks like the error is probably in the cost function.
cost() works ok with any case between 1 - 9

1
2
3
4
5
6
7
8
9
unsigned short cost(string treat){
    while(!found){
        for (unsigned short i = 0; i < num; i++)
            if (treatments[i] == treat){
                return prices[i];
                found = true;
                }
    }
}


cost() works ok with any case between 1 - 9

Trust me, we know.

Are you sure treatments[i] contains the string "Upstyle" ?
What is num?

I would guess that your array is large enough to hold 10 elements, right? Since an array starts with 0, only indexes from 0 to 9 are valid. Index 10 is out of bounds.

By the way: found will never be true due to the return on line 5.
constant num is a number of components.
just to test I did
cout << cost("Cut") << cost("Blow Dry")
and i got price for both
but when I did this
cout << cost("Cut") << cost("Blow Dry") << cost("Upstyle");
nothing happened.
I am sure "Upstyle" is there. I get 2 arrays streamed from text file from my teacher.
1
2
3
4
   if (treatments[i] == treat){
                return prices[i];
                found = true;
                }


To elaborate on what @coder777 said. found will never be true. Because as soon as that if statement is true, then it will return the price and leave the function. So basically it will never go over the found = true; code
Last edited on
I have arrays from my teachers that have number of components declared as numComponenets
and whole bunch of arrays with different number of components that i declared as num
in cost() I should have said for(int i = 0; i < numComponents...
rather than for(int i =0; i < num
thx for asking what num was fg
and thank you all other for looking and trying to solve my problem
Topic archived. No new replies allowed.