multiple if statement criteria?

Okay, I started learning C++ yesterday and am really clueless. Probably much easier ways of doing things than I am but yea... I've made this really bad Username password thing and I wan't to make it so if the username and password are both correct then it outputs "correct" or something, but I can't. It has something to do with the if statement I think. I don't know how to make it check if both the username and password are correct... It works if I only check if the password is correct. I'm probably doing something completely wrong.
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
#include <iostream>


using namespace std;

int main()
{

    string username;
    string password;



    cout<< "Username: ";
    cin.ignore();
    cin>> username;

    cout << "Password: ";
    cin.ignore();
    cin>> password;

    if (password == "cheese") && (username == "hello") {
        cout<<"Access Permitted";
    }

    else {
        cout<<"Access Denied";
    }

}

Take out those cin.ignore() lines. And wrap that conditional statement on line 22 in parentheses. The code as is doesn't compile.
closed account (E0p9LyTq)
You are only testing for the password string. Change (password == "cheese") && (username == "hello") to ((password == "cheese") && (username == "hello")) to test for both "cheese" and "hello".

Using cin.ignore(); with cin>> password or cin>> username will still make the test fail even if both strings are entered correctly. On my Win 7 machine using TDM-GCC 4.9.2 the test condition fails.

I personally would use std::getline(std::cin, username); and std::getline(std::cin, password);.
Thank you very much.
closed account (E0p9LyTq)
You are more than welcome, I know how being a real beginner learning C++ can be a bit intimidating.

Here is the complete code as I rewrote 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
26
27
28
#include <iostream>
#include <string>

int main()
{
   std::string username;
   std::string password;

   std::cout<< "Username: ";
   // std::cin.ignore();
   // std::cin>> username;
   std::getline(std::cin, username);

   std::cout << "Password: ";
   // std::cin.ignore();
   // std::cin>> password;
   std::getline(std::cin, password);

   if (password == "cheese" && username == "hello")
   {
      std::cout<<"Access Permitted";
   }

   else
   {
      std::cout<<"Access Denied";
   }
}
Topic archived. No new replies allowed.