0

Have to do this for the first cs course I'm taking. It's a basic calculator that takes an operator and a value and calculates the total (total beginning at 0).

#include <iostream>

using namespace std;

int main()
{
    char oprtr;
    float value, total = 0.0;

    cin >> oprtr >> value;

    while (oprtr != "q")
    {
        if (oprtr == "+")
            total += value;
        else if (oprtr == "-")
            total -= value;
    }
}

It's not finished but already having issues with it. It gives errors saying something along the lines of "prohibits comparing char values to int values"

Tyrone Smith
  • 9
  • 1
  • 2

4 Answers4

10

Double quotes ("q") are for strings. Single quotes ('q') are for characters.

So:

while (oprtr != 'q')
{
    if (oprtr == '+')
        total += value;
    else if (oprtr == '-')
        total -= value;
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
1

Char means Character and you must use single quotes '' for these, double quotes "" are for strings.

The reason you are getting this error is because you're attempting to compare a character to a string literal (a const char), the exact error you're getting will be:

Operand types are incompatible ("char" and "const char").

The below code will fix this error:

#include <iostream>

using namespace std;

int main()
{
    char oprtr;
    float value, total = 0.0;

    cin >> oprtr >> value;

    while (oprtr != 'q')
    {
        if (oprtr == '+')
            total += value;
        else if (oprtr == '-')
            total -= value;
    }
}
AStopher
  • 4,207
  • 11
  • 50
  • 75
1

And also, since you are reading the statement or the expression once, there is no need for you to loop while the character is not equal to 'q'. You should basically perform one operation. Also, switch is a very useful construct for comparing literals instead of several if's. So I would simplify that as.

#include <iostream>

using namespace std;

int main(){
    char op;
    float value, total = 0.0;

    cin >> op >> value;
    //it is important at this stage to check for errors, as they are most likely
    //to occur.
   if(!cin){
      cerr << "Error: format unknown! \n"; //basic error handled here, the prog outputs the error
   }

    //now perform the calculation
    switch(op){
       case '+': total += value;
        break;
       case '-' : total -= value;
        break; 
       case 'q' : break;        //i presume q is your quit character
       default: /*unknown character*/ cerr << "Unknown operation! \n";
   }

  cout << "Total: "<<total << endl;

  return 0;
}

this basically reads in one expression and add it to the total. You can modify it to read as long as you want.

shaddyshad
  • 217
  • 4
  • 15
-3

Comparing string length is a common function in C programming, as it allows you to see which string contains more characters. This is very useful for sorting data. Comparing strings requires a special function; do not use != or ==.

http://www.techonthenet.com/c_language/standard_library_functions/string_h/strcmp.php

user3661321
  • 103
  • 1
  • 10