What is wrong with my casting in this program?

I am having trouble casting an integer to string in this program.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "stdafx.h"
#include <sstream>

using namespace std;

int x1,x2,x3,x4,x5;
int y1,y2,y3,y4,y5;
string str1,str2,str3;

int main()
{
  //question 1
  cout << "x1 + y1 = ?" << endl;

  cout << "value for x1:" << endl;
  cin >> x1;

  cout << "value for y1:" << endl;
  cin >> y1;

  int add = cpu.add_(x1,y1);

  cout << "the answer to " << x1 << " + " << y1 << " = " << add << endl;

  //question 2
  cout << "x2 * y2 = ?" << endl;

  cout << "value for x2:" << endl;
  cin >> x2;

  cout << "value for y2:" << endl;
  cin >> y2;

  int mult = cpu.mult_(x2,y2);

  cout << "the answer to " << x2 << " * " << y2 << " = " << mult << endl;

  //question 3
  cout << "x3 - y3 = ?" << endl;

  cout << "value for x3:" << endl;
  cin >> x3;

  cout << "value for y3:" << endl;
  cin >> y3;
            
  
  int sub = cpu.sub_(x3,y3);

  string x33,y33;
  stringstream x31,y31;
  x31 << x3;
  x33 = x31.str();
  y31 << y3;
  y33 = y31.str();

  if (x3 > y3) { str1 = "the answer to " + x33 + " - " + y33 + " = " + sub; } else { str1 = "the answer to " + y33 + " - " + x33 + " = " + sub; }

  cout << str1 << endl;

  //question 4
  cout << "x4 / y4 = ?" << endl;

  cout << "value for x4:" << endl;
  cin >> x4;

  cout << "value for y4:" << endl;
  cin >> y4;

  int div = cpu.div_(x4,y4);

  string x44,y44;
  stringstream x41,y41;
  x41 << x4;
  x44 = x41.str();
  y41 << y4;
  y44 = y41.str();

  if (x4 > y4) { str2 = "the answer to " + x44 + " / " + y44 + " = " + sub; } else { str2 = "the answer to " + y44 + " / " + x44 + " = " + div; }

  cout << str2 << endl;

  //question 5
  cout << "x5 % y5 = ?" << endl;

  cout << "value for x5:" << endl;
  cin >> x5;

  cout << "value for y5:" << endl;
  cin >> y5;
  
  int mod = cpu.mod_(x5,y5);

  string x55,y55;
  stringstream x51,y51;
  x51 << x5;
  x55 = x51.str();
  y51 << y5;
  y55 = y51.str();

  if (x5 > y5) { str3 = "the answer to " + x55 + " % " + y55 + " = " + sub; } else { str3 = "the answer to " + y55 + " % " + x55 + " = " + mod; }

  cout << str3 << endl;

  //BREAK

  cout << "BREAK------------------------------" << endl;
  cout << "the answer to " << x1 << " + " << y1 << " = " << add << endl;
  cout << "the answer to " << x2 << " * " << y2 << " = " << mult << endl;
  cout << str1 << endl;
  cout << str2 << endl;
  cout << str3 << endl;

  int nul;
  cin >> nul;

  cout << "goodbye" << endl;
  return 0;
}                                                               


Here is stdafx.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

class cpu {

public:
  virtual int add_(int x, int y) { return x+y; } // addition
  virtual int mult_(int x, int y) { return x*y; } // multiplication
  virtual int sub_(int x, int y) { if(x >= y){ return x-y; } else { return y-x; } } // subtract
  virtual int div_(int x, int y) { if(x >= y){ return x/y; } else { return y/x; } } // divide
  virtual int mod_(int x, int y) { if(x >= y){ return x%y; } else { return y%x; } } // moduli

} cpu;


Here is the error message

1
2
3
4
5
6
7
8
$ g++ main.cpp -o cpu
main.cpp: In function ‘int main()’:
main.cpp:56: error: no match foroperator+’ in ‘std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" = ")) + sub’
main.cpp:56: error: no match foroperator+’ in ‘std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" = ")) + sub’
main.cpp:78: error: no match foroperator+’ in ‘std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" = ")) + sub’
main.cpp:78: error: no match foroperator+’ in ‘std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" = ")) + div’
main.cpp:100: error: no match foroperator+’ in ‘std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" = ")) + sub’
main.cpp:100: error: no match foroperator+’ in ‘std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" = ")) + mod’


I have tried every conceivable way (that I think) to go about this. What am I doing wrong?
You do realize that you can't just add to string literals, right? You'll probably want to convert the first string literal on each of those lines to std::strings (with string()) first. :/

-Albatross
Last edited on
You do convert x3 and x4 to strings, but you forget to do that for sub, div and mod.
Though it would be much more simple to have
1
2
if (x4 > y4) cout << "the answer to " << x4 << " / " << y4 << " = " << sub;
else cout << "the answer to " << y4 << " / " << x44 << " = " << div;

or
cout << "the answer to " << max(x4,y4) << " / " << min(x4,y4) << " = " << sub;
You do realize that you can't just add to string literals, right? You'll probably want to convert the first string literal on each of those lines to std::strings (with string()) first. :/


I have tried that; that was my first idea; then my compiler gave out more errors;

Now, Hamsterman, yours was the best I have heard, I will try it.
Topic archived. No new replies allowed.