cin>> on a for loop

My output is coming out funny suggestion how to fix plz?

[code]
for (counter=0; counter<3; counter++)
{
cout << " Price per gallon at the gas station ";
cin >> pergal;
}
return 0;
}
[code/]
You need an int variable I believe. You have a cin >> pergal; but no place to store that information. Then if you want to output it you need another extraction. So after you declare an int variable for pergal,

cout<< "Price per gallon at gas station";
cin >> pergal;
cout << "The price is: " << pergal << "for every gallon" <<endl;
Here is an example in case you want it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
	int pergal;
	cin >> pergal;
	//input the price here, you can throw in a message if you want...
	for (int counter = 0; counter < 3; counter++) {
		cout << "Price per gallon at a gas station: \n";
		cout << "The price is " << pergal << " for a gallon \n";
	}
	system("PAUSE");
    return 0;
}
The other problem is you're asking for the price 4 times and storing it in the same location.
Each iteration through the loop overlays the previous value.

Your closing code tag should be
[/code]
.
Ok, thanks I thinks I found the error
[code]
for (counter=0; counter<3; counter++)
{
cout << " Price per gallon at the gas station ";
cin >> pergal;

pergal+=avg;
}
/[code]
closed account (48T7M4Gy)
I knew it was something like that.
Topic archived. No new replies allowed.