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.
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.
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.
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.
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;
}
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.
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?