#include <iostream>
#include <cstdlib>
using namespace std;
double amountSold;
double galSold;
double price;
int start;
int pump;


int main()
{
	price = 2.85;
	galSold = 0;
	pump = 1;
        while(pump==1)
		{
	cout << "Sales Total " << amountSold << endl; 
		cout<< endl;
cout << "Gallons Of Gas Sold " <<galSold;
cout<< endl;
cout << endl;
cout << "Price per Gallon $ "<<price;
cout << endl;
	cout << endl;
	cout<< "Start Pump? Enter 1 to start and -1 to end " <<endl;
	cin>> start;
		if (start == 1)
		{
   for (amountSold=0; amountSold<20.00; amountSold=amountSold+0.01)
	  
   {
	   
	  galSold = amountSold/price;
system("CLS");
        cout << "Sales Total " << amountSold << endl; 
		cout<< endl;
cout << "Gallons Of Gas Sold " <<galSold;
cout<< endl;
cout << endl;
cout << "Price per Gallon $ "<<price;
cout << endl;
	cout << endl;	

}// end of for loop
		}//end of if
		else 
		{
			cout<< "Start Pump? " <<endl;
		cin>> start;
		} // end of else
		system("CLS");
		}
}// end of int main

I am trying to program a very basic gas pump for my intro to C++ class. Unfortunently my teacher was an idiot and i ended up teaching myself most of it. The two issues i cant seem to figure out are how to accept a user input to start and stop the gas pumping loop and how to pause the loop at the start so that it appears to "zero out" before pumping gas. Any help is welcome and if you see some mistakes other than that in the code feel free to comment. I am always open to suggestions and learning.

Thanks an Advance!
-David

One solution would be to add an else if and if start == -1 then change pump to something other than 1.

Nothing should happen except the user prompts showing up until user enters a value for start at the cin >> start level.

In line 18 the values of the variable being sent to screen hasn't been initialized so the output will be junk.

i have the same type of program i am working on and got as far as you did...but for my program i need to use the rand function. the user only inputs the number of customers and then the program randomly generates the number of gallons and calculates price for each customer. The price per gallon of gas is constant. Anyone have info on rand and how to use it?

One solution would be to add an else if and if start == -1 then change pump to something other than 1.

Nothing should happen except the user prompts showing up until user enters a value for start at the cin >> start level.

In line 18 the values of the variable being sent to screen hasn't been initialized so the output will be junk.

i apreciate the response, i agree witht he variable in line 18. That will be fixed ASAP!
I am trying to figure out what your saying in the first to lines though, is their any way you could physically write out the code real quick for it? Or try explaining it a different way?

Thanks again!
-David

PS. I would apreciate it if no else tried to threadjack me...Thanks!

i cant seem to figure out are how to accept a user input to start and stop the gas pumping loop and how to pause the loop at the start so that it appears to "zero out" before pumping gas.

Aren't you supposed to enter the amount of gas to be purchased?

i have the same type of program i am working on and got as far as you did...but for my program i need to use the rand function. the user only inputs the number of customers and then the program randomly generates the number of gallons and calculates price for each customer. The price per gallon of gas is constant. Anyone have info on rand and how to use it?

What does you book say about rand() ? Look in the index.

PS. I would apreciate it if no else tried to threadjack me...Thanks!

Yes. this is frowned upon here.

Aren't you supposed to enter the amount of gas to be purchased?

What does you book say about rand() ? Look in the index.

Yes. this is frowned upon here.

the max limit i have set in the for loop was a "max purchase" assuming you use like debit card. If you were to buy gas with some sort of plastic, they set aside a certain amount of money ie. 75$.

I was just trying to figure out how someone could start and stop the loop when they felt so compelled.

while(pump == 1)
{
    //program will pause here until input entered
    cin >> start;
    if(start == 1) 
       //start pump
    else if(start == -1)
       //assign 666 to pump, this will stop the while loop, since pump no longer equals 1
    else 
       //tell user to enter only 1 or -1, nothing else
}

the max limit i have set in the for loop was a "max purchase" assuming you use like debit card. If you were to buy gas with some sort of plastic, they set aside a certain amount of money ie. 75$.

I was just trying to figure out how someone could start and stop the loop when they felt so compelled.

You really can't. C++ is not designed to 'stop when a key is entered' which it sounds like you're asking about.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.