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;
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.
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?
unsignedshort cost(string treat){
while(!found){
for (unsignedshort i = 0; i < num; i++)
if (treatments[i] == treat){
return prices[i];
found = true;
}
}
}
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.
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
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