bmi class (beginner problem)

closed account (yAUiz8AR)
i'm a beginner and i'm trying to build a BMI calculator as a class, i honestly don't know what's wrong, i'm not sure how to initialize it either, help!
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
#include <iostream>
#include <cmath>

using namespace std;

class bmiCalculator {
    public:
        int bmiEquation ()
        {
    int weight;
    int height;
    

   cout <<"\n*******************************************\n";
    cout <<"This program will calculate your BMI and give you some suggestions based on your results";
    cout <<"\n*******************************************\n";
    cout << "\nPlease enter your weight in pounds\n";
    cin >> weight;
    cout << "\nEnter your height in inches";
    cin >> height;
    cout <<"Your estimated BMI is "<<(weight/pow (height, 2.0))*703 <<endl;
        }
};
int main(){

bmiCalculator bmiObject;
bmiObject.bmiEquation();

};

cout <<"\n*******************************************\n";
cout <<"Review your results and compare them with the BMI chart:";
cout <<"\nVery severely underweight -> less than 15\n";
cout <<"\nSeverly underweight -> From 15.0 to 16.0\n";
cout <<"\nUnderweight -> From 16.0 to 18.5\n";
cout <<"\nNormal (healthy weight) -> From 18.5 to 25\n";
cout <<"\nOverweight -> From 25 to 30\n";
cout <<"\nObese -> From 30 and over\n";
cout <<"\n*******************************************\n";
cout <<"\nBased on results what would you like to do? 1) lose weight, 2) maintain, 3) gain weight\n";

//from here on i want to make a loop that based on the option user chooses, it will calculate calorie intake and suggest a work out schedule//
system ("pause");
return 0;
}
The closing brace for main is on line 29.

So then what is all that stuff on line 31 and up? Did you intend to put that inside of main?
Last edited on
closed account (yAUiz8AR)
from 31 on i want it to display the BMI values for user to compare his/her results then line 40 asks what would you like to do etc...then i will type a loop for the 3 different options, does that make sense? or should i put all that inside the class?
You can't just have lines of executable code sitting there outside a function. If you want those lines of code to do something, they have to be inside a function.

