-2

There are 2 errors expression must be a modifiable lvalue (line 12) and expected a statement (line 25). I am new to c++ and i only know scratch and qbasic. I have been sitting in front of this error for almost 2 hours now.

Code:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int d;
        cout << "Do you want Addition or Subtraction? (a/s)";
        cin >> d;

        if ('d' = "s");
    {
        int a, b;
        cout << "Enter any number: ";
        cin >> a;
        cout << "Enter another number: ";
        cin >> b;
        int c = a + b;
        cout << "The sum is ";
        cout << c;

        return 1;

    }else{
        int e, f;
        cout << "Enter any number: ";
        cin >> e;
        cout << "Enter another number: ";
        cin >> f;
        int g = e + f;
        cout << "The sum is ";
        cout << g;

        return 0;

    }
}
  • Recommendation: Use descriptive variable names (and function names when you get there). Good names make code easier to read because they give information about what a variable contains and how it should be used. The immediate benefit is it reduces the amount of commenting your code requires. In addition, the human brain has a really annoying habit of seeing the expected letter when the fingers typed in the wrong one. With a whole word if you screw up one character, the compiler usually catches the mistake. With a single character you can spend hours looking for the `i`-> `j` mistake. – user4581301 Nov 15 '20 at 05:37

1 Answers1

3

Change

   if ('d' = "s");
      {

to

 if (d == 's')
  {

This fixes 4 problems in your code

  1. the variable name is d & not 'd' (remove the single quotes around d in the if check. d is the variable int d. 'd' is the character d.

  2. Conditional Check should be == & not =. = is assignment. == is conditional check. This is the cause for the l-value error (https://softwareengineering.stackexchange.com/questions/155665/what-are-the-key-terms-rvalue-and-lvalue)

  3. The RHS of the conditional check should be 's' and not "s". Something between single quotes is a character. Something between double quotes is a string.

  4. There should be no semicolon at the end of the if check. Remove it.

  • Also change int d to char d
user93353
  • 13,733
  • 8
  • 60
  • 122
  • 1
    To expand on this: The `=` instead of `==` means asker is performing an assignment and checking if the result evaluates to `false` instead of performing a comparison. The double-quotes around `"s"` instead of single-quotes make it a C-style string literal, not a character literal. And the `;` after the `if` statement meant that when the `if` evaluates to `true`, nothing happens; the block below it would execute normally, and the `else` block way below is malformed. – Nathan Pierson Nov 15 '20 at 05:32
  • [Since this is C++, rvalues and lvalues are only the start of the fun](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues). – user4581301 Nov 15 '20 at 05:39
  • And in fact note that `int d` isn't what asker would want to use if you're trying to read a character in anyway, so earlier in the program it should be `char d` (or, preferably, a more descriptive variable name entirely). – Nathan Pierson Nov 15 '20 at 05:41
  • @NathanPierson - no. `int` is the right type to use to input a char - because EOF (-1) is int - https://www.eskimo.com/~scs/cclass/notes/sx6b.html – user93353 Nov 15 '20 at 05:46
  • @user93353 This is a C++ question. Your text there is about using `getchar` in C, not `std::istream`s in C++. – Nathan Pierson Nov 15 '20 at 05:48
  • @NathanPierson true. Updated the answer. – user93353 Nov 15 '20 at 05:49