Simple homework

I have an assignment where I have to
A metric ton is 35,27.92 ounces. Write a program that will read the weight of a package breakfast cereal in ounces and output the weight in metric tons as well as the number of boxes needed to yield one metric ton of cereal.

so I have it mostly done in a simple format(me being mostly a noob ... except at the hello world I totally owned that lol) So far you enter a value it error checks it if there isn't a error continue a Ton can be found by dividing by 16 then by 2000 so it does that but when I type in 1 it says 0



Thanks in advance,
Dr Chill
P.S. I use dev C++ and here is my source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string input = "";
int OZ;
int TOT;

int main (int argc, char *argv[])
{
    while (true) {
          cout << "How much ounces does your cereal weigh? ";
          getline(cin, input);
          // This code converts from string to number safely.
          stringstream myStream(input);
          if (myStream >> OZ)
          break;
          cout << "Invalid number" << endl;
    }
    cout << "You entered: " << OZ << endl;
    TOT = OZ/16/2000;
    cout << TOT;
    cin.get(); //Pause at the end 
    return 0;
}
TOT is an int type, just like an integer in mathematics... A double is a floating-point type that you might find useful.
No TOT is the total amount after you divide the ounces what I need i for it to display decimals
Well, TOT is an int, since that is what YOU set it to. Change it to a double for decimals.
Here's the problem. When you use
 
TOT = OZ/16/2000;

You are using integer division. In integer division, the remainder is always thrown away, so if you do 5/4, the answer will be 1. if you do 16/20, the answer will be 0. Since you are entering 1, you have 1/16, which is 0.

For your TOT to work, and to display decimals, you need to do floating point division.

First, TOT must be either a float or double variable(I prefer double).
 
double TOT;

Secondly, if you want to divide two numbers, and get a floating point result, at least one of the two numbers need to be floating point. since you are dividing twice on the same line, both of them need to be floating point numbers.
 
TOT = OZ/16.0/2000.0

Last edited on
Thanks very much (very very much) but when I run it it says :3.125e-005 is there any way to make it say the number with the zeros?
Topic archived. No new replies allowed.