1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Beginner C++ hassles

  1. Apr 8, 2015 #1
    1. The problem statement, all variables and given/known data
    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.

    2. Relevant equations

    3. 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;

    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;

    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.
  2. jcsd
  3. Apr 8, 2015 #2
    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.
  4. Apr 8, 2015 #3
    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?
  5. Apr 8, 2015 #4
    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:)
  6. Apr 8, 2015 #5
    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.
  7. Apr 8, 2015 #6
    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
  8. Apr 8, 2015 #7
    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.
  9. Apr 8, 2015 #8
    Whoops. Dunno what happened there. Time to hit the deck I think.
  10. Apr 8, 2015 #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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted