Flowchart help

Can someone help me create a good algorithm and flowchart for this program?

#include <iostream>
using namespace std;

int main(){

//Declaring variables
int age, multiple = 10;
double waterNeeded = 0.0, weight, exerciseMins;

//Getting the input entered by the user
cout << "Enter your weight: ";
cin >> weight;
cout << "Enter your age: ";
cin >> age;
cout << "How much time do you exercise (in mins): ";
cin >> exerciseMins;

//calculations for weight needed
waterNeeded += weight / 2.2;
if (age < 30)
{
waterNeeded *= 40;
}
else if (age >= 30 && age <= 55)
{
waterNeeded *= 35;
}
else if (age > 55)
{
waterNeeded *= 30;
}
waterNeeded /= 28.3;
waterNeeded += (exerciseMins / 30) * 12;
int orig = waterNeeded;

//rounding to the nearest 10
int rounded = ((orig + multiple / 2) / multiple) * multiple;

//Displaying the output
cout << "Water needed (in ounces): " << rounded << endl;

return 0;
}
Last edited on
Hello marygamess,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

You are a bit backwards here as you should have written the flow chart first to know how to write your program. All you can do now is make your flow chart match your code.

But, first I would fix up the program before working on the flow chart.

You are using magic numbers like 2.2, 40, 35 30, 28.3 and 12 along with multiple. All of them should be defined as a const at the beginning of the program, so that there is only one place to change these numbers in the program.

Also you have initialized two variables when they were defined, but all your variables need to be initialized when they are defined. This avoids problems when the program compiles and runs. Especially when the program runs and you use a variable with an unknown value in a calculation receiving a wrong result.

Without seeing the instructions for this program I can only guess if the math is being done correctly or if it can be shortened or done any better. Also with out a known input and output to work with I am not even sure if the math is correct.

That reminds me when you enter a weight is it in pounds of kilos? This could make a big difference in your calculations.

Two last thing I will mention. It help to make the code easier to read.

You have written:
int main(){
And there is nothing wrong with this placement of the opening {, but this makes the program a bit hard to follow.

An alternative to this is:

1
2
3
4
int main()
{
    // Code here
}  // End main 

This way the {}s line up in the same column and the indentation shows what is part of that block. Also when the closing } is beyond the last line of the screen it makes it easier to find the opening {. It is mostly a matter of style and preference as there are several ways to use {} to define a block of code all of which make no difference to the compiler.

The use of using namespace std; is a bad idea as it WILL get you in trouble some day. Then when you leave school and start your first job and they tell you not to use this line ever yo wll now have to learn what you should have in a short period of time. It is better to learn how to use "std::" to qualify what is in the standard name space now when it is easier rather than later.

Hope that helps,

Andy
@simonferry,

The answer is 42.

See this: https://www.youtube.com/watch?v=aboZctrHfK8

Edit: response is to a deleted post.
Last edited on
Registered users can post here. Sign in or register to post.