#include <iostream>
usingnamespace 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
#include <iostream>
#include <string>
usingnamespace 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;
}
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.
}
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);
}
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.
#include <iostream>
usingnamespace 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: