My if statements and while loops aren't functioning

Pages: 12
Neither of my if loops are functioning. I keep being asked to enter the username. So the second prompt to enter username is functioning (I added jjj so I could tell). The comparison for temp==username does not work in both cases.

What I want to do here is to loop asking user for their username until it matches with temp. temp is related to an external file names.txt

names.txt
1
2
3
4
qwerty
yesyes
pop
yin


this is the relevant code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
usernames.open("names.txt");

		while(usernames>>temp){
			cout<<"please enter your username: ";
			cin>>username; //userinput

				if(temp==username){
					cout<<"found";
				}

					while(temp!=username){
						cout<<"please enter your usernamejjj: ";
						cin>>username; //userinput
					}
				}

		usernames.close();
		//closing the file 
Because you don't break out of the loop in line 13. Thus, the outer loop keeps repeating. It will repeat until you have entered all the usernames in the file.
How did you declare your temp and username variables?

std::string - good
char array - bad
@TheToaster

i already tried that. i wrote break; under cin>>username. I tried it again too just now and it still won't work...

@Salemc

both are strings.
My guess is that your pushing the whole text file into temp. Print out temp and see what it looks like.

Also I'm not entirely sure the result of >> temp will ever be false as long as the file is open. Kinda an unorthodox way to go about it. Probably better to open file, get the user input and then loop though the file line by line and do the comparison.

Like I said just a guess so take these words with a grain of salt.
Hello yin,

I was wondering how long it would take before you found the other problems.

See http://www.cplusplus.com/forum/beginner/270710/ for more code.

