• Register
1 vote
823 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,260 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 127 views
127 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.4k points
0 votes
1 answer 9 views
9 views
Problem: I have written following program: #include<iostream> using namespace std; int main() { const char *name_of_day = "Wednesday"; if(*name_of_day == "\0") { cout<<"There is no name of the day stored"; } else { cout<< *name_of_day; } } When I run this program, ... with them recently. Moreover, I want to print the value of the pointer on the screen. Am I doing it in the right way? Please help!
asked Mar 17 Code Learner 9.8k points
0 votes
1 answer 8 views
8 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 13k points
0 votes
1 answer 29 views
29 views
Problem: Why Iso c++ forbids variable length array?
asked Feb 14 jasimchaudary 1.5k points
0 votes
1 answer 33 views
33 views
Problem: I get the following warning; Exercise2-3.cpp:46: warning: comparison between signed and unsigned integer expressions Any Suggestion on how to fix this?
asked Mar 15 ummesalma 29.2k points
1 vote
1 answer 78 views
78 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 5 views
5 views
Problem: Can somebody help me with this &ldquo; Iso c++ forbids in-class initialization of non-const static member&rdquo;?
asked 3 days ago Raj Tyagi 14.4k points
1 vote
1 answer 36 views
36 views
Problem: I need to convert normal text into Morse code and I get the error likre this. ISO c++ forbids converting a string constant to 'char*'.
asked Mar 30 PkGuy 23.5k points
0 votes
1 answer 43 views
43 views
Problem: Hello! I am trying to run the following code on my PC: #include<stdio.h> void main() { int number_1,number_2,result; char *operation; printf("Enter the first number: "); scanf("%d",&number_1); printf("Enter the second number: "); scanf("%d",&number_2) ... to know why is it giving these warnings? Moreover, what I can do to remove these warnings? Do they affect the working of my program?
asked Mar 17 Code Learner 9.8k points
0 votes
1 answer 14 views
14 views
Problem: If the address of the first node of a linked list consisting of an integer and a pointer to the next node is 1000, what will be the address of the second node of the same linked list? Supposing two nodes are allocated contiguously.
asked Apr 25 Moumita Bhowmick 12.4k points