Solve Beginner C++ Hassles: Convert Grams to Kilograms

  • Comp Sci
  • Thread starter Lord Anoobis
  • Start date
  • Tags
    Beginner C++
In summary, the program converts a package of butter from grams to kilograms and calculates the number of packages needed to make up at least 1 kilogram. It prompts the user to enter the weight of the package in grams and outputs the weight in kilograms, as well as the number of packages needed. The user can repeat this calculation as many times as they wish. The program has a bug where the number of packages required is correct for the first figure entered, but carries over for any other value. This is due to the variables 'total_weight' and 'packages' not being reset after each iteration.
  • #1
Lord Anoobis
131
22

Homework Statement


A kilogram is 1000 grams. Write a program that will read the weight of a package of butter in grams and output the weight in kilograms, as well as the number of packages of butter needed to yield 1 kilogram of butter. Your program should allow the user to repeat this calculation as often as the user wishes.

Homework Equations

The Attempt at a Solution


/*Converts a package of butter from grams to kilograms and
calculates the number of packages needed to make up at least 1 kilogram*/

#include <iostream>
using namespace std;
int main()
{
const float kilo = 1000; //grams per kg
float total_weight = 0, weight_g, weight_kg;
int packages = 0;
cout << "Enter the weight of the package of butter in grams: ";
cin >> weight_g;
cout.setf(ios::fixed);
cout.precision(3);

while (weight_g != 0)
{
weight_kg = weight_g/kilo; // convert to kg
cout << endl << "The weight of the package in kilograms is: "
<< weight_kg << "kg" << endl;

while (total_weight < 1000) // counting the packages required
{
total_weight = total_weight + weight_g;
packages++;
}

cout << "For at least 1 kilogram of butter you need "
<< packages << " packages." << endl;
cout << "Enter another package weight to continue or enter 0 to "
<< "end the program." << endl << endl
<< "Package weight: ";
cin >> weight_g;
}

return 0;
}

The program works in all aspects but one, crucial too. The number of packages required is correct for the first figure entered, but carries over for any other value. So it stays, for example, 5 packages for 200g. I'm sure I'm missing something simple here but I just don't see it. The compiler used is Codeblocks. Please assist.
 
Physics news on Phys.org
  • #2
Lord Anoobis said:
The number of packages required is correct for the first figure entered, but carries over for any other value.
Disregarding possible optimizations :wink:, consider what value 'total_weight' has as you iterate - does it ever get reset?

You can use [ code ][ /code] tags (without the spaces) to make your code more legible.
 
  • #3
milesyoung said:
Disregarding possible optimizations :wink:, consider what value 'total_weight' has as you iterate - does it ever get reset?

You can use [ code ][ /code] tags (without the spaces) to make your code more legible.
It seems that total_weight is using the same initial value on each pass. I can't see why the new value is used for the conversion but gets tossed for the number of packages though. I get the feeling it has to do with the second while loop, right?
 
  • #4
Lord Anoobis said:
It seems that total_weight is using the same initial value on each pass. I can't see why the new value is used for the conversion but gets tossed for the number of packages though. I get the feeling it has to do with the second while loop, right?

Yes and no. Try this. On a piece of paper set up all your variables and walk through the code updating the variables by hand. Do only what the code tells you to do. Might point out your problem.

Or alternatively

When facing a coding bug try and ask yourself when does the bug happen and when does it not happen?
In your case you say it works for the first time but not the subsequent times.
So what does your code do on the first run that it doesn't on the next time through?? (Hint you actually have at least 2 of these errors but one is being masked by the current issue you're having :wink:)
 
  • #5
Lord Anoobis said:
It seems that total_weight is using the same initial value on each pass.
After you've calculated the first package count, then 'total_weight' might, for instance, have the value 1005. As you encounter the while loop a second time, 'total_weight < 1000' evaluates to false, and you'll never enter the loop again, i.e. 'packages' will always have the value of the first count.

Lord Anoobis said:
I can't see why the new value is used for the conversion but gets tossed for the number of packages though.
You're talking about 'weight_g'? It does change, but since you effectively disable the second while loop, it makes no difference.
 
  • #6
