Small Problem with Switch Command

Hi, Im working on a program using a class and im just having a problem with my switch command in my main program. I get an error that says i need a primary expression before int i, such as in:
 
case 'g': p.grow(int i);


But im not quite sure what would go there, this error confuses me. Any help would be very appreciated. Here is my full code for my main program and my class file.

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
#include <cstdlib>
#include <iostream>
#include "person.h"

using namespace std;

int main(int argc, char *argv[])
{
    char command;
    person p;
    
    p.input ();
    
    cout << "\nEnter one of the following: ";
    cout << " e: Enter a persons's statistics";
    cout << " g: Grow by a number of inches";
    cout << " s: Shrink by a number of inches";
    cout << " i: Increase in weight";
    cout << " d: Decrease in weight";
    cout << " p: Print the current statistics";
    cout << " q: Quit the program";
    
    cin >> command;
    
    while (command != 'q')
    {
          switch (command)
          {
                 case 'e': p.input();
                           break;
                 case 'g': p.grow(int i);
                           break;
                 case 's': p.shrink(int i);
                           break;
                 case 'i': p.gain(int w);
                           break;
                 case 'd': p.lose(int w);
                           break;
                 case 'p': p.print();
                           break;
                 default: cout << "You must enter either e, g, s, i, d, p, or q!";
          }
          
    cout << "\nEnter one of the following: ";
    cout << " e: Enter a persons's statistics";
    cout << " g: Grow by a number of inches";
    cout << " s: Shrink by a number of inches";
    cout << " i: Increase in weight";
    cout << " d: Decrease in weight";
    cout << " p: Print the current statistics";
    cout << " q: Quit the program"; 
    
    cin >> command;
    }
    
    cout << "\nThank you!!!"; 
    
    
    system("PAUSE");
    //return EXIT_SUCCESS;
}



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

using namespace std;

class person
{
      private:
         string name;
         int feet;
         int inches;
         int weight;
      public:
         person();
         void input ();
         void grow (int i);
         void shrink (int i);
         void gain (int w);
         void lose (int w);
         void print ();
};


person::person()
{
     name = " Unassigned ";
     feet = 0;
     inches = 0;
     weight = 0;
}


void person::input()
{
     string name;
     int f;
     int i;
     int w;
     
     
     cout << "\nEnter Name: ";
     cin >> name;
     cout << "Enter Height";
     cout << "  Feet: ";
     cin >> f;
     cout << "  Inches: ";
     cin >> i;
     
     if (f < 0 || i < 0 || w < 0)
     {
           cout << "Illegal data entry! You'll need to try again! "; 
           f = 0;
           i = 0;
           w = 0;
     }
}

void person::grow(int i)
{
     if (i < 0)
     {
           cout << "Number of inches to grow must be positive!";
           i = 0;
     }
     else
     {
         inches = inches + 1;
     }

     
}


void person::shrink(int i)
{
     if (i < 0)
     {
           cout << "Number of inches to shrink must be positive!";
           i = 0;
     }
     else
     {
         inches = inches - 1;
     }
    
     
}


void person::gain (int w)
{
     if (w < 0)
     {
           cout << "Number of pounds to gain must be positive!";
           w = 0;
     }
     else
     {
         weight = w + weight;
     }
}

void person::lose (int w)
{
     if (w < 0)
     {
           cout << "Number of pounds to lose must be positive!";
           w = 0;
     }
     else
     {
         weight = weight - w; 
     }    
}


void person::print ()
{
     cout << "Name:   " << name;
     cout << "Height: " << feet << "feet, " << inches << "inches ";
     cout << "Weight: " << weight << "pounds ";
}
where do you entered value for i and w in main()?
And you can't make them in function call.
Recent Code is below.
Last edited on
Also, in line 35 of the header file are you referring to the variable "name" in line 9?

In the shrink and grow functions you increment and decrement by 1
Last edited on
Recent Code Below.
Last edited on
Recent Code Below.
Last edited on
Recent Code Below.
Last edited on
Ok, so I tried to make all the changes you suggested. But now I can not get my math to work. For example, in the grow function, I have inches =inches + i, but when i am prompted to add the amount of inches to grow by in my program, the result will be off by a hundred or so. My current inches could be 5, I would add 1, and it would go up to 50..... now I'm really confused
Last edited on
Recent Code is below.

The math you wanted is included.
Last edited on
Recent Code is below
...
Last edited on
Just some greater/less than observations:

1
2
3
4
5
6
if (inches>12)
{
	feet+=inches/12;
	inches=inches%12;

}


This needs to be >= since a foot is 12 inches.

I guess the next things are up to you. When you make your person (p.input), you allow for a person to be 0 tall, does that person exist? I would allow for zero values when growing/shinking/gain/lose. If I were using this program to keep track of my height/weight, there would be a lot of times where I grow zero inches. Just ideas.


Last edited on
Yes you are right i left that off.

i will fix it in the code below.
Last edited on
Code works give it a try.

person.h

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185


#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

