My if statements and while loops aren't functioning

Pages: 12
@Maryrocks thank you, that was very easy to understand! I ended up solving it (while keeping the fstream and not using vectors) last night. But for the other route that my code will follow I want to add vectors so ill try with the one you sent me. Thank you so much. Vectors has always been so daunting to me but your code was so simple it really ease my worries.

This is the fixed code (with fstream and no vector)

relevant code from login class
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
class login : public user {
	public:
		int checkusername(){

				//open file to read
		usernames.open("names.txt");
		/*
		if (!usernames){
			cout<<"file not open";
			return 0;
		} */

		cout << "please enter your username: ";
		cin >> username; //userinput

		while (usernames >> temp) {
			//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');   
			if (temp.compare(username) == 0) {
				cout << "found";
				usernames.close();
				return 1;
			}
		}
		usernames.close();
		//closing the file
		return 0;
		}


main
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
int main() {
user u;   
login verify; 
cout<<"\nAre you a new user? (y/n)";
cin>>u.choice1;
	if (u.choice1 == 'y') 
	{
		int attempts, success;
		//checks for success of username match
		//user is given 3 attempts
		for (attempts = 1; attempts < 4; attempts++) {
			success = verify.checkusername();
			if (success == 1) { 
				break; // it will jump to the check pass thing
			}
		}
		//if inspite of those 3 attemps user still has not succeeded if(success == 0 ) will provide a new route
		if (success == 0) // they have done all 3 attempts and still success is 0
		{
			//code for what happens if user can't have all 3 tries
		}
		verify.checkpassword();
		verify.creatnewpass();
	}

	else if (u.choice1 == 'n') {
		cout<<"cnoij";
	}
	else {
		cout<<"error";
	}
return 0;
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12