problems with char variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

char username, password;

int main()
	{
		cout<<"What is the username?\n";
		cin>>username;
		cout<<"\nWhat is the password?\n";
		cin>>password;
				
	return 1;
	}
	


The program above ONLY let me enter the username, but not the password. I believe that the problem comes from the variable. What should i put as the variable so that the program will let me enter the username and the password as a CHARACTER. Thank you
std::string http://www.mochima.com/tutorials/strings.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

std::string username, password;

int main()
	{
		cout<<"What is the username?\n";
		cin>>username;
		cout<<"\nWhat is the password?\n";
		cin>>password;
				
	return 1;
	}

Last edited on
Let me explain your mistake:

As you wrote it, your program lets you input a character. a SINGLE character, no more.

If you want to use more than one character you have two main ways:
1. Using std::string as JLBorges wrote above
2. Using character arrays:
1
2
3
4
5
6
7
8
9
10
11
12
13
char username[512]; // Up to 512-chars nick
char password[512]; // Up to 512-chars pass

int main()
{
	cout<<"What is the username?\n";
	cin>>username;	
	cout<<"\nWhat is the password?\n";
	cin>>password;
	return 0;
// Remember: If your program terminates successfully return 0.
// Only if you had a fatal error you should return 1 or any other value.
}
Understood. Thank you guys!
Also, if you want to input like a sentence (with spaces) (like this one), use std::getline(std::cin,variable) instead of cin>>variable. Try out this code and see why:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
	std::string hello;
	std::cout<<"(getline)Enter sentence.  ";
	std::getline(std::cin,hello);
	std::cout<<"You entered: "<<hello<<std::endl;
	std::cout<<"(cin)Enter same sentence by pressing the 'up' arrow key.  ";
	std::cin>>hello;
	std::cout<<"You entered: "<<hello;
	return(0);
}
Last edited on
So I try writing something where you first enter you desired username, and when the program ask what's your desired username, you just enter what you just wrote, if it's incorrect, the program would say invalid code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

char username[512];
char password[512];
char username2[512];
char password2[512];

int main()
	{
		cout<<"What is the username (character only)?\n";
		cin>>username; //username
		cout<<"\nWhat is the password (number only)?\n";
		cin>>password;
				
		cout<<"\n";
		for(int run=10;run>=0;run--)
			{
				cout<<"\n"<<run;
			}
		cout<<"\nRUNNING PROGRAM\n";
		
		cout<<"\nEnter username: ";
		cin>>username2; //username2
			if(username2==username)
				{
					cout<<"Enter Password: ";
					cin>>password2;
						if(password2==password)
						{
						 cout<<"Username and password is valid";
						}
				}
			else
				cout<<"INVALID";
	return 1;
	}
	


I put 'john' as an input in username. When I tried to put 'john' again as an input during username2, the program fails to continue to password2. Please correct my coding.
It's because == doesnt work for type char*. For type char*, you need to go through a loop and check both arrays with each other.

Therefore, I changed the type char* to std::string and changed cin>> to getline() (because cin is for char and getline is for string). Here is the corrected program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

std::string username,password,username2,password2;

int main()
{
	std::cout<<"What is the username (character only)?  ";
	std::getline(std::cin,username); //username
	std::cout<<"What is the password (number only)?  ";
	std::getline(std::cin,password); //password
	std::cout<<std::endl;
	
	for(int run=10;run>=0;run--)
	{
		std::cout<<std::endl<<run;
	}
	
	std::cout<<std::endl<<"RUNNING PROGRAM"<<std::endl<<std::endl;
	std::cout<<"Enter username:  ";
	std::getline(std::cin,username2); //username2
	
	if(username2==username)
	{
		std::cout<<"Enter Password:  ";
		std::getline(std::cin,password2); //password2
		
		if(password2==password)
		{
			std::cout<<"Username and password is valid";
		}
	}
	else
	{
		std::cout<<"INVALID";
	}
	
	return (0);
}


I think the explanation is right, not sure. If someone else could explain it better then thank you!
Last edited on
Now I know. Thanks again!
usandfriends wrote:
It's because == doesnt work for type char*. For type char*, you need to go through a loop and check both arrays with each other.

Technically yes. Though in practice one would use a library function strcmp() to do the job. http://www.cplusplus.com/reference/cstring/strcmp/

But after all that, yes, it's simpler to use the C++ std::string instead.
Thanks Chervil for the help.
Topic archived. No new replies allowed.