interesting error message for my if's

hi all,

I'm getting the following error for the following if statement

"Error 4 error C2106: '=' : left operand must be l-value"

if (serv = "p" || serv = "P"){

to be quite honest, I don't know what the error message is talking about. The if statement seems fine to me, but then there must be something wrong...
closed account (zb0S216C)
That line of code doesn't do what you think it does. This is what it actually does:

These conditions are assignments. serv is assigned to the address of "p". After the assignment, serv is then evaluated. If it evaluates to true, then the block is entered. Otherwise, serv is assigned to "P". Once the assignment has been made, serv is then evaluated again. If it evaluates to true, the block is entered. Otherwise, the block is ignored completely.

Note that, however, C++ short-circuits conditions such as the one you've written. By this I mean if the left-most condition evaluates to true, the right-most condition is never tested. Though, this is not the case for &&.

Wazzak
oh, hm.

then i guess i mis-understand whats going on. what i'm trying to do is to see if the user inputs "p" or "P" then do whatever else underneath the if statement.

if this line of code doesn't do that, then what does?

Edit: if it would help, i'll be more then happy to copy and paste the program as it stands now. I think i've got the code about right. just can't get past this problem.
Last edited on
= - this is assignment operator
== - this is compare operator.

http://www.cplusplus.com/doc/tutorial/operators/
Last edited on
hm, when I replace all the = with ==, i get a whole new set of errors on top of my older ones. The error message says "No conversion from 'cont car *' to 'int' "
It seems you are trying to add number int to char pointer.
As I can guess your variable 'serv' is defined as int and you are trying to compare the integer with the string literal "p".

You shall write

if ( serv == 'p' || serv == 'P' )

that is to use 'p' instead of "p"
Last edited on
hm, serv is defined as a character.

and single quotes, combined with double equal signes, I was using the oposite.
Try to use a single quote. Then tell us the results. If they are bad, then probably you are doing something wrong. From the error message, serv is a int, not a char. Check twice.
ha! that got rid of those errors. But i'm having trouble with the others. if you all wouldn't mind, heres the code.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
int main()
{
    char serv, choice;
    double accountnumber, min, min2, min3, min4, total, daymin, daymin1, daymin2, daytotal, nightmin, nightmin1, nightmin2, nighttotal;

    cout << "What is your Account number?" << endl;
    cin >> accountnumber;

    cout << "What type of service do you have?  Remember, R is regular and P is premium" << endl;
    cin >> serv;

    if(serv == 'r' || serv == 'R' || serv == 'p' || serv ==' P'){

	   if (serv == 'r' || serv == 'R'){

	        cout << "How many minutes did you use?" << endl;
	        cin >> min;

	        if (min > 50){
	      
	            min2 = min - 50;
		        min3 = min2 * .20;
		        total = min3 + 10;

		        cout << accountnumber << "\t" << serv << "\t" << min << "\t" << "$" << total << endl;
			
			};
		    else{

			    cout << accountnumber << "\t" << serv << "\t" << "Your Bill is : $10" << endl;
           };

    };

		if (serv == 'p' || serv == 'P'){

	        cout << "How many day time Minutes did you use?  Remember, daytime minutes are those used between 6AM and 6PM" << endl;

	        cin >> daymin;

	        cout <<"How many night time Minutes did you use?  Remember, night time minutes are those used between 6PM and 6AM" << endl;

		cin >> nightmin;

		        daymin1 = daymin - 75;
			daymin2 = daymin1 * .10;
			daytotal = daymin2 + 25;

			nightmin1 = nightmin - 100;
			nightmin2 = nightmin1 * .05;
			nighttotal = nightmin2 + 25;



    };
    };

    else
        cout << "Invalid input" << endl;

return 0;
}


the errors are saying both else's have no matching ifs
Last edited on
An "Correct" if looks like this:
1
2
3
4
if( Check )
{
   // ...
}

and NOT like this:
1
2
3
4
if( Check )
{
   // ...
};

See the difference?
oh! the semi colins! thank you, so much!
Semi Colons* :d
Topic archived. No new replies allowed.