Invalid char conversion error

I'm having problem to run this code involving the following function, I was trying to do conversion from char to integer. It ran well when I didn't use function. The sixth line gets me error. I was trying to get it worked as same function as the second code below but when I use function it didn't run well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void EnterTime (char& time1, char& time2)
{
        stringstream str, str2;

        cout << "Call started at (hhmm): ";
        cin.get(time1, 3); cin.get(time2, 3);


        str  << time1;
        str2 << time2;

        int w, x;
        str  >> w;
        str2 >> x;
        cout << w << endl;
        cout << x << endl;
}



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
#include <iostream>
#include <sstream>

using namespace std;

int main()

{
char code[3], code2[3];
stringstream str, str2;

cout << "Enter your code : ";
cin.get(code, 3); cin.get(code2, 3);

str  << code;
str2 << code2;

int w, x;
    str >> w;
    str2 >> x;

    cout << w << x << endl;
    cout << w + x << endl;
    cout << code << " " << code2;
}


Below is my whole coding :
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
#include <iostream>
#include <cstdlib>
#include <sstream>

using namespace std;

void EnterTime (char& time1, char& time2)
{
        stringstream str, str2;

        cout << "Call started at (hhmm): ";
        cin.get(time1, 3); cin.get(time2, 3);


        str  << time1;
        str2 << time2;

        int w, x;
        str  >> w;
        str2 >> x;
        cout << w << endl;
        cout << x << endl;
}

void YesNo (char& select)
{
        cout << "\nDo you want to continue? ";
        cin >> select;
        select = toupper(select);


}

int main()
{

char select, time1[3], time2[3];
stringstream str, str2;

do
{
    
    EnterTime(time1, time2);

    YesNo(select);

}while (select == 'Y');


return 0;

}


Anyone can help?...
time1 and time2 are single characters, code and code2 are arrays of 3 characters.
Dear LB,

time1 and time2 are single characters, code and code2 are arrays of 3 characters

Are you sure?

Take a look at the code. I did declare time1 and time2 are arrays of 3 characters in int main() function.
Last edited on
 
void YesNo (char& select)
select is a single character.

 
void EnterTime (char& time1, char& time2)
time1 is also a single character.

If you want to pass an array to a function, do either this:
void EnterTime (char * time1, char * time2)
or this:
void EnterTime (char time1[], char time2[])

Dear Chervil,

If you want to pass an array to a function, do either this:
void EnterTime (char * time1, char * time2)
or this:
void EnterTime (char time1[], char time2[])


I've tried your suggestion and it did get me no error but the output still didn't work out the way I want it to be as shown in the code 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
#include <iostream>
#include <sstream>

using namespace std;

int main()

{
char code[3], code2[3];
stringstream str, str2;

cout << "Enter your code : ";
cin.get(code, 3); cin.get(code2, 3);

str  << code;
str2 << code2;

int w, x;
    str >> w;
    str2 >> x;

    cout << w << x << endl;
    cout << w + x << endl;
    cout << code << " " << code2;
}



How do you convert char to int? Can I use stringstream? Can you teach me?..
How do you convert char to int?

I think you may mean a string of characters? If so, then yes, stringstream will work.

But your function EnterTime () uses cin to get its input, so there's no need to pass any parameters. Just define everything within the function.
Topic archived. No new replies allowed.