Keep getting error 86:23: error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'void')

The goal of this program is to convert from number to English text. I am a beginner C++ programmer so please don't give complex explanations. Maybe someone can give me another method to approach the algorithim.

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
  // Example program
#include <iostream>
#include <string>
#include <sstream>

using namespace std;


void less_hundred(int number);
void onsy(int number);

int main()
{
  
  int number;
  cout << "Enter a number: ";
  cin >> number;
  
  if (number < 100)
  {
      less_hundred(number);
  }

}  
 
 
void ones(int number)
{
    string onsy;
    int j = number % 10;
    
    if (j == 1)
    {
        onsy = "one";
    }
    
    cout << onsy;
}        
 
void less_hundred(int number) 
{
    string ty;
    int i = number / 10;
    
    
    if (i == 2)
    {
        ty = "twenty";
    }
        
    if (i == 3)
    {
        ty = "thirty";
    }
    
    if (i == 4)
    {
        ty = "fourty";
    }
    
    if (i == 5)
    {
        ty = "fifty";
    }
    
    if (i == 6)
    {
        ty = "sixty";
    }
    
    if (i == 7)
    {
        ty = "seventy";
    }
    
    if (i == 8)
    {
        ty = "eighty";
    }
    
    if (i == 9)
    {
        ty = "ninty";
    }
 
    cout << ty << ' ' << ones(number);
        
        
  
}
Your function called ones is void - it does not return a value.
The << operator in this case expects a value like a string or an int.

Since you already have a cout statement internally in this function, you'd probably just want to do:
1
2
cout << ty << ' ';
ones(number);

or something similar.


To make it look nicer, the other solution would be to make your ones function return the string you made inside it.

___________________
As a side note, your ones function doesn't seem complete ("two", "three" ...), but you probably already know that.
Last edited on
Thanks also is there another way i should do this problem. The text says to:

1) Break the number into a chunk of three digits.

so how would i break up say 200560 into 200 560

I'm confused how to do this with the minimal amount of C++ that I know.
Well here's some hints:
Are you aware of the modulus ( % ) operator and integer division?
Basically, 238 % 10 = 8 because 8 is the remainder when you divide 238 by 10.
and, 238 / 10 = 23.

You can use these two operations to get the first and last digits of any number.

Then, you can do more manipulation to get two separate numbers as the final result.
_________________________

Another way to do it is to use std::stringstreams. They can convert strings into numbers and back using the << and >> operators. They can be really convenient and powerful.
Here's a small example you can work from:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <sstream> // std::stringstream
#include <string> // std::string
int main()
{
    using namespace std;
    
    int x = 5;
    stringstream ss;
    ss << x; // store x into ss
    
    string my_string = ss.str() + " as a string!";
    cout << my_string << endl;
    
    int x2;
    ss >> x2; // will fail if the current stringstream can't be represented as an integer.
    cout << x2 << " as a number again!" << endl;
}
Last edited on
Topic archived. No new replies allowed.