Tried to make login mechanism.

So I tried to make a very simple login thingy.

This is what i've got so far (Not working)

Source:


#include <iostream>

using namespace std;
int main(){
char ped[30];
char ped1[30];

cout << "What password would you like?" << endl;
cin >> ped;
cin.ignore();


cout << " Enter password to continue" << endl;
cin >> ped1;



if (ped1 = ped) {

cout << " Login Succesfull." << endl;
cin.ignore();
}
}

I got theese errors:

C:\Users\xxx\Desktop\lol.cpp In function `int main()':

Line: 18 C:\Users\xxx\Desktop\lol.cpp ISO C++ forbids assignment of arrays

Line 18 C:\Users\xxx\Desktop\lol.cpp invalid use of non-lvalue array

So, can anyone please tell me about the errors and what I need to change to make it work.

Thanks in advance and sorry for my english...! :)




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;
int main()
{
    char ped[30];
    char ped1[30];

    cout << "What password would you like?" << endl;
    cin >> ped;
    cout << " Enter password to continue" << endl;
    cin >> ped1;
    if (ped1 == ped)
    {
        cout << " Login Succesfull." << endl;
        cin.ignore();
    }
}


Although a better way would be to use Wide-character strings.

EDIT:
Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;
int main()
{
    wstring ped;
    wstring ped1;
    cout << "Set your password:" << endl;
    getline(wcin,ped);

    cout << "Login using your password:" << endl;
    getline(wcin,ped1);
    if (ped1 == ped)
    {
        cout << endl << "Login successful." << endl;
    }
    // Pause here
}

I did not add the pausing myself, because I don't want to encourage things like system-pausing.
Last edited on
@Kyon
You can't compare char arrays like that. It will compare it's addresses, not contents.
@xcynic
Either use std::string, which has overloaded comparison operators. Or compare ped and ped1 with strcmp.
I kind of expected that to happen, yeah, didn't test the code. The wstring IS tested, though.
Okey, I think this was abit out of my league tbh. Just started with C++ yesterday and wanted to give this a shot.

I think ill go back to this when Im abit wiser :p
There's no need to fear, the only concept you need to get why your comparison doesn't work is called: Pointers. And the only concept you need to understand to get why the string method works fine is.. frankly, String itself. Use the tutorial found on this website and combine it with the C++ Console lessons from http://www.xoax.net/ and you'll be fine.
Topic archived. No new replies allowed.