milesyoung said:
After you've calculated the first package count, then 'total_weight' might, for instance, have the value 1005. As you encounter the while loop a second time, 'total_weight < 1000' evaluates to false, and you'll never enter the loop again, i.e. 'packages' will always have the value of the first count.You're talking about 'weight_g'? It does change, but since you effectively disable the second while loop, it makes no difference.
Damn. Talk about being in plain sight. Reinitializing total_weight and packages solves the problem. Is there a simpler way of going about this program as a whole
cpscdave said:
Yes and no. Try this. On a piece of paper set up all your variables and walk through the code updating the variables by hand. Do only what the code tells you to do. Might point out your problem.

Or alternatively

When facing a coding bug try and ask yourself when does the bug happen and when does it not happen?
In your case you say it works for the first time but not the subsequent times.
So what does your code do on the first run that it doesn't on the next time through?? (Hint you actually have at least 2 of these errors but one is being masked by the current issue you're having :wink:)

?
 
  • #7
cpscdave said:
Yes and no. Try this. On a piece of paper set up all your variables and walk through the code updating the variables by hand. Do only what the code tells you to do. Might point out your problem.

Or alternatively

When facing a coding bug try and ask yourself when does the bug happen and when does it not happen?
In your case you say it works for the first time but not the subsequent times.
So what does your code do on the first run that it doesn't on the next time through?? (Hint you actually have at least 2 of these errors but one is being masked by the current issue you're having :wink:)
Checking it on paper certainly did make things clearer. The packages counter issue is quite well hidden behind the other. I'll be sure to do the paper check more often in future.
 
  • #8
Lord Anoobis said:
Damn. Talk about being in plain sight. Reinitializing total_weight and packages solves the problem. Is there a simpler way of going about this program as a whole?
Whoops. Dunno what happened there. Time to hit the deck I think.
 
  • #9
Glad you got what I was trying to get at :D

To answer your questions yes there are couple other ways you can solve this problem.

Is there a way you can figure out the number of packages required using only math? Think how you would solve this problem yourself :)

Also you could write the over all program flow using a do/while loop as opposed to a while loop.

I usually tell people there are 3 basic looping types
For loops (when you want to run the loop an known number of times)
do while loops (when you want to run the loop at least one time and until a condition is met)
while loops (when you want to run the loop until a condition is met)

This seems to fit the 1 time and until condition is met.
 

FAQ: Solve Beginner C++ Hassles: Convert Grams to Kilograms

1. How do I convert grams to kilograms in C++?

To convert grams to kilograms in C++, you can use the formula: kilograms = grams / 1000. This is because there are 1000 grams in one kilogram. So, to convert a given value in grams to kilograms, simply divide that value by 1000 and store the result in a variable.

2. What data type should I use to store the values in grams and kilograms?

In C++, you can use the float data type to store decimal values in grams and kilograms. If you want to store whole numbers, you can use the int data type. It is important to choose the appropriate data type based on the type of values you want to store.

3. How can I make my program convert from kilograms to grams as well?

You can modify your program to convert from kilograms to grams by using the formula: grams = kilograms * 1000. This is the inverse of the formula used to convert from grams to kilograms. So, you can add a menu option or user input to choose which conversion the user wants to perform.

4. Can I use the same formula to convert other units of mass, such as pounds to kilograms?

Yes, you can use the same formula to convert other units of mass to kilograms. For example, to convert pounds to kilograms, you can use the formula: kilograms = pounds * 0.453592. You just need to find the appropriate conversion factor for the specific unit you want to convert from.

5. Is there a built-in function in C++ to convert units of mass?

No, there is no built-in function in C++ specifically for converting units of mass. You will need to use a formula or write your own function to perform the conversion. However, there are libraries available that have functions for unit conversions, such as the Boost Units library.

Similar threads

Replies
3
Views
957
Replies
2
Views
2K
Replies
8
Views
2K
Replies
9
Views
2K
Replies
6
Views
3K
Replies
23
Views
2K
Replies
13
Views
2K
Replies
3
Views
1K
Back
Top