As Disch said - did you intend for those lines to be executed by main?
closed account (yAUiz8AR)
i wanted to "end" the bmi class and start something totally different from line 31 (is that possible? what am i doing wrong?), right now my main concern is that the class works (and it does't) from line 1-29
But when would those lines run? How will the program know when those lines are to be executed? When do you want them to be executed? In which function?


Last edited on
closed account (yAUiz8AR)
class to come up first, line 31 ... after the class
Program flow basics:

With a few exceptions... executable code (that you want the program to run) must be put inside a function.

Main is the first function run by your program. Main can call other functions, at which point your program will "jump" to that other function... execute all the code contained in that function... then it will "jump back" to main and resume.

Those other functions can call other functions as well.


In your program, you have main(), which is then calling your bmiEquation() function. Both of these functions contain executable code, which is good.


But then you have stray code outside of any function... on line 31+. This code is not in any function.. and therefore cannot be executed (and therefore, the compiler is probably barking at you and giving you errors).


So the question is... where do you want this code to execute? Before or after you call bmiEquation? If before... you would put it in main above that call. If after, you'd put it in main after that call.


Or... you could put all that code in a separate function and call that new function from main.
What do you mean, "after the class"? Do you mean after the lines in main which create the class and call the bmiEquation method? If so, why aren't they also in main?

Also:

On line 44, you have a return statement. But it's not inside a function - so what could it possibly be returning from?

On line 45, you have a closing brace? Why? What is it supposed to be closing? Where is the corresponding opening brace?
closed account (yAUiz8AR)
i want 31... to execute after bmiEquation, how do i call a new function for main? sorry for my weird logic i'm too new at this!
closed account (yAUiz8AR)
the system ("pause") return 0; my professor always makes us put it at the end of everything just so the program does the "press any key..." (press enter and close)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void myFunction()
{
    // this is a new function.... you can put whatever executable code you want
    //   in here
}


int main()
{
    // main is the "entry point" function... IE, your program starts executing
    //   code here

    myFunction();  // <- this will call "myFunction".  Code inside that function
        // body will execute.  Once it finishes, the program will return back here
        // and will continue to execute code from main
}
closed account (yAUiz8AR)
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
#include <iostream>
#include <cmath>

using namespace std;
void class bmiCalculator
{
   class bmiCalculator {
    public:
        int bmiEquation ()
        {
    int weight;
	int height;
    
	cout <<"\n*******************************************\n";
    cout <<"This program will calculate your BMI and give you some suggestions based on your results";
    cout <<"\n*******************************************\n";
    cout << "\nPlease enter your weight in pounds\n";
    cin >> weight;
    cout << "\nEnter your height in inches";
    cin >> height;
    cout <<"Your estimated BMI is "<<(weight/pow (height, 2.0))*703 <<endl;

        }
};

int main()
{

cout <<"\n*******************************************\n";
cout <<"Review your results and compare them with the BMI chart:";
cout <<"\nVery severely underweight -> less than 15\n";
cout <<"\nSeverly underweight -> From 15.0 to 16.0\n";
cout <<"\nUnderweight -> From 16.0 to 18.5\n";
cout <<"\nNormal (healthy weight) -> From 18.5 to 25\n";
cout <<"\nOverweight -> From 25 to 30\n";
cout <<"\nObese -> From 30 and over\n";
cout <<"\n*******************************************\n";
cout <<"\nBased on results what would you like to do? 1) lose weight, 2) maintain, 3) gain weight\n";


    class bmiCalculator();  

}
closed account (yAUiz8AR)
this is how i understood it, im soo slow at this
void class bmiCalculator

This is incorrect. You seem to be mixing up the concept of functions and classes. You seem like you need a deeper explanation of what actually is going on here. Let me try to elaborate.

A function is a block of code that can be "called". Functions perform a certain task by executing code when they're called. As such... you can think of a function as a verb... in that it "does something".

In my previous example.... there were two functions: main() and myFunction(). As mentioned, your program starts executing code inside of main. When main calls another function (like myFunction), the program will do the following:

- stop executing code in main
- jump to myFunction
- execute code inside of myFunction
- when myFunction exits, return back to main
- continue executing from where it left off in main

Once the program reaches the end of main, the program will close.

Understanding functions is CRITICAL to programming in languages like C++. Make sure you completely understand everything I said above. If you don't, ask questions. You won't be able to understand classes or other more complex concepts until you understand the above.


Assuming you understand functions:

A class is something else entirely. Classes are not blocks of executable code like functions are. You do not "call" classes. The code you put inside of a class body is not executable.

Instead, classes represent a "thing".

If functions are verbs because they do something.... then classes are nouns because they are a thing.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Foo  //  a simple class called 'Foo'
{
};

int main()
{
    int var;  // this create a variable named 'var'.  var is an integer becaue
              //  it is of type 'int'
              // We can use var in our code by assigning values to it, printing
              //  it, etc.  It represents a tangible thing.
              
    Foo obj;  // Here, we're creating another variable named 'obj'.  Just like
              //  var, we can use it and manipulate it.  It's a "thing".
              // The difference is... instead of being an integer... it's a "Foo"
}



See the difference between classes and functions?

Now... moving on....


A member function is a function that is part of a class. Just like any other function... member functions are verbs in that they do something. But since they're part of a class, it's implied that they do something with that class.

So... to recap:
- Functions are verbs
- Classes are nouns
- Member functions are verbs which use a noun


Coming back to the code in your original post:

1
2
3
4
5
6
7
class bmiCalculator {
    public:
        int bmiEquation ()
        {
           //...
        }
};


Here, bmiCalculator is a class. It's a thing.
bmiEquation is a function. It's a verb... it does something: It gets input from the user and calculates an prints the bmi.

But bmiEquation is also inside of bmiCalculator... which makes it a member function. That is... it uses a bmiCalculator object to do its work.

(This is not very clear from your code example though... because the way you have this class written... bmiEquation could be in just a normal non-member function. The actual bmiCalculator class doesn't really do anything and serves no purpose. If you need a better example, let me know and I'll try to make one... but this post is already very long)

Now let's take a look at main and see what you're doing there:

1
2
3
4
5
int main()
{
    bmiCalculator bmiObject;
    bmiObject.bmiEquation();
}


First thing you do is you are creating an object... 'bmiObject'. This is our 'thing'. We now want to use that thing to actually do the calculation... so you use it to call the member function bmiEquation:

bmiObject.bmiEquation(); <- This uses our bmiObject variable and calls the member function bmiEquation. Remember that member functions need an object on which to perform their action. So we have to tell it which object we want to use. That's why we need bmiObject here.

Once this function is called... as with any function... the program will stop running code in main, it will jump to the code inside bmiEquation.




So.... does that help clarify things? Hopefully this wall of text doesn't just confuse you further.
closed account (yAUiz8AR)

-result is displayed along with BMI table- end of class then i want add a loop doing other stuff

Last edited on
You can do all of that in main without any classes or other functions if you wanted. All you need is to understand how the flow of the program works.

IE:

- program starts in main
- program goes to other functions only when called
- code outside of any function cannot be executed because there is no way to call it.



EDIT:

In case it isn't clear... I'm trying to help with your deeper conceptual problems rather than merely trying to help you solve this one particular assignment. Your original code was very close to what you wanted, you just made a very silly goof that should be incredibly easy for you to spot if you understand the basics of program flow.
Last edited on
closed account (yAUiz8AR)
sorry to be specific, a class is required and i wanted the class to be the bmi calculator, after the bmi was calculated by the class i was going give 3 different options lose weight, maintain, gain and calculate a calorie intake (just as a loop), but obviously i'm completely lost at the class part
Topic archived. No new replies allowed.