Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string user[2] = {"may","kj"};
	int pass[2] = { 686668,7977979 };
	string username;
	string x;
	int passeword;
	int y;
	string press;
	string delate;
//add user
	cout << "do you want to add username press ok or not press not " << endl;
	cin >> press;
	if (press == "ok")
	{
		cout << "enter new username: " << endl;
		cin >> username;
		user[2] = username;
		for (int i = 0; i <= 2; i++)
		{
			if (username == user[i])
			{
				cout << "username already exist" << endl;
			}
			else
			{ 
				cout << "enter the passeword : " << endl;
				cin >> passeword;
				pass[2] = passeword;
				cout << "username already added" << endl;
			}
			cout << user << endl << pass << endl;
		}
	}
	else
		//delate
	{
		cout << "do you want to delate any username press yes or not" << endl;
		cin >> delate;
		if (delate == "yes")
		{
			cout << "enter your username: " << endl;
			cin >> username;
			string sure;
			cout << "are you sure to delate this username" << endl;
			cin >> sure;
			if (sure == "yes")
			{
				cout << "the username already delate" << endl;
			}
			else
			{
				cout << "ok" << endl;
			}
		}
		else
		{
			cout << "username didn't delate" << endl;
		}
	}
//login
	string login;
	cout << "login press log or sigup press sign" << endl;
	cin >> login;
	if (login == "log")
	{
		cout << "enter your username" << endl;
		cin >> username;
		cout << "enter your passeword" << endl;
		cin >> passeword;
		for (int i = 0; i <= 2; i++)
		{
			for (int z = 0; z <= 2; z++)
			{
				if (username == user[i])
				{
					if (passeword == pass[z])
					{
						cout << "already login" << endl;
					}
					else
					{
						cout << "please enter passewor again" << endl;
						cin >> passeword;
					}
				}
				else
				{
					cout << "username not found please enter username again" << endl;
					cin >> username;
				}
			}
		}
	}
	else
	{
		cout << "enter new username: " << endl;
		cin >> username; 
		cout << "enter the passeword : " << endl;
		cin >> passeword;
		cout << "already sig in" << endl;
	}
 }


What I have tried:

find the best solution
i am beginner
Posted
Updated 20-Sep-20 9:26am
v2

Quote:
If my code correct or not

No, it is not correct.
C++
	string user[2] = {"may","kj"}; // here you declare a fixed size array
	int pass[2] = { 686668,7977979 }; // here you declare a fixed size array
...
		cin >> username;
		user[2] = username; // you store data after the end of array without resizing
		                    // problem fixed size array can not be resized
		for (int i = 0; i <= 2; i++)
		{
			if (username == user[i]) // this will always match because you also test user[2]
			{
				cout << "username already exist" << endl;
			}
			else
			{ 
				cout << "enter the passeword : " << endl;
				cin >> passeword;
				pass[2] = passeword; // you store data after the end of array without resizing
				                     // problem fixed size array can not be resized
				cout << "username already added" << endl;
			}
			cout << user << endl << pass << endl;
		}
	}
 
Share this answer
 
v2
Comments
CPallini 20-Sep-20 13:29pm    
5.
Patrice T 20-Sep-20 13:45pm    
Thank you
I strongly suggest you using std::vector, instead of C-like arrays. Try, for instance:
C++
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;


struct User
{
  string name, pwd;
  bool loggedin;
};

void adduser( vector <User> & v );
void deluser( vector <User> & v );
void loguser( vector <User> & v );

int main()
{
  vector <User> v {{"may", "686668", false},{"kj", "7977979" , false}};

  char choice;
  do
  {
    cout << "insert '+' to add a user, '-' to delete a user, 'l' to login, 'q' to quit\n";
    cin >> choice;

    switch ( choice)
    {
    case '+':
      adduser(v);
      break;
    case '-':
      deluser(v);
      break;
    case 'l':
      loguser(v);
      break;
    }
  } while (choice != 'q');

  cout << "bye" << endl;
}
void adduser( vector <User> & v)
{
  string name;
  cout << "please enter the new user name\n";
  cin >> name;
  auto it_found = find_if(v.begin(), v.end(), [ & name ] (auto u) { return u.name == name; } );
  if (it_found != v.end() )
  {
    cout << "this user name is already present\n";
    return;
  }
  string pwd;
  cout << "plaese enter the password for this user\n";
  cin >> pwd;
  v.emplace_back(User{ name, pwd, false } );
}

void deluser( vector <User> & v)
{
  cout << "implementation left as exercise\n";
}
void loguser( vector <User> & v)
{
  cout << "implementation left as exercise\n";
}
 
Share this answer
 
Comments
Patrice T 20-Sep-20 16:59pm    
+5
CPallini 21-Sep-20 2:04am    
Thank you!
How would we know?
We have no idea what your code is intended to do, so we have no idea if what it does is intended behaviour or not!

Suppose I hand you a black box with switches and coloured lights on it, and say "does it work?"
You flip a switch and the red light comes on.
You fip another and a car crashes in the street outside.
Did the box do that? Was it supposed to? You don't know.

We are in the same position with your code: it has a lot of problems that I can see from my position as a professional regarding the security aspects, but they probably aren't of any interest to you unless your homework assignment specifically asks for an even-slightly-secure login system.

Even if we knew, testing and debugging it is part of your task: and an important one. So run it and do what the homework question asks you to: then look at what happened. Then try it again, but give it bad values. test everything you can think of, and see what happens. Some of it will be what you want to happen, but some of it probably won't - so break out the debugger and start looking at why, and then fix them: then start testing all over again!
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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