Segmentation fault (core dumped)

I'm doing a login system via OOP.
here is my code:

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
header:
#include<string>
using namespace std;

class Login{
	private:
		int option;
		string us,ps,psw;
		
	public:
		string login();
		string setAcc(string us, string psw);
		string setLogin(string us, string ps, string psw);
};


variable:
#include"Login.h"
#include<iostream>
#include<fstream>

string Login::login(){
	using namespace std;
	cout<<"Welcome to Car Renting system"<<endl;
	cout<<"Please choose what you want to do."<<endl;
	cout<<"1 for Login."<<"    "<<"2 for register(if you don't have a account)"<<endl;
	cin>>option;
}

string Login::setAcc(string us, string ps){
	
	using namespace std;
		
  	cout<<"Please key in a username."<<endl;
	cin>>us;
		
	cout<<"Please key in a password."<<endl;
	cin>>ps;
		
	ofstream myfile;
 	myfile.open ((us+".txt").c_str());
 	myfile<< us<<endl;
 	myfile<< ps<<endl;
  	myfile.close();
  	
	 return login();
}

string  Login::setLogin(string us, string ps, string psw){
	
	using namespace std;
	
	cout<<"Please key in your username."<<endl;
	cin>>us;
		 
	cout<<"Please key in your password."<<endl;
	cin>>psw;
		 
	ifstream myfile ((us+".txt").c_str());
	getline (myfile,us) ;
	getline (myfile,ps);
		 	
      	if ( psw ==ps){
			cout<<"Welcome"<<endl;
		}	
  
  		else{
  			cout<<"You have entered a wrong username or password."<<endl;
  			
  		return login();
		}
}

main function:

#include<iostream>
#include"Login.h"
#include<string>

using namespace std;



int main(){
	
	int option;
	string us, ps, psw;
	
	Login Jason;
	
	Jason.login();
		
	if(option==1){
		Jason.setLogin(us, ps, psw);	
	}
	
	else{
		Jason.setAcc(us, ps);
	}
	
	return 0;
}


where was the problem that caused segmentation fault(core dumped) error.
Last edited on
In login() you forgot to return anything.

In setLogin() you forgot to return something when the keys match.

In main() you are using the option variable without initializing it.
Last edited on
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
header:
#include<string>
using namespace std;

class Login{
	private:
		int option;
		string us,ps,psw;
		
	public:
		int getLogin(int option);
		string setAcc(string us, string psw);
		string setLogin(string us, string ps, string psw);
};


variable:
#include"Login.h"
#include<iostream>
#include<fstream>

int Login::getLogin(int option){
	cout<<"Welcome to Car Renting system"<<endl;
	cout<<"Please choose what you want to do."<<endl;
	cout<<"1 for Login."<<"    "<<"2 for register(if you don't have a account)"<<endl;
	cin>>option;
	return option;	
}
	


string Login::setAcc(string us, string ps){
	
	using namespace std;
		
  	cout<<"Please key in a username."<<endl;
	cin>>us;
		
	cout<<"Please key in a password."<<endl;
	cin>>ps;
		
	ofstream myfile;
 	myfile.open ((us+".txt").c_str());
 	myfile<< us<<endl;
 	myfile<< ps<<endl;
  	myfile.close();
  	
	return getLogin(option);
}

string  Login::setLogin(string us, string ps, string psw){
	
	using namespace std;
	
	cout<<"Please key in your username."<<endl;
	cin>>us;
		 
	cout<<"Please key in your password."<<endl;
	cin>>psw;
		 
	ifstream myfile ((us+".txt").c_str());
	getline (myfile,us) ;
	getline (myfile,ps);
		 	
      	if ( psw ==ps){
			cout<<"Welcome"<<endl;
			
		return 0;
		}	
  
  		else{
  			cout<<"You have entered a wrong username or password."<<endl;
  			
  		return getLogin(option);
		}
}

main function:

#include<iostream>
#include"Login.h"
#include<string>

using namespace std;

int main(){
	
	int option;
	string us, ps, psw;
	
	Login Jason;
	
	Jason.getLogin(option);
	
	if(option==1){
		Jason.setLogin(us, ps, psw);	
	}
	
	else{
		Jason.setAcc(us, ps);
	}
	
return 0;
}


i have error that cant convert int into string. i knw the reason. but how should i return back to the function int Login::getLogin(int option) so the user can login again after register or entered wrong username/ password......
Topic archived. No new replies allowed.