Division Help

What I'm wanting this to do is take the amount that I enter for litter and and multiply it by .264179 and then divide that number into whatever I enter for mile. For some reason I keep getting a result of 0.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() 
{
    
    double litter;
    double mile;
    double gallon;
    int mpg = mile / (litter * .264179);
    
    cout << "Enter the amount of gasoline in litters: ";
   cin>> litter;
   cout << "Enter the number of miles traveled: ";
   cin>> mile;
   cout << "The gas mileage of this travel is " <<  mpg;
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
You should be doing the calculation after you receive the input from the user.
That did the trick. It kept giving me a whole number so I changed int to double and it fixed the problem. Now all I need to do is add a do-while loop so that it will ask the user if they want to run the program again. Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main() 
{
    
    double litter;
    double mile;
    double gallon;
    
    
    cout << "Enter the amount of gasoline in litters: ";
   cin>> litter;
   cout << "Enter the number of miles traveled: ";
   cin>> mile;
   double mpg = mile / (litter * .264179);
   cout << "The gas mileage of this travel is " <<  mpg << endl;
  
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
So this is where I sit in the current process. I want it to re-run the program if repeat = 1 and to end it if repeat = 0. I feel like Im pretty close but I can't put my finger on what the problem is.

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
int main() 
{
    do
  {
    double litter;
    double mile;
    double gallon;
    int repeat;
  

   cout << "Enter the amount of gasoline in litters: ";
   cin>> litter;
   cout << "Enter the number of miles traveled: ";
   cin>> mile;
   double mpg = mile / (litter * .264179);
   cout << "The gas mileage of this travel is " <<  mpg << endl;
   cout << "Do you want to run this program again(yes=1, no=0)?";
   cin >> repeat; 
   }
   
   while ( repeat == 1);
   
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


EDIT: changed it to the code below and it seems to work. Thanks for the help.
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
int main() 
{
   
    double litter;
    double mile;
    
    int repeat;
   
   do
  {

   cout << "Enter the amount of gasoline in litters: ";
   cin>> litter;
   cout << "Enter the number of miles traveled: ";
   cin>> mile;
   double mpg = mile / (litter * .264179);
   cout << "The gas mileage of this travel is " <<  mpg << endl;
   cout << "Do you want to run this program again(yes=1, no=0)?";
   cin >> repeat; 
   }
   
   while(repeat==1);
   system("PAUSE");
    return 0;
}
Last edited on
Topic archived. No new replies allowed.