Click here to Skip to main content
15,889,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've figured out everything, the main error is that the check for same username during sign up is not working. Please help

What I have tried:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include<stdlib.h>
#include<time.h>
#include <cstdlib>
#include "conio.h"
using namespace std;
int check(string username)
{
	cin >> username;
	string data1, data2;
	ifstream infile;
	infile.open("profile.txt", ios::app);
	getline(infile, data1);
	if (username == data1)
	{
		cout << "Username exits" << endl;
		cout << "Enter new username: ";
		cin >> username;
		return check(username);
	}
	else
	{
		
	}
}
int main()
{
	int z;
FIRST:	
	cout << "Press 1 to Create Account" << endl; cout << "Press 2 to Log In " << endl; cout << "your choice: ";
VALID:	cin >> z;
	switch (z)
	{
	case 1:                            
	{
		string name, pas, repas;
		cout << "Enter Your username:";
		cin >> name;
		check(name);
	    cout << "Enter Password(Atleast 5 characters) :";
		START1:     cin >> pas;
		if (pas.length() < 5)
		{
		    cout << "Enter a password of atleast 5 characters : ";
			goto START1;
		}
		else
			cout << "Re-write Password to confirm:";
	START2:		cin >> repas;	
		if (repas != pas)
		{
			cout << "Re-Enter the Password !";
			goto START2;
		}
			else
				cout << "Profile Created With the User Name " << name << endl;    // cout on the console
		ofstream myfile;
		myfile.open("profile.txt",ios::app);
		myfile << name << endl;        
		myfile << pas << endl;             
	    myfile.close();
	    _getch();
		system("CLS");
		goto FIRST;   
	}
	case 2:         // For logging in
	{
						system("CLS");
						cout << "Welcome to Login screen\n\m";
						char pass[32];
						string name, pas;
					START3:		
						cout << "Enter Username:";
						cin >> name;
						string data1, data2;
						ifstream infile;
						infile.open("profile.txt",ios::app);
						getline(infile, data1);   
						if (name != data1)
						{
							cout << "Invalid User Name.Try Again\n";
							goto START3;
						}
						else
							cout << "User Name Found\n";
						cout << "Enter Password : ";
						int i = 0;
						char a;
						for (i = 0;;)
						{
							a = _getch();
							if ((a >= 'a'&&a <= 'z') || (a >= 'A'&&a <= 'Z') || (a >= '0'&&a <= '9'))
							{
								pass[i] = a;
								++i;
								cout << "*";
							}
							if (a == '\b'&&i >= 1)
							{
								cout << "\b \b";
								--i;
							}
							if (a == '\r')
							{
								pass[i] = '\0';
								break;
							}
						}
						getline(infile, data2);         
					START4:		if (pass == data2)
					{
									cout << "You are Logged In";
									_getch();
					}
								else
								{
									cout << "Invalid Password\nEnter Again:";
									char pass[32];
									cout << "Enter Password : ";
									int i = 0;
									char a;
									for (i = 0;;)
									{
										a = _getch();
										if ((a >= 'a'&&a <= 'z') || (a >= 'A'&&a <= 'Z') || (a >= '0'&&a <= '9'))
										{
											pass[i] = a;
											++i;
											cout << "*";
										}
										if (a == '\b'&&i >= 1)
										{
											cout << "\b \b";
											--i;
										}
										if (a == '\r')
										{
											pass[i] = '\0';
											break;
										}
									}
									goto START4;
								}

								infile.close();
								break;
	}
	default:

	{ 
			   cout << "Please Enter Valid Number !";
			   goto VALID; }
	}
}
Posted
Updated 11-Jan-18 7:50am

1 solution

Quote:
int check(string username)
{
cin >> username;
string data1, data2;
ifstream infile;
infile.open("profile.txt", ios::app); //<-- What are you going to append to a input file?
getline(infile, data1);
if (username == data1)
{
cout << "Username exits" << endl;
cout << "Enter new username: ";
cin >> username;
return check(username);//<-- it is NOT a good idea to call the function recursively (the file is open again and again)
}
else
{

}
}


My advice: implement the check function simply for checking, then ask again (in the calling code) the user to enter the username if it fails.

My general advice: don't use goto, use the C++ features.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900