-
Can't write to file
I can open the file, but all I get is the run window that says press any key to continue and when I press a key it closes the window.
Here is my code
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <algorithm>
int getNumbers(int &);
using namespace std;
int main()
{
//declare Variables
string clerkid = "";
string name = "";
string address = "";
string city = "";
string state = "";
string hyphfill = "";
int zip = 0;
int ssn = 0;
int visa = 0;
int pin = 0;
//opens file CCApp.txt
ifstream inFile;
inFile.open("WACKOFILE.dat", ios::in);
if(inFile.is_open())
{
getline(inFile,clerkid,'#');
while(!inFile.eof())
{
inFile>>ssn;
inFile.ignore(1);
getline(inFile,name,'#');
getline(inFile,address,'#');
getline(inFile,city,'#');
getline(inFile,state,'#');
inFile>>zip;
inFile.ignore(1);
inFile>>pin;
inFile.ignore(1);
inFile>>visa;
inFile.ignore(2);
cout<<left<<setw(25)<<setfill(' ')<<"NAME"<<" "<<setw(25)<<"ADDRESS"<<" "<<left<<setw(16)<<"CITY"<<" "<<setw(2)<<
left<<"ST"<<" "<<setw(5)<<left<<"ZIP";
cout<<left<<setw(25)<<setfill('-')<<hyphfill<<" "<<setw(25)<<setfill('-')<<hyphfill<<" "<<left<<setw(16)<<setfill('-')<<hyphfill<<" "<<setw(2)<<left<<setfill('-')<<hyphfill<<" "<<
setw(5)<<left<<setfill('-')<<hyphfill;
cout<<left<<setw(25)<<setfill(' ')<<name<<" "<<setw(25)<<address<<" "<<left<<setw(16)<<city<<" "<<setw(2)<<left<<state<<" "<<setw(5)<<right<<
setfill('0')<<zip<<endl;
cout<<setw(7)<<left<<"CLERKID"<<" "<<setw(9)<<left<<setfill(' ')<<"SSN"<<" "<<setw(4)<<left<<setfill(' ')<<"PIN"<<" "<<setw(5)<<left<<setfill(' ')<<"VISA"<<" "<<endl;
cout<<setw(7)<<left<<setfill('-')<<hyphfill<<" "<<setw(9)<<left<<setfill('-')<<hyphfill<<" "<<setw(4)<<left<<setfill('-')<<hyphfill<<" "<<setw(5)<<left<<setfill('-')<<hyphfill<<" "<<endl;
cout << setw(7) << left << setfill(' ') << clerkid
<<" "<<setw(9)<<right<<setfill('0')<<ssn<<" "<<setw(4)<<left<<setfill(' ')<<pin<<" "<<setw(5)<<left<<setfill(' ')<<visa<<" "<<endl;
cout<<endl;
cout<<endl;
getline(inFile,clerkid,'#');
}
}
else
{cout<<"File not opened"<<endl;}
inFile.close();
cout << endl;
return 0;
}//end main
-
Learning how to debug a program is as important as learning how to "write" a program. Run time bugs seem harder to find than compile time bugs. I'd suggest learning how to use your debugger (if you have one) or do what I do and generously sprinkle cout statements throughout your code to see if you can track down the error. For example:
Code:
if(inFile.is_open())
{
cout << "file open" << endl;
cin.get();
will confirm that the file is open and force the program to wait at that point until you can think about the results.
Maybe it's as simple as doing this:
cout << setf(ios::left)
instead of
cout << left
Who knows.
-
I am wondering if it has anything to do with the program that saves the file. Here is the code for that program
[CODE] #include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<algorithm>
int getpin(int & );
int i;
using namespace std;
int main()
{
//declare variables
string clerkId ="";
int ssn =0;
string name ="";
string address ="";
string city ="";
string state ="";
int zip =0;
int pin = 0;
int visa = 0;
//enter input itesm to a file
ofstream outfile;
outfile.open("WACKOFILE.dat", ios::out);
if(outfile.is_open())
{
//Clerk is to enter ID for login Information
cout<<"Enter ClerkId: ";
getline(cin, clerkId);
//This will Loop Through Program and then Exit Loop
//Once Clerk-ID id Null
while(clerkId != "xx")
{
//Get Name Address City State Zip Social Security Number
cout <<"Enter SSN: ";
cin>>ssn;
cin.ignore(1);
cout <<"Enter Name: ";
getline(cin, name);
cout <<"Enter Address: ";
getline(cin, address);
cout <<"Enter City: ";
getline(cin, city);
cout <<"Enter State: ";
getline(cin, state);
cout <<"Enter Zip: ";
cin>>zip;
pin = getpin(visa);
cin.ignore(1);
cin.ignore(1);
// Format Title Lines for Name, Address, City, State, and Zip
cout<<setw(25)<<left<<"Name"<<" "<<setw(25)<<left<<"Address"<<" "
<<setw(16)<<left<<"City"<<" "<<setw(2)<<"ST"<<" "<<setw(5)<<left<<"Zip"<<endl;
//Format Display Output
cout<<setw(25)<<left<<name<<" "<<setw(25)<<left<<address<<" "
<<setw(16)<<left<<city<<" "<<setw(2)<<state<<" "<<setw(5)<<setfill('0')<<right<<zip<<endl;
cout<<setw(2)<<left<<"ClerkID"<<" "<<setw(9)<<setfill(' ')<<left<<"SSN"<<endl;
cout<<setw(2)<<left<<clerkId<<" "<<setw(9)<<left<<ssn<<endl;
cout<<"Enter ClerkId: ";
getline(cin, clerkId);
}
}
else
{cout<<"File not Open"<<endl;}
outfile.close();
return 0;
}
int getpin(int & visa)
{
srand(time(NULL));
visa = 10000 + rand()%(99999-10000 + 1);
return 10000 + rand()%(9999-1000 + 1);
}
[CODE]
Any and all help please, I am going nuts with this one.