How can I make a calculation using strings?

Feb 26, 2013 at 4:58pm
I'm having trouble using strings to display the result,
it says: no match for operator - in arg1 - arg2. Anyone has a clue?
(I can't use doubles or integers as when a character is pressed, the program rage-loops, I want it to to keep looping until a decent argument is entered for the calculation)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    string arg1, arg2;
    string tool;
    int resultadd = arg1 + arg2;
    arg1 = "0";
    arg2 = "0";
    cout << "Make a calculation. (use spaces)"; cout << '\t' << "Example: 10 [SPACE] + [SPACE] 10 [ENTER]" << endl;
    cout << "WARNING: Do not type any characters (a, b, c), as it will make the\nprogram loop to infinity." << endl << endl;
    do
    {
    cout << "Calculation: ";
    cin>>arg1;cin>> tool;cin>> arg2;
    }while ((tool != "+") && (tool != "-") && (tool != "/") && (tool != "*"));
    if (tool == "+")
    cout << "" << arg1 << " " << tool << " " << arg2 << " is: " << arg1 + arg2 << endl;
    if (tool == "-")
    cout << "" << arg1 << " " << tool << " " << arg2 << " is: " << arg1 - arg2 << endl;
    if (tool == "/")
    cout << "" << arg1 << " " << tool << " " << arg2 << " is: " << arg1 / arg2 << endl;
    if (tool == "*")
    cout << "" << arg1 << " " << tool << " " << arg2 << " is: " << arg1 * arg2 << endl;
}

Also I want to know how can I assign a for loop here so I can give the user 3 tries until it prompts it to go back to the menu or leave (code not included for that)
Feb 26, 2013 at 5:07pm
I don't think you can add strings like this.
What is the pupose of the following statement?
 
int resultadd = arg1 + arg2; // it is wrong because you haven't initialized arg1 and arg2 

To carry out string arithmetic you will have to make your own functions and treat strings as arrays of characters. Also note that the value of character '0' is not 0 but 48 in decimal notation.

Feb 26, 2013 at 5:11pm
1
2
3
4
5
6
7
int input = -1;
do
{
    std::cin.clear();
    std::cin.sync();
    std::cout << "Please enter a valid number: ";
}while(!(std::cin >> input));
It keeps asking them until they enter a valid number. Problem solved.
Feb 26, 2013 at 5:12pm
That's just a placeholder for something I tried haven't removed it yet.
What I need is the string inputs to be printed out as I can't use floaters/integers as the moment a character is pressed the program goes rage-loop.

I tried to do this, no succes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
string add (string x, string y)
{
    return x + y;
}
string min (string x, string y)
{
    return x - y;
}
string muliply (string x, string y)
{
    return x * y;
}
string decrease (string x, string y)
{
    return x + y;
}
    string arg1, arg2;
    string tool;
    string result1 = add(arg1, arg2);
    string result2 = min(arg1, arg2);
    string result3 = multiply(arg1, arg2);
    string result4 = decrease(arg1, arg2);
    cout << "Make a calculation. (use spaces)"; cout << '\t' << "Example: 10 [SPACE] + [SPACE] 10 [ENTER]" << endl;
    cout << "WARNING: Do not type any characters (a, b, c), as it will make the\nprogram loop to infinity." << endl << endl;
    do
    {
    cout << "Calculation: ";
    cin>>arg1;cin>> tool;cin>> arg2;
    }while ((tool != "+") && (tool != "-") && (tool != "/") && (tool != "*"));
    if (tool == "+")
    cout << "" << arg1 << " " << tool << " " << arg2 << " is: " << result1 << endl;
    if (tool == "-")
    cout << "" << arg1 << " " << tool << " " << arg2 << " is: " << result2 << endl;
    if (tool == "/")
    cout << "" << arg1 << " " << tool << " " << arg2 << " is: " << result3 << endl;
    if (tool == "*")
    cout << "" << arg1 << " " << tool << " " << arg2 << " is: " << result4 << endl;
Feb 26, 2013 at 5:14pm
You missed my post while you were typing. There is a way to make sure they have to enter a valid number; there's no need to mess around with numbers inside of strings.
Feb 26, 2013 at 5:17pm
You may want to look-up stringstream.

Quick example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// example of stringstream

#include <iostream>
#include <sstream> // Also includes string

int main()
{
	std::string myNumber = "55";
	int myInt;

	std::stringstream ss( myNumber );

	if( ! ( ss >> myInt ) ) // Conversion failure, output error.
		std::cout << "Error converting int.\n";
	else
		std::cout << "Your int is: " << myInt << '\n'; // Conversion success

	return 0;
}
Feb 26, 2013 at 5:17pm
Thank you L B, I haven't learned about these std:: statements or whatever they are. Just trying to make things work with what I know
I'm at chapter 6 in Jumpingintoc++: Functions and haven't seen that yet, I'll give it a shot.

Could you give a little explanation of how these std:: things work and will I see those in the book?
Feb 26, 2013 at 5:21pm
Gregzksi wrote:
Could you give a little explanation of how these std:: things work and will I see those in the book?

http://www.parashift.com/c++-faq/using-namespace-std.html
Feb 26, 2013 at 5:23pm
Thank you guys, I'll look into it tonight. Dinner time :)
Last edited on Feb 26, 2013 at 5:24pm
Feb 26, 2013 at 5:24pm
Sorry, LB. lol (:

I did see your post come up after mine.
Feb 26, 2013 at 5:30pm
It's fine - I think it was better to link to the FAQ. The OPer can always ask a more narrow question.

@Gregzki: Check this out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using std::endl;

namespace Hello
{
    void f()
    {
        std::cout << "Hi!" << endl;
    }
}

void f()
{
    using std::cout;
    cout << "This is bob." << std::endl;
}

int main()
{
    Hello::f();
    f();
    ::std::cout << "Bye!" << ::endl;
}
See if you can understand the program just by reading it - test it when you think you understand.
Last edited on Feb 26, 2013 at 5:32pm
Topic archived. No new replies allowed.