Why is this not working?

closed account (2E0XoG1T)
#include <iostream>
using namespace std;
int main()
{
	string password;
	cout <<"Enter password ";
	cin >> password ;
	if (password == 123456) {
		cout << "Access granted. \n"; }
	else {
		cout << "Acces denied. \n"; }

return 0;
}


I'm learning c++ and i'm stuck at step "If...else"
Last edited on
Currently, you comparing a number to string. In order to tell the compiler you want to use a string, you need to put " " around the number, like when you are outputting text.
closed account (2E0XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
	string password;

	cout <<"Unesite password ";
	cin >> password ;

	if(password = "idioti12") 
	{
		do cout << "Pristup odobren. \n";
	}

	else 
	{
		do cout << "Pristup odbijen. \n";
	}

return 0;
}


--->Not working as well. I type right "password" but it says: Access denied. lol
With password = "idioti12" you set idioti12 as value of password. For comparing you need to use ==.
But for Objects like Strings it's better to use their special compare-function

See:
http://www.cplusplus.com/reference/string/string/compare/
closed account (2E0XoG1T)
what about error:
"The variable 'a' is being used without being initialized."
I can't see a variable 'a' in your code.
If it says "The variable 'password' is being used without being initialized."
change string password; into string password="";
Last edited on
closed account (2E0XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
	string password = idioti12;

	cout <<"Unesite password ";
	cin >> password;

	if(password = "idioti12") 
	{
		cout << "Pristup odobren. \n";
		cout << "Dobrodosli! \n";
	}

	else 
	{
		cout << "Pristup odbijen. \n";
	}

return 0;
}

still many errors and "beeps"
Yeah, this part ist still wrong:
1
2
3
4
5
if(password = "idioti12") 
{
	cout << "Pristup odobren. \n";
	cout << "Dobrodosli! \n";
}


Like I said 3posts ago you should use compare.
Try this:
1
2
3
4
5
if(password.compare("idioti12") == 0) 
{
	cout << "Pristup odobren. \n";
	cout << "Dobrodosli! \n";
}
Last edited on
closed account (2E0XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
	string password = idioti12;

	cout <<"Unesite password ";
	cin >> password;

	if(password.compare ("idioti12) == 0) 
	{
		cout << "Pristup odobren. \n";
		cout << "Dobrodosli! \n";
	}

	else 
	{
		cout << "Pristup odbijen. \n";
	}

return 0;
} 

9 errors

EDIT - 13 errors...
Last edited on
How many with this?
And which kind of errors?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>    // have to include string
using namespace std;
int main()
{
	string password = ""; // use " for string values - "" is a empty string

	cout <<"Unesite password ";
	cin >> password;

	if(password.compare("idioti12") == 0)  // in your example you missed the 2nd "
	{
		cout << "Pristup odobren. \n";
		cout << "Dobrodosli! \n";
	}

	else 
	{
		cout << "Pristup odbijen. \n";
	}

return 0;
} 
Last edited on
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
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

using namespace std;

int main()
{
    /*
     * this is totally okay, only built in types need explicit initialization
     * as class's default constructor takes care of initialization.
     */
    string password; // password == ""

    cout <<"Unesite password ";
    /*
     * Much safer, read http://www.cplusplus.com/forum/articles/6046/
     */
    getline(cin, password);

    /*
     * comparing with operator==() is faster than string::compare()
     */
    if(password == "idioti12")  //check for equaliy with double equation marks ==
    {
        cout << "Pristup odobren. \n";
        cout << "Dobrodosli! \n";
    }
    else
    {
        cout << "Pristup odbijen. \n";
    }

    return 0;
}
closed account (2E0XoG1T)
Thanks alot! Working cool now. I just had to #include <string>
Will continue program now.
Topic archived. No new replies allowed.