&& Operator

Hi, I know that you can use '&&' to say 'and' for ints. However, how do you say 'and' for strings? The compiler gives me this error:


1>------ Build started: Project: Project6, Configuration: Debug Win32 ------
1>  Project6.cpp
1>Project6.cpp(14): error C2677: binary '&&' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


When I run this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "StdAfx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
	string username;
	string password;
	cout << "Username: ";
	getline(cin, username);
	cout << "Password: ";
	getline(cin, password);
	if(username == "username" && password = "password") //Line 14
	{
	}
}


So is there no way to say 'and' when using strings?
You're using the wrong == there the second time.

-Albatross
Albatross is correct. You will want to use another == after the first one.
Your code will look like this on line 14.
1
2
3
if(username == "username" && password == "password")
	{
	}



Joshua Ward
UAT Programming Student
Thanks that fixes it.
It might make it less confusing if you encase those statements in parentheses, like so:

 
if((username == "username")&&(password == "password"))
@^
Why? People should KNOW that!
The ordering of operations in C/C++ is:
selection+preincrement >
negation,not+postincrement >
multiplication+division+modulo >
addition+subtraction >
shifting >
relationals >
bitand > bitxor > bitor >
and > or > ternary >
assignment >
commas.
Last edited on
@packetpirate: IMO, that's just uglier and slightly less readable. When you have a messy expression, though, sometimes it does make sense to add a few unneeded parentheses.
Topic archived. No new replies allowed.