-
while loop help
hey guys i got a big prob, this program works...sorta but im having troubles getting this loop to work right, i suck at while loops so i need help, heres the code
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void autocheck();
void main()
{
int sellerID;
int autoID;
double price;
int c;
ofstream fileout;
while(c != 'y'){
cout<<"Please Enter Seller ID : ";
cin>>sellerID;
cin.get();
system("cls");
cout<<"Please Enter Auto ID : ";
cin>>autoID;
cin.get();
cout<<"Pease Enter Price : $";
cin>>price;
cin.get();
fileout.open("C:/sellerID.txt",ios::app);
fileout<<sellerID<<" ";
fileout.close();
fileout.open("C:/autoID.txt",ios::app);
fileout<<"Seller's ID# "<<sellerID<<endl;
fileout<<"Auto ID# "<<autoID<<endl;
fileout<<"Price Sold "<<price<<endl<<endl;
cout<<"Would You Like To Make Another Entry? y/n : ";
cin>>c;}
}
the problem is at the end, when i try and loop it so that the user can enter more "data", when i go to enter 'y' so that it can start over the program crashes, im almost posative that its the loop, but anyways i really need help on this one guys, thanks!
-
1. STOP USING VOID MAIN
2. Indent
3. You need to initialize c to some value
4. You logic for while loop is messed up, try this:
5. You need to use cin.get() here:
Code:
c = cin.get();
cin.ignore(80, '\n');
Now it works.