• Register
1 vote
643 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

Please log in or register to answer this question.

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

0 votes
1 answer 29 views
29 views
Problem Hey, I Am getting a lot of errors. I don&rsquo;t know how to resolve these errors. Can somebody help me to resolve them? This will be great for me to make me relax after solving this problem Here&rsquo;s the following errors, I am facing ISO C++ forbids ... ttTree::ttTree(void) {    } ttTree::ttTreeInsert(int value) { } ttTree::ttTreeDelete(int value) { } ttTree::ttTreePrint(void) { }
asked Dec 24, 2020 chris jordan 2.2k points
0 votes
1 answer 6 views
6 views
Problem: I am currently working through Accelerated C++ and have come across an issue in exercise 2-3. A quick overview of the program - the program basically takes a name, then displays a greeting within a frame of asterisks - i.e. Hello ! surrounded framed by *'s. The ... . They then ask the reader, as part of the exercise, to ask the user for input as to how big they want the padding to be
asked Dec 13, 2020 Mashhoodch 5.7k points
1 vote
1 answer 47 views
47 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, 2020 stewart 4k points
0 votes
1 answer 14 views
14 views
Problem : What are the differences between a pointer variable and a reference variable in c++?
asked Oct 8, 2020 sikandar 2.4k points
0 votes
1 answer 11 views
11 views
Problem: When creating an array of characters I don't have to put const to it to be valid char str [11] = "Hello World"; Instead when I create it with a pointer I have to do it as follows const char * str = "Hello World"; I understand that it is ... why the same does not happen with an array, I can create it without prepending const and I even have the possibility to modify the assigned literal.
asked Nov 5, 2020 sasha 5.3k points
0 votes
1 answer 52 views
0 votes
1 answer 3 views
3 views
Problem: Hi there! I am a beginner in programming, and I recently came across the concept of call by reference and call by address. To understand the concept better, I wrote the following programs: Call by address: #include <iostream> using namespace std; void ... So, I got more confused now. Could anybody here please explain the difference between call by address(pointer) and call by reference?
asked Jan 4 Code Learner 5.5k points
0 votes
1 answer 17 views
17 views
Problem Hey, what is the difference between null and null pointer. Answer me with the help of an example.
asked Dec 17, 2020 chris jordan 2.2k points
0 votes
1 answer 8 views
8 views
Problem: Why do we use int main in C++? How does a main function in C++ differ from main in C? Why void main is wrong? How to know the exit status of your program. That's the reason why you have the int main() -- you return your exit status. It's wrong because this is not what the C++ Standard specifies as a legal main . Nobody cares about what the other languages specify?
asked Dec 12, 2020 Mashhoodch 5.7k points
0 votes
1 answer 7 views
7 views
Problem: Which is better C++ or Java? Which is more difficult C++ or Java? What is the difference between the for statement of C++ and that of Java?
asked Dec 10, 2020 Mashhoodch 5.7k points