This problem is geting disgusting.
fonzie (11)Aug 21, 2008 at 10:55pm UTC
I've been having a reccuring problem with my code for around a month now where the code compiles fine, however, the file is not created. While at my friends place, (using dev bloodshed C++ v.4 if it matters) He points out to me that I put "fstream" rather than "ofstream". Success! My code finaly does what it should. However, when I get home (dev bloodshed C++ v.5) and add the aforementioned "ofstream" it does NOT create the file. Here's my code. PS: The function "readInCustomer" is my begining to experiment with reading in the file, I'm not 100% on how this works, however, if anybody has any better ideas I'm open to them.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct customerDataBase{ //basic customer information struct
string lastName;
string firstName;
string addr1;
string addr2;
string eMail;
} Customer;
const int maxCustomerNumber = 1;
customerDataBase customerArray[maxCustomerNumber];
int k;
void TheScreen(customerDataBase customerInfo){
//this SHOULD print out all the information in the array
cout<<"Last name: " <<customerInfo.lastName<<endl;
cout<<"First name: " <<customerInfo.firstName<<endl;
cout<<"Addr1: " <<customerInfo.addr1<<endl;
cout<<"Addr2: " <<customerInfo.addr2<<endl;
cout<<"E-Mail: " <<customerInfo.eMail<<endl;
}
void readInCustomer(){
ifstream CustomerFile;
CustomerFile.open("C:\\DoNotDelete.txt" );
system ("PAUSE" );
CustomerFile>>customerArray[k].lastName;
system ("PAUSE" );
CustomerFile>>customerArray[k].firstName;
system ("PAUSE" );
CustomerFile>>customerArray[k].addr1;
system ("PAUSE" );
CustomerFile>>customerArray[k].addr2;
system ("PAUSE" );
CustomerFile>>customerArray[k].eMail;
system ("PAUSE" );
TheScreen(customerArray[k]);
system ("PAUSE" );
}
void addNewCustomer(){
ofstream CustomerFile;
CustomerFile.open("C:\\DoNotDelete.txt" );
TheScreen(customerArray[k]);
cout<<endl<<"please enter last name: " ;
cin>>customerArray[k].lastName;
CustomerFile<<customerArray[k].lastName<<"," ;
system ("cls" );
TheScreen(customerArray[k]);
cout<<endl<<"please enter first name: " ;
cin>>customerArray[k].firstName;
CustomerFile<<customerArray[k].firstName<<"," ;
system ("cls" );
TheScreen(customerArray[k]);
cout<<endl<<"please address name: " ;
cin>>customerArray[k].addr1;
CustomerFile<<customerArray[k].addr1<<"," ;
system ("cls" );
TheScreen(customerArray[k]);
cout<<endl<<"please other address information name: " ;
cin>>customerArray[k].addr2;
CustomerFile<<customerArray[k].addr2<<"," ;
system ("cls" );
TheScreen(customerArray[k]);
cout<<endl<<"please enter e-mail name: " ;
cin>>customerArray[k].eMail;
CustomerFile<<customerArray[k].eMail<<"," ;
system ("cls" );
TheScreen(customerArray[k]);
CustomerFile.close();
}
int main(int argc, char *argv[])
{
readInCustomer();
addNewCustomer();
system("PAUSE" );
return EXIT_SUCCESS;
}
Duoas (1087)Aug 21, 2008 at 10:55pm UTC
If you wish to overwrite the file, make sure line 50 reads
CustomerFile.open("C:\\DoNotDelete.txt" , ios_base::trunc ); Between lines 50 and 51 you should check that the file was actually opened and writable:
if (!CustomerFile) fooey(); (It may not be if you don't have high-enough permissions.) I hope all those system("PAUSE")s and system("CLS")s are temporary... Hope this helps.
fonzie (11)Aug 21, 2008 at 10:55pm UTC
Thanks a bunch, I'll try it out. And this isn't a professional program, just a little thing of my own, so I'm not to worried about systempause and systemcls, but I'll do my best to remove them.
Duoas (1087)Aug 21, 2008 at 10:55pm UTC
I figured that was the case. Good luck!
This topic is archived - New replies not allowed.