Calculate Jacket Size with Height, Weight, and Age | C++ Loop Tutorial

  • C/C++
  • Thread starter Streitsky
  • Start date
  • Tags
    C++ Loop
In summary, the user is trying to calculate the jacket size based on their height, weight, and age. They are considering using an if/else statement or a loop to adjust the size based on age, but ultimately decide to use a formula that involves dividing and rounding the age instead.
  • #1
Streitsky
2
0
Hey guys it my first post, so please be nice :). So I am doing a problem right now and I can decide how to go about it... I am kinda lost right now, I could use an If/Else statement but it would take along time so I decided to use a loop, so here is the problem.

Program asks user to enter height weight and age. (Completed already)

Jacket size = height times weight divided by 288 and then (This is where I am having trouble>>) adjusted 1/8 of an inch for 10 years after 30 (For example adjusted 1/8 of an inch at 40, 50, 60, 70, but not at 39, 49, 59, 69) Thanks for your help
 
Technology news on Phys.org
  • #2
You could solve this with a loop, but I'd just divide and round:

Code:
if (age < 40)
  adj = 0;
else
  adj = floor(age / 10 - 3) * 1.0/8;
 
  • #3
hmm yea, maybe I shouldn't loop it, ill try the if else statement thanks
 

1. How do I create a loop in C++?

In C++, there are several types of loops you can use: for, while, and do-while. Each type has its own syntax and purpose. To create a loop, you need to define a condition that will be evaluated after each iteration, and a set of statements to be executed within the loop.

2. How do I control the number of iterations in a loop?

The number of iterations in a loop can be controlled using a loop counter or a conditional statement. For example, in a for loop, you can specify the number of iterations by setting the initial value of the loop counter, the condition for the loop to continue, and the increment or decrement of the loop counter. In a while loop, you can use a conditional statement to specify when the loop should terminate.

3. How do I break out of a loop in C++?

To break out of a loop in C++, you can use the break keyword. This will immediately terminate the loop and continue with the code after the loop. You can also use break within a conditional statement to break out of a loop when a certain condition is met.

4. Can I skip an iteration in a loop?

Yes, you can use the continue keyword to skip an iteration in a loop. This will jump to the next iteration without executing the statements within the loop for the current iteration. You can also use continue within a conditional statement to skip an iteration when a certain condition is met.

5. How do I avoid infinite loops in C++?

Infinite loops occur when the condition of the loop is always true, causing the loop to continue indefinitely. To avoid this, make sure to properly define the condition for the loop to terminate. You can also use break or continue within the loop to control the number of iterations. Additionally, debugging tools and techniques such as printing out the current value of the loop counter can help identify and fix infinite loops.

Similar threads

  • Programming and Computer Science
Replies
19
Views
975
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Mechanical Engineering
Replies
19
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • STEM Career Guidance
Replies
24
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Replies
33
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
7
Views
741
Back
Top