need help with if statements
Aug 27, 2014 at 6:37am UTC
I think the problem is that I'm comparing strings in the if statements
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 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include <string>
using namespace std;
int main()
{
string username;
string password;
int loggedin = 0;
int errorcode = 0;
int profileselected = -1;
string profiles [100][3];
profiles[7][1] = "b" ;
profiles[7][2] = "a" ;
while (loggedin == 0)
{
if (errorcode == 1) cout << "Invalid username/password!" << endl;
cout << "Username: " ;
cin >> username;
cout << "Password: " ;
cin >> password;
for (int i = 1; i < 101; i++){
if (profiles [i][1] == username){
if (profiles [i][2] == password){
loggedin = 1;
profileselected = i;
}
}
}
if (profileselected == -1) errorcode = 1;
system("ClS" );
}
}
Aug 27, 2014 at 8:01am UTC
You have a problem on line 30. The index of an array starts with 0, hence:
for (int i = 0 ; i < 100 ; i++){
Aug 27, 2014 at 1:22pm UTC
I know but I'm developing this with my friend, (who ironically is always trying to be efficient) and he doesn't like that.
Aug 27, 2014 at 1:47pm UTC
oh nvm you have to do that don't you, I never noticed that.
Aug 27, 2014 at 4:25pm UTC
You can use
&&
instead of two
if
statements. Also, once you find the right profile, break out of the loop to avoid unecessarily checking all the other profiles:
1 2 3 4 5
if (profiles [i][1] == username && profiles [i][2] == password){
loggedin = 1;
profileselected = i;
break ;
}
Topic archived. No new replies allowed.