Nov 25, 2009 at 8:52am UTC
#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 Nov 25, 2009 at 8:53am UTC
Nov 25, 2009 at 9:05am UTC
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.
Nov 25, 2009 at 9:31am UTC
what about error:
"The variable 'a' is being used without being initialized."
Nov 25, 2009 at 9:51am UTC
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 Nov 25, 2009 at 9:51am UTC
Nov 25, 2009 at 10:00am UTC
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 Nov 25, 2009 at 10:00am UTC
Nov 25, 2009 at 10:07am UTC
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 Nov 25, 2009 at 10:10am UTC
Nov 25, 2009 at 12:44pm UTC
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 Nov 25, 2009 at 12:47pm UTC
Nov 25, 2009 at 5:18pm UTC
Thanks alot! Working cool now. I just had to #include <string>
Will continue program now.