hello there, I'm working on a program for my class
I'm using struct and functions
well here is my problem
I made a function that reads from a txt file and that I want to use the variables taken from it for my whole program, I kept on getting errors for the past two days...
been stuck in opening files for a long time, any kind of help is appreciated
string OPEN()
{
card1.ID+=".txt"; //file won't open unless there is a .txt at the end
ifstream myfile;
myfile.open(card1.ID.c_str());
if (myfile.is_open())
{
cout<<"\nFile is open";
}
else
{
cout<<"\nNo such Account exists";
Sleep(2000);
system("cls");
LOGIN();
}
for(int i=0;i<2;i++) //reading values
{
if (i==0)
getline(myfile, card1.name);
elseif(i==1)
getline(myfile, card1.password);
elsereturn card1.name, card1.password;
}
}
i have a txt with filename 192168 containg these data
John S. Smith //name
notebook //password
5000 // number
this window pos up and displays the message
Unhandled exception at 0x5AE14F98 (msvcr110d.dll) in ATM FINAL.exe: 0xC0000005: Access violation reading location 0xCCCCCCC0.
One problem I see in your OPEN function inside the for loop, the last iteration you want to read in the ID, not return. Since card1 is global you don't need to return anything from this function.
works fine now, changed OPEN() from string to void, thank you binarybob for reminding me that they are global variables...
for a moment there I misread your name to binaryboob ;p