Based on your first code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	user u;
	std::string temp;

	std::cout << "\nAre you a new user? (y/n)";
	std::cin >> u.choice1;

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. Clears the input buffer.

	if (u.choice1 == 'y' || u.choice1 == 'Y')  // <--- Or if (std::tolower(u.choice1) == 'y'). Requires header file "<cctype>".
	{
		std::cout << "please enter your username: ";
		std::getline(std::cin, u.username); //userinput 

Following the formatted input, line 7, you need line 9 to clear the input buffer of the newline character before an unformatted input using "getline". Otherwise "getline" will read the "\n" from the input buffer and move on without waiting for any keyboard input.

When you open a file stream for input it is a must to check that it opened and is usable.
1
2
3
4
5
6
7
8
u.usernames.open("names.txt", std::ios::in);

if (!u.usernames)  // <--- Checks if file stream is open and usable.
{
	std::cout << "\n     File \"names.txt\" did not open!\n";

	return 1;
}

Since you defined the file stream as a "fstream" you need to tell the open statement if the stream is for "in", "out" or both. A "fstream" does not know what you want like "ifstream" and "ofstream" does. The "i" and "o" imply input and output.

The code from your other post was good it just needed a little adjusting.

Andy
@Andy

Hello Andy,

I was wondering how long it would take before you found the other problems.

this gave me a good chuckle!

I switched up my code a bit to make it more object-oriented.

so for context here's the full code (with the added adjustments from your response). Unfortunately it's still not working. So any help would be so appreciated. Also thank you for clarifying me of the importance of telling it if the stream is for in or out.

names.txt
1
2
3
4
qwerty
yesyes
pop
yin


main.cpp
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
main.cpp
#include <iostream>
using namespace std;
#include <fstream>
#include <string>

class user {
  public:
    string username;
    int pass;
    string maiden;
    char choice1;
    string temp;
    string temp1;
    string userpass;
    fstream usernames;
    fstream pswd;
};

class registration : public user {
  public:
    
};

class login : public user {
  public:
  int checkusername(){
    
        //open file to read
    usernames.open("names.txt");
      if (!usernames){
			cout<<"file not open";
			return 1;
		      }

    while(usernames>>temp){
      cout<<"please enter your username: ";
      cin>>username; //userinput

        if(temp==username){
          cout<<"found";
        }

        while(temp!=username){
          for (int i=0;i<4;i++){
          cout<<"please enter your usernamejji: ";
          cin>>username; //userinput
          break;
          }
          //give choice for registration
        }
    }

    usernames.close();
    //closing the file
return 0;
  }
  
  void checkpassword(){
    cout<<"\nplease enter your password: ";
    cin>>userpass;

    //opening file to read
    pswd.open("pass.txt");

    while(pswd>>temp1){
      if(temp1==userpass){
        cout<<"found";
      }
    }

    pswd.close();
    //closing the file
  }
};

int main() {
user u;   
login verify; 
cout<<"\nAre you a new user? (y/n)";
cin>>u.choice1;
  if (u.choice1 == 'y') 
  {
    verify.checkusername();
    verify.checkpassword();
  }

  else if (u.choice1 == 'n') {
    cout<<"cnoij";
  }
  else {
    cout<<"error 505";
  }
return 0;
}
Last edited on

My guess is that your pushing the whole text file into temp. Print out temp and see what it looks like.

Also I'm not entirely sure the result of >> temp will ever be false as long as the file is open.


The loop continues reading until you have reached the end of the file, at which point, the loop will exit.
@TheToaster

But the loop doesn't exit even then. there are four entries in the text file but the loop keeps repeating this part, right or wrong input.

1
2
3
4
5
for (int i=0;i<4;i++){
					cout<<"please enter your usernamejji: ";
					cin>>username; //userinput
					break;
				}
Why don't you read all the usernames into a vector and search then the vector?
@Thomas1965

I didn't think of adding vectors into this, I'll try that!
I tried putting everything in my file, into a vector myvec but its still not working in the same way as before.

This is the relevant code with vectors added in.

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
class login : public user {
	public:
	vector<string> myvec;
	int checkusername(){
		
				//open file to read
		usernames.open("names.txt");

		if (!usernames){
			cout<<"file not open";
			return 1;
		}

		while(usernames>>temp){
		myvec.push_back(temp);
			cout<<"please enter your username: ";
			cin>>username; //userinput


			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  

				if(temp==username){
					cout<<"found";
				}

				while(temp!=username){
					for (int i=0;i<4;i++){
					cout<<"please enter your usernamejji: ";
					cin>>username; //userinput
					break;
					}
					//give choice for registration
				}
		}

		usernames.close();
		//closing the file
		return 0;
	}
Last edited on
This is what it says about the >> istream operator

stream buffersExtracts as many characters as possible from the stream and inserts them into the output sequence controlled by the stream buffer object 

To me that sounds like the whole file. If it just got one line what would be the point of getline().
Should be much easier:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

vector<string> read_user_names(const string& filename)
{
  // TODO implement me
}

int main()
{
  vector<string> user_names = read_user_names("names.txt");

  while (true)
  {
    cout << "Username: ";
    string input;
    getline(cin, input);
    // now search the vector for input
  }
}
@maryrocks

could you refer to which line you're talking about? I'm a wee confused.
@Thomas1965

Thank you! So expanding on line 10 is that where I would add my loops? what could I put in there?

And could you explain what line 17 is doing? is it saying while the vector is true? (I'm very new to vectors and just started today for this program.

Also does this mean I wont have to open the file, check if its opened and all that as I was initially doing?
I made the changes but im getting an error next to vector<string> read_user_names(const string& usernames){}

If its acceptable and not too much hassle can the changes be made within my code so I can understand better? I realize its a huge task so if its too much hassle or unacceptable it's fine not to do that too, please do keep providing your aid so I can make these loops work.

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
#include <iostream>
using namespace std;
#include <fstream>
#include <string>
#include <limits>
#include <vector>
vector<string> read_user_names(const string& usernames){}

class login : public user {
	public:
		vector<string> user_names = read_user_names("names.txt");
	int checkusername(){
			while (true){

				//open file to read
		usernames.open("names.txt");

		if (!usernames){
			cout<<"file not open";
			return 1;
		}

		while(usernames>>temp){
			cout<<"please enter your username: ";
			cin>>username; //userinput


			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  

				if(temp==username){
					cout<<"found";
				}

				while(temp!=username){
					for (int i=0;i<4;i++){
					cout<<"please enter your usernamejji: ";
					cin>>username; //userinput
					break;
					}
					//give choice for registration
				}
		}

		usernames.close();
		//closing the file
		return 0;
			}
	}
This line

 
while(usernames>>temp)


Print out temp and see what it looks like. Debugging is a skill in itself and printing out the contents of variables is the best thing to do if you don't know what is happening in your code. Now we've taken a simple thing and turned it into a complex program. Comparing a user input to the contents of a text file is not very complicated but here we are.
Your code should have 2 parts. One part read only the usernames from the file into the vector and nothing else.
The second part asks the user for the name and checks if it is in the vector. There is not much need for different classes - unless it's a must.

On line 10 you have a while loop where you read each line with getline.

Line 17 is an endless loop, you get out of it with break if the name is found in the vector.

Also does this mean I wont have to open the file, check if its opened and all that as I was initially doing?
Of course you have to do it starting at line 10.
it don't get any easier than this...

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

#include <iostream>
#include <sstream>;

int main() {
	using namespace std;
	stringstream ss;

	ss << "hello" << "\n" << "yesyes" << "\n" << "querty" << "\n";

	string input = "";
	vector<string> temp;
	string temp2;
	while (ss >> temp2) { temp.push_back(temp2); }

	cout << "enter username " << "\n";
	cin >> input;

	bool correct = false;

	while (correct == false) {
		for (string s : temp){
			if (input == s) { cout << "bingo"; correct = true; break; }
		}
	if (correct == false) { cout <<"  incorrect username enter another" << "\n"; cin >> input; }
	else { break; }

	}
}
Last edited on
Pages: 12