• Register
1 vote
367 views

Problem:

I need help with a calculator I created. It keeps giving me the error; "ISO C++ forbids comparison between pointer and integer" and "invalid conversion from `const char*' to `char'".

Here's the code:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
   char operation; // operation
   double first; //first number
   double second; // second number
   char operationd; // displays operation later on
   double result; // sum/difference/product/quotient
   
cout << "Calc v2 "<< endl; 
           cout << "by Marvolo1300" << endl;
           cout << " "<< endl;
           
           cout<< " Choose your first number"<< endl;
           cin >> first;
           
           cout<< "You have chosen"<< first;
           
           cout<< "Choose the operation";
           cin >> operation;
           
           if (operation=="+")
           {
                           operationd= "Addition (+)";
           }
           
           
           
           if (operation =="-")
           {
                          operationd= "Subtraction (-)";
                          }
                          
                          
            if (operation=="*")
            {
                            operationd= "Multiplication (*)";
                            }
                            
            if (operation=="/")
            {
                            operationd= "Division (/)";
                            }
                            
           
           cout<< " You have chosen"<< operationd<< endl;
           
           
           cout<< "Choose the second number"<< 
           cin >> second;
           
           if (operationd=="+")
              {
                            cout<< "The sum is"<< first + second<< endl;
                            }
           
           if (operationd=="-")
              {
                            cout << "The difference is" << first - second << endl;
                            }
                            
           if (operationd=="*")
           
              {
                            cout<< "The product is" << first * second<< endl;
                            }
                            
           if (operationd=="/")
              {
                               cout<< "The quotient is" << first / second<< endl;
                               cout << ""<< endl;
                               cout << ""<<endl;
                               }
                               
           cout<< "Press ENTER to exit";
           
           cin.ignore(LONG_MAX,'\n');
           cin.get();
           
           return 0;
   
}

I am struggling with the code for a long time and couldn’t figure out the problem. Could you please help me out?
Thanks

12 7 7
15,250 points

2 Answers

0 votes

Solution:

The double quotes (") in line like if (operation=="+") should be a single quote ('). The double-quote makes it a string literal, a single quote makes is a character literal.
and line like operationd= "Addition (+)"; you are trying to assign a string to a char.

In your code, operationd is declared as a char but it appears that your intentions are to store a string-representation of operation in it

Please follow the code below to see the best practice of declaring a string.

 
#include <string> // on some implementations this is done for you in iostream 

string s;
s = "Addition (+)";
cout << s << endl;

Note: Different types of quotes that surround char literals and string literals.

13 9 6
94,240 points
0 votes

Solution:"

You have two ways to solve this. The preferred method is to use:

string answer;

(instead of char). The other possible method to solve it is:

if (answer == 'y') ...

note single quotes instead of double, illustrating a char constant

A string literal is delimited by quotation marks and is of type char* not char.

Example: "hello"

Hence at the time you compare a char to a char* you will obtain that similar compiling error.

char c = 'c';
char *p = "hello";

if(c==p)//compiling error
{
} 

To solve exercise a char literal which is delimited by single quotes.

Example: 'c'

You should remember to employ single quotes for char constants. So exercise

if (answer == 'y') return true;

Rather than

if (answer == "y") return true;

 

10 6 4
31,120 points

Related questions

1 vote
1 answer 32 views
32 views
Problem : I am very new to C++ programming. I am facing below error if I try to execute my code. Please find below the error: Warning: comparison between signed and unsigned integer expressions . Please find below my erroneous code: vector<long int> row;     long n,m;     long ... ;i<row.size();i++) I am looking for quick fix for my above error can some c++ expert help me in fixing above error ?
asked Apr 25 stewart 4k points
0 votes
1 answer 5 views
5 views
Problem : What are the differences between a pointer variable and a reference variable in c++?
asked Oct 8 sikandar 2.4k points
0 votes
1 answer 15 views
0 votes
1 answer 6 views
6 views
What is the difference between structure and union in c++?
asked Oct 10 sikandar 2.4k points
0 votes
1 answer 6 views
6 views
What is the difference between while loop and do-while loop?
asked Oct 9 sikandar 2.4k points