class person
{
      private:
         string name;
         int feet;
         int inches;
         int weight;
      public:
         person();
         void input ();
         void grow (int i);
         void shrink (int i);
         void gain (int w);
         void lose (int w);
         void print ();
};


person::person()
{
     name = " Unassigned ";
     feet = 0;
     inches = 0;
     weight = 0;
}


void person::input()
{
     string named;
     int f=0;
     int i=0;
     int w=0;
     while(f <=0 || i <= 0 || w <= 0)
	 {
     //corrected
                    cout << "\nEnter Name: ";
                    cin.clear();
                    cin.sync();
                    getline(cin,named);
                    cout << "Enter Height";
                    cout <<endl<< "Feet: ";
                    cin >> f;
                    cout << "Inches: ";
                    cin >> i;
                    cout << "Enter  Weight: ";
                    cin >>w;

                    if (f <=0 || i <= 0 || w <= 0)
                    {
	               	 if (f>0)
	               	 {
			                break;
	               	 }
	               	 else if(i>0) 
	               	 {
			               break;
		          }
		         else
		         {
                                         cout << "Illegal data entry! You'll need to try again! "; 
                                         f = 0;
                                         i = 0;
                                         w = 0;
                         }
                     }
            }
	
        name = named;
        weight = w;
        inches=i;
        feet=f;
        if (inches>=12)
        {
	        feet+=inches/12;
        	inches=inches%12;

        }

}
void person::grow(int i)
{
     if (i <= 0)
     {
           cout << "Number of inches to grow must be positive!"<<endl;
           i = 0;
     }
     else
     {
         inches = inches +i;
     }

if (inches>=12)
{
	feet+=inches/12;
	inches=inches%12;

}
     
}


void person::shrink(int i)
{
	int temp=0;
     if (i <= 0)
     {
           cout << "Number of inches to shrink must be positive and not 0!"<<endl;
           i = 0;
     } 
	 else if ((feet*12)+(inches-i)<=0)
	 {
		 cout<<"HOW IS THAT POSSIBLE...TRY AGAIN"<<endl;
	 }
     else
     {
		if (inches<i)
		{
		   temp = ((feet*12)+(inches))-i;
		   inches=(((feet*12)+(inches))-i)%12;
		
		   feet=temp/12;
		

		}
		else
         inches -=i ;
     }

    
     
}


void person::gain (int w)
{
     if (w <= 0)
     {
           cout << "Number of pounds to gain must be positive and not 0!"<<endl;
           w = 0;
     }
     else
     {
         weight = w + weight;
     }
}

void person::lose (int w)
{

     if (w <= 0)
     {
		
         cout << "Number of pounds to lose must be positive and not 0!"<<endl;
		   w = 0;
     }
     else if ((weight-w)<=0)
     {
         cout<<"HOW IS THAT POSSIBLE...TRY AGAIN"<<endl;
     }
     else
     {
         weight = weight - w; 
     }    
}


void person::print ()
{
	   cout<<"----------------------------------------------------------------"<<endl
		 << "INFORMATION"<<endl
		 << "----------------------------------------------------------------"
		 << "\nName: " << name
		 << "\nHeight: " << feet << " feet, " << inches << " inches "
		 << "\nWeight: " << weight << " pounds "<<endl
		 <<"----------------------------------------------------------------";
}


main.cpp
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
#include <cstdlib>
#include <iostream>
#include <string>
#include "person.h"

using namespace std;
void menu()
{

	    cout << "\nEnter one of the following: "
           << "\ne: Enter a persons's statistics"
	   << "\ng: Grow by a number of inches"
           << "\ns: Shrink by a number of inches"
           << "\ni: Increase in weight"
           << "\nd: Decrease in weight"
           << "\np: Print the current statistics"
           << "\nq: Quit the program" 
	   <<"\n>> ";
}
int main()
{
    int i,w;
    char command;
    person p;
    
    p.input ();
    p.print();

    menu();
    cin >> command;
    
    while (command != 'q')
    {
          switch (command)
          {
                 case 'e': 
                           p.input();
                           break;
                 case 'g': 
        //corrected
                         cout<<endl<<"Enter Number Inches to grow>>";
                         cin >> i;
                         p.grow( i);
                         break;
                         case 's':
        //corrected
                         cout<<endl<<"Enter Number Inches to shrink>>"; 
                         cin >> i;
                         p.shrink(i);
                         break;
                         case 'i':
        //corrected
                           cout<<endl<<"Enter Number Weight to gain>>"; 
                           cin>>w;
                           p.gain(w);
                           break;
                 case 'd':
        //corrected
                         cout<<endl<<"Enter Number Weight to shrink>>";                        
                         cin>>w ;
                        p.lose(w);
                           break;
                 case 'p': p.print();
                           break;
                 default: cout << "You must enter either e, g, s, i, d, p, or q!";
          }
          
	menu();
    
    cin >> command;
    }
    
    cout << "\nThank you!!!"; 
    
    
    system("PAUSE");
    return 0;
}


Last edited on
Topic archived. No new replies allowed.