PLEASE HELP!!!!

i'm back again with another stupid question of mine...
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
 do
{
switch(no2)
{
case 1 :     std::cout<<"\n MANAGING DIRECTOR(Salary=800000)";
                   ba_sal=800000;
                   break;
 case 2 :     std::cout<<"\n DIRECTOR (Salary=500000)";
                   ba_sal=500000;
                   break;
  :
  :
  :
 case 10:     std::cout<<"\n SUB-STAFF (15000<Salary<25000)";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<15000||ba_sal>25000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
 default:    std::cout<<"Please Check Number.";
                ba_sal=0;
                break;
    }
    }while(ba_sal==0);

this generates an infinite loop.. is it because i'm assigning the value 0 to ba_sal? please help me out here :/
Last edited on
In cases like this... it's REALLY easy to just step through in a debugger and see why your code is infinitely looping.

I outline the basics here:
http://www.cplusplus.com/forum/beginner/75304/#msg403990

Basically... with a debugger you can step through execution of the program line by line to see exactly what it's doing and when. You can also examine the contents of all variables while you're doing this.


In your case... it looks like if you hit the default label you will infinitely loop because you set ba_sal to zero and never give it an opportunity to change to anything else.


Learning debugger basics should really be taught in like in the first week of programming.
Last edited on
it creates an infinite loop even if i remove line 24. :/
and i didn't really understand the debugger.. just when it enters line 26, it starts running an infinite loop in the case of the default label..
You're not gathering input for your default switch case.
If "no2" doesn't match any of the other cases, you're setting "ba_sal" to the value of 0 and breaking out of the switch statement -- not the do while () loop.

When your switch statement checks "no2" again, it's going to hold the same non-case-matching value and loop infinitely.
what do you suggest i do then? because if the user inputs some wrong value, i need the output of
1
2
std::cout<<"\n 1. Managing Director \n 2. Director \n 3. General Manager \n 4. Deputy General Manager \n 5. Chief Manager \n 6. Senior Executive \n 7. Executive \n 8. Officer \n 9. Clerk \n 10. Sub-Staff \n Enter designation number : ";
    std::cin>>no2;
to be displayed..
http://www.cplusplus.com/doc/tutorial/

You need to go through the tutorial, that's all there is to it.
i got the mistake :P
Topic archived. No new replies allowed.