C++ Switch/Loop Problem

Oct 12, 2014 at 12:21am
Hello everybody!
I am in a Beginners C++ class in high school and we have been give this problem:
http://gyazo.com/0d4d9206730e4fb240661a8dec0dbeb4

I'm having a hard time understand this problem. I know that we have to use a switch statement and the program has to hold count the number of times each character was entered. But I need some help. If anyone could assist me that would be great.
Oct 12, 2014 at 2:25am
What exactly do you need help with? It sounds pretty straight forward to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while(letter != '.')
{
    switch(letter)
    {
        case 'R':
            //increment count for retired
            break;
        ...
        case 'A':
            //increment count for active <--this has extra feature also
            //ask for the number of years since employed
            break;
        default:
            //catch any erroneous values here
    }
}

//display table 
Oct 18, 2014 at 10:41pm
Sorry for the late response but I just recently got back this problem. Here's what I did but it's not working right. Any suggestions?

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
#include <iostream>

using namespace std;

int main()
{
   char status;
   int retire;
   int active;
   int former;
   int sick;
   int vacation;
   int years;

   cout << "Enter your employment status:" << endl;
   cin>>status;

   while(status !='.'){
    switch(status){
        case 'R':
            retire++;
            break;
        case 'A':
            active++;
            /*cout<< "How many years have you been employed"<<endl;
            cin>>years;*/
            break;
        case 'F':
            former++;
            break;
        case 'S':
            sick++;
            break;
        case 'V':
            vacation++;
            break;
        default:
            cout<<"Please an appropriate letter"<<endl;

    }


    }

    cout<<"Retired: "<< retire <<endl;
    cout<<"Active: " << active<< endl;
    cout<< "Former: "<< former <<endl;
    cout<<"Sick leave: " << sick <<endl;
    cout<<"On Vacation: " << vacation <<endl;
    return 0;
}
Oct 18, 2014 at 11:31pm
You have created an infinite loop, therefore the boolean expression in your while loop will always be true. You should also look at variable initializations.
Oct 18, 2014 at 11:33pm
Just looking at your variable's initial values, none of them are reset to zero. Either make them global outside of main so that the compiler will zero them or set each one to zero when you declare them.

1
2
3
4
5
6
7
int main()
{
   char status = 0;
   int retire = 0;
   int active = 0;

   ...
Oct 19, 2014 at 12:03am
Well I did what you said venomDev but the program still won't let me enter a letter. I try to input "R" but when I press enter nothing happened. Still don't know what I'm doing wrong
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
int main(){

   char status;
   int retire=0;
   int active=0;
   int former=0;
   int sick=0;
   int vacation=0;
   int years;

   cout << "Enter your employment status:" << endl;
   cin>>status;

   while(status !='.'){
    switch(status){
        case 'R':
            retire++;
            break;
        case 'A':
            active++;
            cout<< "How many years have you been employed"<<endl;
            cin>>years;
            break;
        case 'F':
            former++;
            break;
        case 'S':
            sick++;
            break;
        case 'V':
            vacation++;
            break;
        default:
            cout<<"Please an appropriate letter"<<endl;

    }


    }

    cout<<"Retired: "<< retire <<endl;
    cout<<"Active: " << active<< endl;
    cout<< "Former: "<< former <<endl;
    cout<<"Sick leave: " << sick <<endl;
    cout<<"On Vacation: " << vacation <<endl;



    return 0;
}
Oct 19, 2014 at 12:45am
You still provide no way to break out of the loop. There is no way for the condition to change once the loop starts. It would be better to use a do/while loop, but as it stands you need to ask again for the status after the switch. Also you need to take input from the default part of the switch.
Oct 19, 2014 at 2:46am
Inside your loop read in the status. while(cout << "Enter your employment status: " && cin >> status && status != '.')
Topic archived. No new replies allowed.