954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Char question

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?

kcp
Newbie Poster
2 posts since Apr 2004
Reputation Points: 10
Solved Threads: 0
 

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 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! :)

cscgal
The Queen of DaniWeb
Administrator
19,423 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

wow thanks:)

kcp
Newbie Poster
2 posts since Apr 2004
Reputation Points: 10
Solved Threads: 0
 

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.

Keby
Newbie Poster
1 post since Jun 2009
Reputation Points: 3
Solved Threads: 0
 

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

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

hey i am working in c++ and need to make command for pasword in letters
i do this and it has an error
#include
#include
#include
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?

aammiirr
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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

hermann87
Junior Poster in Training
50 posts since Jan 2010
Reputation Points: 21
Solved Threads: 2
 
#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!
cplusplus-uxegs
Newbie Poster
4 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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..

[code]#include
#include
#include

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:" <>x;

Welcome :D

shanker31
Newbie Poster
21 posts since Apr 2011
Reputation Points: 10
Solved Threads: 2
 

3 resurrections of this zombie is enough!

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You