Hi all,

I have this if condition which never seems to be true, but should be! It's the part in the code where I pull the String^ from textboxes (the regUsername textbox) and compare the inputted username with the first word from every text file line. So every text file line should be loaded, first word extracted and compared to the entered username, if it's the same with what's entered: error. If not, proceed with registration. But somehow, the result2 == fileUser is never true! How come?
Here's my code.

String^ result = regUsername->Text;
                 string result2 = marshal_as<string>(result);

                 ifstream regUsers ("regUsers.csv");
                 string line;
                 string fileUser;

                 /* While there is still a line. */
                 while(getline(regUsers, line))
                 {
                     fileUser = extractWord(1, line);

                     if (result2 == fileUser)
                     {
                         errorLabel->Text = L"Username already exists!";
                         errorLabel->Visible = true;
                     }
                     else
                     {
                         if (!regUsers)
                         {
                             // 'File doesn't exist' solution

                             System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter("regUsers.csv");
                             sw->WriteLine("USERNAME,PASSWORD,FIRST-NAME,LAST-NAME,E-MAIL");
                             sw->WriteLine(regUsername->Text + "," + regPassword->Text + "," + regFirstname->Text + "," + regLastname->Text + "," + regEmail->Text);
                             sw->Close();
                         }
                         else
                         {
                             // 'File exists' solution

                             int length;

                             regUsers.open("regUsers.csv", ios::binary); // open your file
                             regUsers.seekg(0, ios::end); // put the "cursor" at the end of the file
                             length = regUsers.tellg(); // find the position of the cursor
                             regUsers.close(); // close your file

                             if (length == 0)
                             {
                                 // 'File is empty' solution

                                 System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter("regUsers.csv");
                                 sw->WriteLine("USERNAME,PASSWORD,FIRST-NAME,LAST-NAME,E-MAIL");
                                 sw->WriteLine(regUsername->Text + "," + regPassword->Text + "," + regFirstname->Text + "," + regLastname->Text + "," + regEmail->Text);
                                 sw->Close();
                             } 
                             else
                             {
                                 // 'File is not empty' solution(s)

                                 string firstLine;
                                 ifstream fileToSearch ("regUsers.csv");
                                 getline(fileToSearch, firstLine);

                                 if (firstLine == "USERNAME,PASSWORD,FIRST-NAME,LAST-NAME,E-MAIL")
                                 {
                                     // 'First line is header' solution

                                     System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter("regUsers.csv", true);
                                     sw->WriteLine(regUsername->Text + "," + regPassword->Text + "," + regFirstname->Text + "," + regLastname->Text + "," + regEmail->Text);
                                     sw->Close();
                                 }
                                 else
                                 {
                                     if (firstLine.empty())
                                     {
                                         // 'First line is empty' solution

                                         std::string line;

                                         ifstream infile ("regUsers.csv");
                                         ofstream outfile ("temp.csv");

                                         outfile << "USERNAME,PASSWORD,FIRST-NAME,LAST-NAME,E-MAIL\n";

                                         if (infile.is_open())
                                         {
                                             while (infile.good())
                                             {
                                                 getline(infile, line);

                                                 if (line.empty())
                                                 {
                                                     outfile << line;
                                                 } 
                                                 else
                                                 {
                                                     outfile << line << endl;
                                                 } 
                                             }
                                             infile.close();
                                         }
                                         else
                                         {
                                             errorLabel->Text = L"Unable to open file";
                                         }

                                         outfile.close();
                                         fileToSearch.close();

                                         System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter("temp.csv", true);
                                         sw->WriteLine(regUsername->Text + "," + regPassword->Text + "," + regFirstname->Text + "," + regLastname->Text + "," + regEmail->Text);
                                         sw->Close();

                                         remove("regUsers.csv");
                                         rename("temp.csv", "regUsers.csv");
                                     } 
                                     else
                                     {
                                         // 'First line is something else' solution

                                         ifstream stream1("regUsers.csv");
                                         ofstream stream2("temp.csv");
                                         stream2 << "USERNAME,PASSWORD,FIRST-NAME,LAST-NAME,E-MAIL\n";
                                         stream2 << stream1.rdbuf();

                                         stream1.close();
                                         stream2.close();
                                         fileToSearch.close();

                                         System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter("temp.csv", true);
                                         sw->WriteLine(regUsername->Text + "," + regPassword->Text + "," + regFirstname->Text + "," + regLastname->Text + "," + regEmail->Text);
                                         sw->Close();

                                         remove("regUsers.csv");
                                         rename("temp.csv", "regUsers.csv");
                                     }
                                 }
                             }
                         } 
                     }
                 }

                 regUsers.close();


                 registerForm::Close();

Let me just say that a pretty much same mechanism already worked before where I loaded each file's line, extracted 2 words (usernames and passwords) and compared them to user-inputted username and password. And it worked! If you want to see the version of this code which worked, tell me, but, as I said, it pretty much is the same mechanism, just slightly different code.

Thank you!

result2 and fileUser are both std::string, therefore the will match if the have exactly the same characters in them, same characters same order same case.

If they don't match then they don't have exactly the same strings in them. This is your real question, why do result2 and fileUsernever contain the same string.

This is harder to tell because you haven't told us what the input is, what the file contents are or what extractWord does.

However you best debuging option may be to output result2 and fileUser when the don't match so you can see them, then you should be able to see if you are getting any spurious characters. If you do this remeber to surround each string with some sentinel character like ' so you can see if they contain strings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.