Char String??

Hi Im just trying it out but Im trying to create a console application that just does a basic thing like create a username and password, so atm I have this...


char sUsernameCorrect[] = "Limbo";

Atm It will only accept Limbo with that working and grammar, so with a capital L and unless it has that it will not work so what I was hoping someone could answer is if they are a way for it to work like this...

char sUsernameCorrect[] = "Limbo" || "limbo" || "LIMBO";


If someone could tell me how I can do this, that would be awesome, thanks :)
Last edited on
You could create an array of const char* like this
const char* sUsernameCorrect[] = {"Limbo", "limbo", "LIMBO"};

A more robust solution is probably to convert all the characters to lower (or upper) case before comparing the characters. That way you can easily handle ALL combinations of upper/lower case.
http://en.cppreference.com/w/cpp/string/byte/tolower
Last edited on
Yeah I tried that but I don't think that would work cause it would be like

const char* sUsernameCorrect[4][8] = {'Limbo', 'LIMBO', 'limbo'};


Cause I tried the one you did and tried compileing it and I got a bunch of errors.

And another big problem is, is when they go to enter the username and say instead of LIMBO and they wanted Lingo it would log them in as LIMBO due to the capital L char
this is an example of it...

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
#include <iostream>
using namespace std;

int main () {
	const char password [] = "limbo";
	char user_entry [6] = {'\0'};
	bool cond = true;
	cout << "enter password: ";
	cin >> user_entry;
	
	for (int length = 0; length < 6; ++length) {
		if (tolower(user_entry[length]) == password [length])
			continue;
		else {
			cout << "wrong password" << endl;
			cond = false;
			break;
		}
	}
	if (cond)
		cout << "correct" << endl;
	cin.get();
	cin.get();
	return 0;
}


CMIIW
Last edited on
I do not see any sense in the declaration

const char* sUsernameCorrect[4][8] = {'Limbo', 'LIMBO', 'limbo'};

@Peter87 showed already how the array should be declared

const char* sUsernameCorrect[] = {"Limbo", "limbo", "LIMBO"};

Take into account the double quotes enclosing string literals.
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
#include <iostream>
using namespace std;



void main ()
{
	char sUsernameCorrect[3][6] = {"limbo" , "LIMBO", "Limbo"};
	char input[6];

	cin>>input;


	for(int i=0;i<3;i++)
	{
		if (strcmp(input, sUsernameCorrect[i] ) == 0)
		{
			cout<<"Corect Username"<<endl;
			break;
		}
	}


	cin.get();
	cin.get();
}
1
2
3
4
5
6
7
8
        for(int i=0;i<3;i++)
	{
		if (strcmp(input, sUsernameCorrect[i] ) == 0)
		{
			cout<<"Corect Username"<<endl;
			break;
		}
	}


you should add 1 more for
1
2
3
4
5
6
7
8
 for(int i=0;i<3;i++)
	{
		if (strcmp(input, sUsernameCorrect[i] ) == 0)
		{
			cout<<"Corect Username"<<endl;
			break;
		}
	}


i=0 --> sUsernameCorrect[0] == "limbo"

i=1 --> sUsernameCorrect[1] == "LIMBO"

i=2 --> sUsernameCorrect[2] == "Limbo"

making i bigger than 3 will just be useless
Last edited on
Use the tolower method, as chipp stated.
Topic archived. No new replies allowed.