C++ function logic problem!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paradox6996
    New Member
    • Jul 2007
    • 21

    C++ function logic problem!

    I'm trying to convert someones cereal weight from ounces to metric tons, and how many boxes it would take to make a metric ton. I suck at converting weights and I wanted to know if the logic in my function is correct and is converting the weights like it should be. If it is not correct can someone please tell me the syntax and function to do this. Thank You!

    #include <iostream>
    using namespace std;

    int main ()
    {
    double Cereal_Weight, Cereal_Total, Total_Boxes;

    cout << "Welcome to the cereal converter program\n";
    cout << "Enter the weight in ounces of the cereal box\n";
    cin >> Cereal_Weight;

    Cereal_Total = Cereal_Weight / 35273.92;
    Total_Boxes = Cereal_Weight * 35273.92;

    cout << "Your cereal weighs " << Cereal_Total << " metric tons\n";
    cout << "It would take " << Total_Boxes << " to fill a metric ton\n";

    cin.sync();
    cin.get();
    return 0;
    }
  • seforo
    New Member
    • Nov 2006
    • 60

    #2
    If you can do it mathematically you can program it simply. Write a functional algorithm first or a pseudocode and then simply code it. Bearing in mind that
    1 ounce = 2.83495231e-5 metric tons

    Comment

    • paradox6996
      New Member
      • Jul 2007
      • 21

      #3
      Originally posted by seforo
      If you can do it mathematically you can program it simply. Write a functional algorithm first or a pseudocode and then simply code it. Bearing in mind that
      1 ounce = 2.83495231e-5 metric tons
      I don't know if I know how to do it mathematically, like I said I'm not good at converting weights and I haven't done it for years. I've tried googling it but all I get is converter's that convert it for me, so if someone can tell me if my logic in my program is correct: 35,273.92 ounces in a metric ton / by the number entered by the user.

      Cereal_Total = Cereal_Weight / 35,273.92;

      Is the logic correct if not can you please tell me what the function should look like so i can convert this and move on. Thank You!

      Comment

      • paradox6996
        New Member
        • Jul 2007
        • 21

        #4
        Anyone that could help would be great thank you!

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by paradox6996
          Anyone that could help would be great thank you!
          Oh come on, if you put 35,273.92 ounces together you get 1 metric ton, so
          if you put 35,273.92*x ounces together you get 1*x metric ton, and if you put
          x ounces together on a pile you get 1*x/35,273.92 ton which is x/35,273.92
          metric ton.

          So if you know the number of ounces 'x', you know the number of metric tons:
          x/35,273.92.

          kind regards,

          Jos

          Comment

          • paradox6996
            New Member
            • Jul 2007
            • 21

            #6
            Originally posted by JosAH
            Oh come on, if you put 35,273.92 ounces together you get 1 metric ton, so
            if you put 35,273.92*x ounces together you get 1*x metric ton, and if you put
            x ounces together on a pile you get 1*x/35,273.92 ton which is x/35,273.92
            metric ton.

            So if you know the number of ounces 'x', you know the number of metric tons:
            x/35,273.92.

            kind regards,

            Jos
            #include <iostream>
            using namespace std;

            int main()
            {
            double Cereal_Weight, Cereal_Total, Total_Boxes;
            char answer;
            do
            {
            cout << "Welcome to the cereal converter program.\n";
            cout << "Enter the weight in ounces of the cereal box.\n";
            cin >> Cereal_Weight;

            Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000);
            Total_Boxes = 35273.92 / Cereal_Weight;

            cout << "Your cereal weighs " << Cereal_Total << " metric tons,\n";
            cout << "It would take " << Total_Boxes << " cereal boxes to fill a metric ton.\n\n";
            cout << "Would you like to convert another cereal weight.\n"
            << "Press y for yes, n for no,\n"
            << "and press return: ";
            cin >> answer;
            }while (answer == 'y' || answer == 'Y');

            cout << "Goodbye!\n ";

            cin.sync();
            cin.get();
            return 0;
            }

            This way is funnier thou! Now since I figured that out, why is the output of the conversion truncated. It comes out to 0.000283495 when it should be:
            0.0002834952312 5. See I wasn't sure if the logic was correct for one I don't know the metric system, and two if I don't know the metric system then htf do I know if I did it right. So I tried long double in place of double still got same result but wouldn't matter anyway because double goes to 10^308 anyway. So back to the point htf do I get the second number and not the first.Thank You!

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by paradox6996
              #include <iostream>
              using namespace std;

              int main()
              {
              double Cereal_Weight, Cereal_Total, Total_Boxes;
              char answer;
              do
              {
              cout << "Welcome to the cereal converter program.\n";
              cout << "Enter the weight in ounces of the cereal box.\n";
              cin >> Cereal_Weight;

              Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000);
              Total_Boxes = 35273.92 / Cereal_Weight;

              cout << "Your cereal weighs " << Cereal_Total << " metric tons,\n";
              cout << "It would take " << Total_Boxes << " cereal boxes to fill a metric ton.\n\n";
              cout << "Would you like to convert another cereal weight.\n"
              << "Press y for yes, n for no,\n"
              << "and press return: ";
              cin >> answer;
              }while (answer == 'y' || answer == 'Y');

              cout << "Goodbye!\n ";

              cin.sync();
              cin.get();
              return 0;
              }

              This way is funnier thou! Now since I figured that out, why is the output of the conversion truncated. It comes out to 0.000283495 when it should be:
              0.0002834952312 5. See I wasn't sure if the logic was correct for one I don't know the metric system, and two if I don't know the metric system then htf do I know if I did it right. So I tried long double in place of double still got same result but wouldn't matter anyway because double goes to 10^308 anyway. So back to the point htf do I get the second number and not the first.Thank You!
              You should rename your variables so you won't get confused so easily;
              here are your variables:
              [code=cpp]
              double cereal_ounce; // number of ounces of cereals
              double cereal_ton; // number of metric tons of cereals
              [/code]

              Then you should build an ounce_to_ton converter (see my previous reply):

              [code=cpp]
              double ounce_to_ton(do uble x) { return x/35273.92; }
              [/code]

              You can use it as follows:

              [code=cpp]
              double cereal_ounce= // some value measured in ounces
              double cereal_ton= ounce_to_ton(ce real_ounce);
              [/code]

              I don't know where those boxes come from in your program; About the number
              of digits in the fraction printed: that's how many digits are printed by default.

              kind regards,

              Jos

              Comment

              • paradox6996
                New Member
                • Jul 2007
                • 21

                #8
                Originally posted by JosAH
                You should rename your variables so you won't get confused so easily;
                here are your variables:
                [code=cpp]
                double cereal_ounce; // number of ounces of cereals
                double cereal_ton; // number of metric tons of cereals
                [/code]

                Then you should build an ounce_to_ton converter (see my previous reply):

                [code=cpp]
                double ounce_to_ton(do uble x) { return x/35273.92; }
                [/code]

                You can use it as follows:

                [code=cpp]
                double cereal_ounce= // some value measured in ounces
                double cereal_ton= ounce_to_ton(ce real_ounce);
                [/code]

                I don't know where those boxes come from in your program; About the number
                of digits in the fraction printed: that's how many digits are printed by default.

                kind regards,

                Jos
                Boxes = how many boxes of X weight in ounces it takes to fill a metric ton(so how many boxes it would take to weigh a metric ton if the box weighed ten ounces) but thats right so im not worried. And on my last reply I forgot to add that that number I get is when I replace 10 with x as u say so 10 / 35273.92 that is the number I want to print out longer.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by paradox6996
                  Boxes = how many boxes of X weight in ounces it takes to fill a metric ton(so how many boxes it would take to weigh a metric ton if the box weighed ten ounces) but thats right so im not worried. And on my last reply I forgot to add that that number I get is when I replace 10 with x as u say so 10 / 35273.92 that is the number I want to print out longer.
                  So you solved everything now except for those number of digits printed? Good.
                  If you want, say, 20 decimals of the fraction printed (which is way too much
                  because only at most 15 fraction decimals are significant) do this before you
                  print anything:

                  [code=cpp]
                  cout.setf(0,ios ::floatfield);
                  cout.precision( 20);
                  [/code]

                  kind regards,

                  Jos

                  ps. I'm not sure whether or not that first line is needed; I don't have my docs
                  with me; give it a try.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    To paradox6996:

                    Get a book on ddimensional analysis.

                    Do you remember stuff like:

                    A
                    ---- x B
                    B

                    and the answer was A because the B's divided each other out? You can do the same thing with units of measure. To get kilometers from miles:

                    miles x kilometers
                    -----------
                    miles

                    You start with miles and multilpy by kilometers per mile. The miles divide each outher out and you are left with kilometers.

                    If you do it the other way:

                    miles x miles
                    ----------
                    kilometers

                    the miles do not divide out so this is the wrong way.

                    If you get your dimensions to work out, the calculation is easy.

                    Comment

                    • paradox6996
                      New Member
                      • Jul 2007
                      • 21

                      #11
                      ok sweet this worked perfect:

                      cout.setf(ios:: fixed);
                      cout.setf(ios:: showpoint);
                      cout.precision( 14);

                      Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000)

                      but is their a way to make the precision work for the above line, and not for this one:

                      Total_Boxes = 35273.92 / Cereal_Weight.

                      Oh and for anyone that wanted to know doing this:

                      Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000)

                      is the same as doing this x * 2.83495231(1 ounce in metric tons) * 10^-5, but since you can't use scientific notation in c++ I think. you gotta do it as above.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by paradox6996
                        ok sweet this worked perfect:

                        cout.setf(ios:: fixed);
                        cout.setf(ios:: showpoint);
                        cout.precision( 14);

                        Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000)

                        but is their a way to make the precision work for the above line, and not for this one:

                        Total_Boxes = 35273.92 / Cereal_Weight.

                        Oh and for anyone that wanted to know doing this:

                        Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000)

                        is the same as doing this x * 2.83495231(1 ounce in metric tons) * 10^-5, but since you can't use scientific notation in c++ I think. you gotta do it as above.
                        *ahem*, you can use scientific number notation: 2.83495231e-5
                        The 'e' (or 'E') represents 'times 10 raised to the power'.

                        kind regards,

                        Jos

                        Comment

                        Working...