Reputation: 11
I'm a beginner in c++ so I'm just messing around with some stuff while reading articles and books. But I spent 20 minutes re-reading this over and over again and I can't tell what's wrong with it.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
cout << "Hello there, this is your personal simple calculator.";
cin.get();
cout << "Type in what you want to do. (Addition, Subtraction, Multiplication, Division)"<< endl;
string c;
getline (cin, c);
if (c == "Addition")
{
string a_1;
string a_2;
cout << "You chose addition. Press enter" << endl ;
cin.get();
cout << "Type in the first value: ";
getline( cin, a_1);
cout << "Type in the second value: ";
getline (cin, a_2);
cout << a_1 << " + " << a_2 << " = " << a_1 + a_2 << endl;
}
else
{
cout << "You spelled it wrong.";
return 0;
}
if ( c == "Subtraction")
{
string s_1;
string s_2;
cout << "You chose subtraction. Press enter" << endl ;
cin.get();
cout << "Type in the first value: ";
getline (cin, s_1);
cout << "Type in the second value: ";
getline (cin, s_2);
cout << s_1 << " - " << s_2 << " = " << s_1 - s_2 << endl;
}
}
I get this as the only error
42 83 C:\Users\Jason\Desktop\Lesson - Header Files\LH1.cpp [Error] no match for 'operator-' in 'first_argument - second_argument'
I don't get it. The addition sign works and everything but the subtraction works. So I messed around with something else
cout << first_argument << " - " << second_argument << " = " << first_argument - second_argument << endl;
But that subtraction part works fine. I don't get it. Help please
Upvotes: 0
Views: 581
Reputation: 199
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main()
{
cout << "Hello there, this is your personal simple calculator.";
cin.get();
cout << "Type in what you want to do. (Addition, Subtraction, Multiplication, Division)" << endl;
string c;
getline(cin, c);
if (c == "Addition")
{
int a_1;
int a_2;
cout << "You chose addition. Press enter" << endl;
cin.get();
cout << "Type in the first value: ";
cin >> a_1;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Type in the second value: ";
cin >> a_2;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << a_1 << " + " << a_2 << " = " << a_1 + a_2 << endl;
}
else if (c == "Subtraction")
{
int s_1;
int s_2;
cout << "You chose subtraction. Press enter" << endl;
cin.get();
cout << "Type in the first value: ";
cin >> s_1;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Type in the second value: ";
cin >> s_2;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << s_1 << " - " << s_2 << " = " << s_1 - s_2 << endl;
}
else
{
cout << "You spelled it wrong.";
return 0;
}
}
Use if else statements because otherwise your code won't have a chance to check to see if the user is trying to Subtract. It is best to leave the else at the very end. Also changed the strings to ints because strings cannot subtract as they are not numbers. On a final note, I used the cin.clear() & cin.ignore() to flush the cin buffer.
Upvotes: 0
Reputation: 54325
The addition part works because string + string results in stringstring. It appends the two strings and returns a new string.
But subtracting two strings doesn't mean anything.
What I believe you actually want to do is convert the strings into numbers and then subtract the numbers.
To do that, you need to use something like the following:
double val_1, val_2;
cin >> val_1;
cin >> val_2;
cout << "result is " << (val_1 - val_2) << endl;
I put the subtraction inside parenthesis because I believe the <<
aka "shift" operator is on the same level as multiplication, which means that without them it would try to evaluate ("result is " << val_1) - (val_2 << endl).
Since I am not sure on operator precedence I checked http://en.cppreference.com/w/cpp/language/operator_precedence and found that << is lower than subtraction, so my parenthesis weren't necessary.
Upvotes: 1
Reputation: 5606
string
can deal with text. When you add two strings, they are concatenated ("2"+"2"=="22"
, not "4"
). String doesn't have operator-
.
To deal with floating-point numbers, use double
. To deal with integers, use int
:
double d1, d2;
//some output
cin >> d1;
//some output
cin >> d2;
cout << d1 << " - " << d2 << " = " << (d1-d2) << '\n';
Upvotes: 3