Push in the right direction? Thanks!

My assignment is to create a program that asks the user for the number of barrels available, the number of gallons removed, and then to output the number of barrels still available. So I have to allow for conversion, but I also have to produce a warning when the available supply is less than 10 percent of the tank's 80000 barrel capacity. So below 8,000 barrels.

Currently, I believe I have it set up to do all of the following, but I have not included a loop. I'm not sure how to proceed. If while statements? or for loop? or sentinel loop? Any advice/help would be greatly appreciated!

Here is my current code:

//Setup standard environment
#include <iostream>
using namespace std;
void title ()
{
// Output program name
cout << endl; // Setups two blank lines before program name
cout << endl;
cout<< " Assignment 3 " <<endl;
cout <<endl;
cout <<endl;
}
int main ()
{
//Variable declarations
double barrel_input, gallon_input, gallon_output, available_gallons, available_barrels;
title ();
do
{
cout << "Please enter the amount of gasoline currently in the tank (barrels) :";
cin >> barrel_input;
gallon_input= barrel_input * 42;
cout << "Please enter the number of gallons pumped into a tanker: ";
cin >> gallon_output;
available_gallons= gallon_input- gallon_output;
available_barrels= available_gallons/ 42;
if (available_barrels < 8000.00)
{
cout << "***WARNING***";
cout <<endl;
cout << "Available supply is less than 10 percent of tanks 80,000.00-barrel capacity.";
cout <<endl;
}
else
cout<< endl;
}
return 0;
}
That requires code tags.
You have a do statement, which requires a while. You cannot create a do{}for loop.
What do you intend to accomplish with the loop? This looks like a simple input-process-output problem.
closed account (jLNv0pDG)
Use [code] tags.

Do you want to loop the entire program (setting quantity and pumping to tanker) or just the bit where you keep removing gasoline from the tank?
Last edited on
Thank you all. I believe I have it working EXECPT

I get this error :

1>LINK : fatal error LNK1168: cannot open C:\Users\Nbe4u\Documents\Visual Studio 2008\Projects\Assignment 3\Debug\Assignment 3.exe for writing

any suggestions?
Maybe the paths are off... after all that is a linktime error. Did you set the executable to read only or something?
I'm not sure what that means tummychow? What is the executable?
Nevermind. I just opened a new project, and I have it working.

Is there any code to clear the page before it loops back to the do while statement? Instead of having it just continue down?
http://www.cplusplus.com/forum/articles/10515/
The only good ways are os-specific. Duoas designed it to be copy-paste code, so don't try to modify it or anything. Just don't even consider using conio and you should be fine.
Last edited on
Perfect. I have it working. Is there anything I can do for it to only do that after the first round? I don't want it to clear screen at the beginning of my do statement...

I want it to run through once, and if they answer they are not done,

Then it will go back to the beginning with a cleared screen.
Clear screen at the end of each iteration if they choose to repeat. Whatever condition you have for repetition, use it again.
Last edited on
Since the clear screen isn't working out, I'm looking for an alternative. Essentially my question is, how can you add information for iterations >1 that isn't included in the first iteration. I want to add a bunch of lines <<endl; before the second run begins. That way users will be able to differentiate between iterations.

Here is my current code & thank you!

//Setup standard environment
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void title ()
{
// Output program name
cout << endl <<endl;
cout<< " Assignment 3 " <<endl;
}
int main ()
{
//Variable declarations
double barrel_input, gallon_input, gallon_output, barrel_output, available_gallons, available_barrels; //Defined all used variable
char sentinel; //sentinel=n continue
//sentinel=y done
// Display title
title ();
cout <<endl <<endl;
//Output statement
cout << "Please enter the number of barrels currently in tank: ";
//Input number
cin >> barrel_input;
//Output current availability
cout << barrel_input <<" barrels are available." <<endl <<endl;
//Beginning do while statement
do
{
cout <<endl;
//Conversion of barrels to gallons
gallon_input= barrel_input * 42;
//Output statement
cout << "Please enter the number of gallons pumped into a tanker: ";
//Input number
cin >> gallon_output;
//Conversion of gallons to barrels
barrel_output= gallon_output/ 42;
//Calcuation of current gallons available
available_gallons= gallon_input- gallon_output;
//Conversion of gallons to barrels
available_barrels= available_gallons/ 42;
cout <<endl <<endl <<"After removal of " << gallon_output << " gallons (" << barrel_output << " barrels), only " << available_barrels << " barrels are left.";
cout << endl <<endl;
if (available_barrels < 8000.00)
{
cout << " ***WARNING*** " <<endl <<endl;
cout << "Available supply is less than 10 percent of tank's 80,000.00-barrel capacity." <<endl <<endl;
}
else
cout<< endl;
//Output continue statement
cout << "Are you done? Please enter 'Y' for yes or 'N' for no: ";
// Input sentinel
cin >> sentinel;
}
while (sentinel == 'n' || sentinel =='N');
cout <<"Assignment 3 is complete." <<endl;
return 0;
}
Create a boolean and initialize to false. Every time in the loop, if it's false, change it to true. If it's true, the first iteration has already finished. Wham. I'm sure there are more elegant solutions but that should work fine.
Honestly you should ignore clearing screen all together. Just put a spacer between iterations and you should be fine.
Last edited on
The spacer idea is so much better. THANK you so much for all your help. I think my assignment is now complete :)

Topic archived. No new replies allowed.