Can't get class to print or run at all!

It's supposed to ask the user for input and the switch decides what it does but when I compile and run it's just an infinite loop.
It's first supposed to display a menu that says: Enter the following: e: enter person's stats, g: grow by a number of inches, etc. Of course it prints error messages and runs until the user presses q for quit. But I can't even get the first prompt to come up to check to see if all my math is correct in the program.

PS. every time I try to use the <> for the code it does not work for me.

header


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
#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)
	 {
                   cout << "\nEnter Name: ";
                    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 << "Height after shrinking must be greater than zero!"<<endl;
           i = 0;
     } 
	 else if ((feet*12)+(inches-i)<=0)
	 {
		 cout<<"Illegal entry"<<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 << ""<<endl;
		   w = 0;
     }
     else if ((weight-w)<=0)
     {
         cout<<"Weight after losing must be greater than 0!"<<endl;
     }
     else
     {
         weight = weight - w; 
     }    
}


void person::print ()
{
	int bmi;
	bmi = weight * 703;
	   cout << "\nName: " << name
		 << "\nHeight: " << feet << " feet, " << inches << " inches "
		 << "\nWeight: " << weight << " pounds "
		 << "\n BMI: " <<bmi << "inches squared"
		 <<endl;
}


main

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

using namespace std;
int main()
{

    int i,w;
    char command;
    person p;
    p.input ();
    p.print();
    
	
    while (command != 'q')
    {
	cout << "\nEnter one of the following: "
           << "\n e: Enter a persons's statistics"
	   << "\n g: Grow by a number of inches"
           << "\n s: Shrink by a number of inches"
           << "\n i: Increase in weight"
           << "\n d: Decrease in weight"
           << "\n p: Print the current statistics"
           << "\n q: Quit the program" 
	   <<"\n>> ";
    cin >> command;
          switch (command)
          {
                case 'e': p.input();
                           break;
                 case 'g': cout<< "Enter a number of inches to grow: ";
                         cin >> i; 
						 p.grow(int i);
                           break;
                 case 's': cout<<"Enter Number Inches to shrink: "; 
                         cin >> i;
                         p.shrink(int i);
                           break;
                 case 'i':  cout<< "Enter a number of pounds to increase: "; 
                        cin >> w;
						p.gain(int w);
                           break;
                 case 'd': cout << "Enter a number of pounds to decrese: ";                        
                         cin >> w ;
						 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 << "\nThank you!!!"; 
    
    
    system("PAUSE");
    return 0;
}
Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

have you tried using your IDE's integrated debugger to figure out why your program isn't doing what you expect?
every time I try to use the <> for the code it does not work for me.

Read the following link on how to use code tags properly.
http://www.cplusplus.com/articles/jEywvCM9/
fixed
Why are you calling functions on lines 13 and 14?

On line 17 you use command but that variable has not been initialized yet. There is a chance the random garbage value it holds is 'q' which would skip the while loop entirely.
I'm getting an error now that says there's an expected primary expression before int in lines 34, 38, 42 & 46. I added one more thing because I forgot that I needed to calculate BMI as well. This is the new thing:

header
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>
#include <string>

using namespace std;

class person
{
      private:
         string name;
         int feet;
         int inches;
         int weight;
         double bmi;
      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;
     bmi = 0.00;
}


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 ()
{
	bmi = weight * 703;
     cout << "Name:   " << name;
     cout << "Height: " << feet << "feet, " << inches << "inches ";
     cout << "Weight: " << weight << "pounds ";
     cout << "BMI: " << bmi << " inches squared";
}


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

using namespace std;
int main()
{
    int i,w;
    char command;
    person p;
   // p.input ();
   // p.print();
    
	
    while (command != 'q')
    {
	cout << "\nEnter one of the following: "
           << "\n e: Enter a persons's statistics"
	   << "\n g: Grow by a number of inches"
           << "\n s: Shrink by a number of inches"
           << "\n i: Increase in weight"
           << "\n d: Decrease in weight"
           << "\n p: Print the current statistics"
           << "\n q: Quit the program" 
	   <<"\n>> ";
    cin >> command;
          switch (command)
          {
                case 'e': p.input();
                           break;
                 case 'g': cout<< "Enter a number of inches to grow: ";
                         cin >> i; 
						 p.grow(int i);
                           break;
                 case 's': cout<<"Enter Number Inches to shrink: "; 
                         cin >> i;
                         p.shrink(int i);
                           break;
                 case 'i':  cout<< "Enter a number of pounds to increase: "; 
                        cin >> w;
						p.gain(int w);
                           break;
                 case 'd': cout << "Enter a number of pounds to decrese: ";                        
                         cin >> w ;
						 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 << "\nThank you!!!"; 
    
    
    system("PAUSE");
    return 0;
}
Last edited on
CPlusPlusAnnoyed wrote:
I'm getting an error now that says there's an expected primary expression before int in lines 34, 38, 42 & 46.
You seem to have forgotten how function call expressions work. On those lines, remove the word int - you only specify a type when you are declaring a variable.
Ok, I have everything working now but one thing. I realized that I have to calculate BMI. the problem is that BMI has to be initialized in the constructor. The formula is bmi = (weight * 703)/(inches^2). With bmi being initialized as zero, of course the program stops running when printed because anything divided by zero is illegal. So I tried do an if statement but nothing is working.

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

using namespace std;

class person
{
      private:
         string name;
         int feet;
         int inches;
         int weight;
         double bmi;
      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;
     bmi = 0;
}


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

        }

}

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

void person::shrink(int i)
{
	int temp;
     if (i < 0)
     {
           cout << "\nNumber of inches to shrink must be positive!";
           i = 0;
     }
     
      else if ((feet*12)+(inches-i)<=0)
	 {
		 cout<<"\nHeight after shrinking must be greater than zero! \n";
	 }
	 
      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 << "\nNumber of pounds to gain must be positive!";
           w = 0;
     }
     else
     {
         weight = w + weight;
     }
}

void person::lose (int w)
{
     if (w < 0)
     {
           cout << "\nNumber of pounds to lose must be positive!";
           w = 0;
     }
     
     else if ((weight-w) <= 0)
     {
         cout<<"\nWeight after losing must be greater than zero! "<<endl;
     }
     else
     {
         weight = weight - w; 
     }    
}

void person::print ()
{	
	int f = (feet * 12);
	int total_inches = (f + inches);
	if (total_inches = 0)
	{
		cout << "Name:   " << name;
     cout << "\nHeight " << feet << " feet, " << inches << " inches ";
     cout << "\nWeight: " << weight << " pounds ";
     cout << "\nBMI: 0.00";
     cout <<endl;
	}
	else
	{
	bmi = (weight * 703)/(total_inches*total_inches);
	cout << "Name:   " << name;
     cout << "\nHeight " << feet << " feet, " << inches << " inches ";
     cout << "\nWeight: " << weight << " pounds ";
     cout << fixed << showpoint << setprecision (2) << "\nBMI: " << bmi;
     cout <<endl;
	}	
     
}


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

using namespace std;
int main()
{
    int i,w;
    char command;
    person p;
    
	
    while (command != 'q')
    {
	cout << "\nEnter one of the following: "
           << "\n e: Enter a persons's statistics"
	   << "\n g: Grow by a number of inches"
           << "\n s: Shrink by a number of inches"
           << "\n i: Increase in weight"
           << "\n d: Decrease in weight"
           << "\n p: Print the current statistics"
           << "\n q: Quit the program" 
	   <<"\n";
	   cout << "\nCommand: ";
    cin >> command;
    cout << endl;
          switch (command)
          {
                case 'e': p.input();
                           break;
                 case 'g': cout<< "Enter a number of inches to grow: ";
                         cin >> i; 
						 p.grow(i);
                           break;
                 case 's': cout<<"Enter a number of inches to shrink: "; 
                         cin >> i;
                         p.shrink(i);
                           break;
                 case 'i':  cout<< "Enter a number of pounds to increase: "; 
                        cin >> w;
						p.gain(w);
                           break;
                 case 'd': cout << "Enter a number of pounds to decrease: ";                        
                         cin >> w ;
						 p.lose(w);
                           break;
                 case 'p': p.print();
                           break;
                case 'q': cout << "\nThank you! \n";
                			break;
                 default: cout << "You must enter either e, g, s, i, d, p, or q!\n";
          }
    }
    
     
    
    
    system("PAUSE");
    return 0;
}
Does the BMI have to be stored as a data member? Couldn't you instead create a member function that calculates it on the fly?
Hi,

With bmi being initialized as zero, of course the program stops running when printed because anything divided by zero is illegal.


Zero is not always a good choice to initialise with. Sometimes 1 is good, other times something obviously wrong is good.

Line 165: = is assignment, you want == for equality comparison.

The lines following that print out values if inches is zero, is that useful?

Line 175 does integer division, even though the result is supposed to be double. Use static_cast to make either weight or total_inches a double.

In person::input() you have some validation code at the end, would this not be better before assigning values to class members?

Consider writing a constructor that takes arguments which are the values of the class data members. Use an initialiser list to set the member variables. I changed the types to std::size_t - you could change the type of the class members too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
person::person(const std::string& Name,
                       const std::size_t Feet,
                       const std::size_t Inches,
                       const std::size_t Weight,
                       const double Bmi = 1
                      )
                      :   // colon introduces initialiser list
                      name        (Name),
                      feet           (Feet),
                      inches       (Inches),
                      weight       (Weight),
                      bmi           (Bmi);
{
    // either  validate the data before calling the constructor
    // or validate the data here, and throw exception if there are problems
    // or set a global error variable, this is one valid use of a global

   // invariants
   // weight cannot be <= 0. 
   // feet must be >= 0
   // 0 >= inches <= 12
   // Put sensible limits on height & weight
}


I would choose different variable names, like weightLoss instead of w

Good Luck !!

Edit: The print function should be marked const it doesn't affect the state of the class. Arguments to your functions should be const as well

161
162
void person::print () const
{


143
144
145
void person::lose (const int weightLoss)  // consider making weightLoss std::size_t
{
     if (w < 0)
Last edited on
One advice, you shouldn't put too many things on main(). you can create a new class display it.
Topic archived. No new replies allowed.