Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

[C++] If condition is never true (but it should be)

$
0
0
Gonna try and keep it short and clear ('cause I thought I posted this, but accidentally didn't, so I have to retype).
Anyway, 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();



This is what I included at the beginning of the header file:
#pragma once
#include <regex>
#include <string>
#include <fstream>
#include <stdio.h>
#include <msclr/marshal_cppstd.h>
using namespace std;
using namespace msclr::interop;



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! :D

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>