structs and functions

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


headers
1
2
3
4
5
6
#include<iostream>
#include<string>
#include<fstream>
#include<conio.h>
#include<windows.h>
using namespace std;


my struct
1
2
3
4
5
6
7
struct CARD
{
string name;
string password;
string ID;

}card1, card2;


function prototypes
1
2
3
4
5
string LOGIN();
//void MENU();
int WITHDRAW();
int DEPOSIT();
string OPEN();


main function
1
2
3
4
LOGIN();
OPEN();
cout<<card1.ID<<" "<<card1.name<<" "<<card1.password;//just testing if values return
//MENU(); 


getting main data
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
string LOGIN()
{
	
	char c = ' ';
    string pass = "";
	cout<<"Enter ID: ";
	cin>>card1.ID;
	cout<<"Enter password: ";
	

	 while((c=getch())!=13) //password masking
           {
            if(c==8)
             {
                     if(pass.length()!=0)
                        {
                            if(true)
                              cout <<"\b \b";
                              pass.resize(pass.length()-1);
                         }
              }
            else if(c==0) 
              {
             getch(); 
             continue;
               }
           else
              {
             pass+=c;
             if(true)
                 cout <<'*';
              }
             }
			card1.password=pass;
	return card1.ID, card1.password;
}


the problem
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
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);
		else if(i==1)
			getline(myfile, card1.password);
		else
			return 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.
Last edited on
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.

change
1
2
else
  return card1.name, card1.password;

to
1
2
else
  getline(myfile, card1.ID);
thanks for replying
but this window keeps on popping out

Unhandled exception at 0x5AE14F98 (msvcr110d.dll) in ATM FINAL.exe: 0xC0000005: Access violation reading location 0xCCCCCCC0.
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

also removed the else
Last edited on
One other thing to point out, a function can only return one string.
The return at the end of LOGIN

return card1.ID, card1.password;

only returns the last string, in this case the password. card1.ID does nothing.
thank you very much

edit: changed it to void, still works fine
Last edited on
Topic archived. No new replies allowed.