Integer conversion question

Jul 26, 2016 at 8:30am
Hi I get two errors and don't know how to act around them. First error says integer.cpp:35:10: warning: multi-character character constant [-Wmultichar]
case '10' :
while the second says integer.cpp:39:1: warning: control reaches end of non-void function
[-Wreturn-type]
}
. Could someone please tell me what I am doing wrong? Thanks.

The code, which is unfinished, is posted below:
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
#include <iostream>

using namespace std;

int converter(int a) {
  switch(a)
  {
    case '1':
    cout << "one" << endl;
    break;
    case '2':
    cout << "two" << endl;
    break;
    case '3':
    cout << "three" << endl;
    break;
    case '4':
    cout << "four" << endl;
    break;
    case '5':
    cout << "five" << endl;
    break;
    case '6':
    cout << "six" << endl;
    break;
    case '7':
    cout << "seven" << endl;
    break;
    case '8':
    cout << "eight" << endl;
    break;
    case '9':
    cout << "nine" << endl;
    break;
    case '10':
    cout << "ten" << endl;
    break;
  }
}
int main() {
  int integer;
  cout << "Input the integer: ";
  cin >> integer;
  converter(integer);

}
Last edited on Jul 26, 2016 at 8:31am
Jul 26, 2016 at 8:41am
#include <iostream>

using namespace std;

void converter(int a) {
switch(a) {
case 1:
cout << "one" << endl;
break;
case 2:
cout << "two" << endl;
break;
case 3:
cout << "three" << endl;
break;
case 4:
cout << "four" << endl;
break;
case 5:
cout << "five" << endl;
break;
case 6:
cout << "six" << endl;
break;
case 7:
cout << "seven" << endl;
break;
case 8:
cout << "eight" << endl;
break;
case 9:
cout << "nine" << endl;
break;
case 10:
cout << "ten" << endl;
break;
}
}
int main() {
int integer;
cout << "Input the integer: ";
cin >> integer;
converter(integer);
return 0;
}
Last edited on Jul 26, 2016 at 8:45am
Jul 26, 2016 at 8:42am
In line 40, you tell the compiler that the main function will return an integer value.
So in line 45, you should return an integer value. That will solve your second error.

For the first error, I am not sure but I think it might have to do with your switch-cases
1
2
3
switch(a)
  {
    case '1':

a is an integer while '1' is a character with ascii code 49, representing the letter 1. Try removing the single quotes.

Kind regards, Nico
Last edited on Jul 26, 2016 at 8:43am
Jul 26, 2016 at 8:42am
Hi,
Firstly :
int converter(int a)

Should be :
void converter(int a)
Jul 26, 2016 at 8:46am
@Peoples

Thanks it sorted first error, and I figured out how to solve the second error.
Last edited on Jul 26, 2016 at 8:51am
Jul 26, 2016 at 8:47am
Secondly :
1
2
3
case '1' : 
    cout << "one" << endl;
    break;

Please do not wrap your numbers with ' '.

==>
1
2
3
case 1 : 
    cout << "one" << endl;
    break;


Repeat this for numbers from 2-10.
Jul 26, 2016 at 8:47am
Does that help you? :)
Jul 26, 2016 at 8:50am
@closed account

Yeah, that helps. thanks.
Jul 26, 2016 at 9:31am
Glad it helped :)
Topic archived. No new replies allowed.