Page 1 of 1

Reputation:

# do while never ends!

Posted 26 July 2010 - 11:14 AM

```//Assignment 2 Question 5
#include <iostream>
using namespace std;

float litres = 0, price = 0, priceAcepted = 0, distanceTravell = 0, totalLitres = 0, totalAmount= 0, totalDistance = 0;
const int NR_FILL_UPS = 6;

void inputAndValidate (float & litres, float & priceAcepted, float & distanceTravell)
{
do
{
//Get litres
cout << "How many litres put in (0 zero to end): " << endl;
cin >> litres;

//Get price
cout << "Price of fuel: (Must be more than or equal to R4 per litre): R";
cin >> priceAcepted;
if(priceAcepted >= 4)
price == priceAcepted;
else
cout << "Please enter a valid price: R"; << endl;
cin >> priceAcepted;
price == priceAcepted;
//Get distance
cout << "Distance travelled: " << endl;
cin >> distanceTravell;
}
while (litres != 0);

cout << endl << endl;

}
void updateTotals (float litres, float price, float distanceTravell, float totalLitres, float totalAmount, float totalDistance)
{
if (litres != 0)
totalLitres += litres;
if ( price != 0)
totalAmount += price;
if (distanceTravell != 0)
totalDistance += distanceTravell;
}

int main ()
{

cout.setf(ios::fixed);
cout.precision (2);

for (int i=0 ;i<=NR_FILL_UPS ;i++)
{
inputAndValidate (litres, price, distanceTravell);
updateTotals (litres, price, distanceTravell, totalLitres, totalAmount, totalDistance);
}
cout << "The average fuelconsumption was: " << totalLitres / NR_FILL_UPS << endl;
cout << "The average cost of feul per kilometer was: " << totalDistance * totalAmount / NR_FILL_UPS << endl;

system ("pause");
return 0;
}

```

Edited by macosxnerd101: Welcome to DIC! Please post your code using code tags, like so: .

Is This A Good Question/Topic? 0

## Replies To: do while never ends!

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12275
• Posts: 45,364
• Joined: 27-December 08

## Re: do while never ends!

Posted 26 July 2010 - 11:16 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
• Post the code you're having problems with (Done)
• Post the exact error you're receiving, if you are receiving one
• If no error explain what the code is doing versus what you want it to do
• Post your question in the body of your post, not the description field

### #3 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

## Re: do while never ends!

Posted 26 July 2010 - 11:38 AM

You set a variable type to float & then test if it's equal to a whole number?

### #4 Nakor

• Professional Lurker

Reputation: 448
• Posts: 1,504
• Joined: 28-April 09

## Re: do while never ends!

Posted 26 July 2010 - 12:13 PM

not part of your question but the line

```price == priceAcepted;
```

inside the while loop is supposed to be accomplishing what?

This post has been edited by Nakor: 26 July 2010 - 12:14 PM

### #5 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: do while never ends!

Posted 26 July 2010 - 02:58 PM

why not just use two different loops to check the input conditions
```    do{
cout << "How many litres put in (0 zero to end): " << endl;
cin >> litres;
}while (litres != 0);

do{
cout << "Price of fuel: (Must be more than or equal to R4 per litre): R";
cin >> priceAcepted;
}while(priceAccepted<4);

```

### #6 Hystaspes

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 09-May 10

## Re: do while never ends!

Posted 26 July 2010 - 03:07 PM

I don't know what is the problem that you are trying to solve so I can't help you that much. Post the question please.

All we have here is your code so my answer is based on your own code. I'm not an expert but these are the problems that I could find in your code:

1. mistakes in indentation and some syntax errors.
2. Using global variables when they are not needed at all; you should not use them unless they are really needed and you have no other choice.
3. You have variables of type "float" and they are initialized to integers.
4. "Never use names with all capital letter (e.g., BEGIN_TRANSACTION) because that's conventionally reserved for macros." Bjarne Stroustrup

I have changed your code a little bit but still it does not work properly which means your solution PROBABLY is not correct (maybe I am wrong). I think you should try again to solve the question. First try to change the code you already have.

```#include <iostream>
using namespace std;

void inputAndValidate( float &litres, float &priceAccepted, float &distanceTravel, float &price )
{
do
{
//Get litres
cout << "How many litres put in (0 zero to end): " << endl;
cin >> litres;

if ( litres == 0 )
{
break;
}

//Get price
cout << "Price of fuel: (Must be more than or equal to R4 per litre): R";
cin >> priceAccepted;

if ( priceAccepted >= 4 )
{
price = priceAccepted;
}
else
{
cout << "Please enter a valid price: R" << endl;
cin >> priceAccepted;
price = priceAccepted;
}

//Get distance
cout << "Distance travelled: " << endl;
cin >> distanceTravel;
} while (litres != 0);

cout << endl << endl;

return;
}

void updateTotals( float litres, float price, float distanceTravel, float &totalLitres, float &totalAmount, float &totalDistance )
{
if ( litres != 0 )
{
totalLitres += litres;
}

if ( price != 0 )
{
totalAmount += price;
}

if ( distanceTravel != 0 )
{
totalDistance += distanceTravel;
}

return;
}

int main ()
{
float litres = 0.0f, price = 0.0f, priceAccepted = 0.0f, distanceTravel = 0.0f, totalLitres = 0.0f, totalAmount= 0.0f, totalDistance = 0.0f;
const int nr_fill_ups = 6;

cout.setf(ios::fixed);
cout.precision(2);

for ( int i=0; i <= nr_fill_ups; i++ )
{
inputAndValidate( litres, priceAccepted, distanceTravel, price );
updateTotals( litres, price, distanceTravel, totalLitres, totalAmount, totalDistance );
}

cout << "The average fuelconsumption was: " << totalLitres / nr_fill_ups << endl;
cout << "The average cost of feul per kilometer was: " << (totalDistance * totalAmount) / nr_fill_ups << endl;

system ("pause");
return 0;
}

```

P.S.:
Some changes that I made are not necessary. For example you can just write
```float a = 0.0;

```

```float a = 0.0f;

```

or you can just write this
```float a = 0;

```

The compiler will do the type conversion automatically.

Another thing is the return statement in "void" functions, which is not necessary to write, but I always do

This post has been edited by Hystaspes: 26 July 2010 - 03:12 PM