I just recently started learning C++, in the book i am learning from there is a problem that asks you to write a simple pasword program. It just asks the user the password and then displays a message wether its right or wrong after they enter it. I am having a lot of trouble declaring the right variable type, the password is kevin so i declared kevin as a char and the letter "x" as a char to and said:
cin >> x;
if (x==kevin){
cout<< "Correct"
else
cout<<"Incorrect"
}

could anyone help me out?

A char variable represents a single character, such as a single letter or symbol. It cannot be used for a string such as "Kevin" ... Have you learned about arrays yet? Generally a string is represented as an array of characters. I'm not sure which textbook you're using, but it may provide you with its own String class for you to use. If you do a #include <string> or "String.h" or some variant at the top of your program, you should be able to have a String kevin; The thing is, that for some reason, strings aren't natively built-into the C++ language and need to somehow be worked in.

In addition to that, a string should always be referenced by double quotes while a character by single quotes. Therefore, once you get the string declaration worked out, it should read if (x == "Kevin") ... Good luck! :)

commented: doesn't solve the problem, rather just thoughtfull. +0

wow thanks:)

Really old question, but in case anyone else reads this I would do this. Please note that I learned this method years ago, and there may be better ways now, if there wasn't already back then.

char[20] password;

int main()
{

cin >> password;

if (strcmp(password, "kevin")==0)
cout << "Correct!";

else
cout << "Incorrect!";

cout << endl;
system("PAUSE");
return 0;

}

Saying char[20] when declaring "password" sets the max number or characters to 20. You may make it whatever you like, such as if you wanted the max to be 65 characters you would type char[65], putting a high max number of characters could prevent possible string overflow.

In my if statement I typed strcmp. This means string compare and will compare the two strings in brackets. Eg. strcmp(String1, String2). The strcmp command will return a 0 if the strings are equal and another value if they are not. Saying if (strcmp(password, "kevin")==0) means that it compares the string password to the string "kevin", and if it returns a 0 (meaning that it is true) it will do what you tell it.

commented: Nothing useful, and some of it just plain wrong, in this 5 YEAR grave-dig -7
commented: does not solve the proble, besides -incomplete codeing/poor xample +0

char[20] is rarely used in C+ :D

commented: criticital/ not helpfull! +0

hey i am working in c++ and need to make command for pasword in letters
i do this and it has an error
#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;
char kod,red,pokusaj,hah,af,ih,sad,pii,gii,ab,nije,ihhh,gay,nogay,no,peder,krava,alma,omer;
int main () {
startover:
cout<<
can u help me with this?

why do you have so many variables? I'm pretty sure they are not needed...

#include <string.h>
#include <iostream.h>
#include <stdlib.h>

char x[20]; // declares a string variable called 'x' with capacity of 19 letters
char y[20]; // declares a string variable called 'y' with capacity of 19 letters

std::cin << "enter password plz:" << x << "\n";

/* at this point i have trouble working with chars, logical ops 2 b precise */
// however u will find declaring x while assigning how many characters it can hold 
// (the strings variable capacity to hold characters assigned).....as
// i have here above! cslled an array apparently. not to b confused 
// with x(234,6,19)=value. such as found in BASIC and PASCAL.:)
// to all devs - GOTO RULEZ- XXX HahaHa!

U'll need to change it...
Your certain structures are wrong..

Here is what I think should work...
Its your code, I just corrected it..

#include <string.h>
#include <iostream.h>
#include <stdlib.h>

char x[20]; // declares a string variable called 'x' with capacity of 19 letters
char y[20]; // declares a string variable called 'y' with capacity of 19 letters

std::cout << "enter password plz:" <<endl;
cin>>x;

Welcome :D

3 resurrections of this zombie is enough!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.