Error with conversion: char * to char

Hello There,

I was trying to pass the char value to a function and there compare the character with another character and return if its true or not. I get the error.

ERRORS:
=======

functionall.cpp: In function `int main()':
functionall.cpp:135: error: invalid conversion from `char*' to `char'
functionall.cpp:135: error: initializing argument 1 of `char namec(char, char&)'
functionall.cpp: In function `char namec(char, char&)':
functionall.cpp:196: error: invalid conversion from `char' to `const char*'
functionall.cpp:196: error: initializing argument 1 of `int strcmp(const char*, const char*)'


My only issue is with name checking.. option 7. I have only implemented 1,2,3,7 options in my program. It would be case 7 in this case.

Please help me solve this.


MY CODE:
=======
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

#include <iostream>

using namespace std;


// Function Prototypes
float currency(float rcur, float ecur, float & exc);
float mm(float inches, float & ina);
float in(float mms, float & mma);
float addm(float adder, float & ans);
float sub(float num1, float num2, float & ans);
float mul(float num1, float num2, float & ans);
float div(float num1, float num2, float & ans);
char namec(char namechk, char & names);

int main()
{

int selection, twoselection;
float rcur;
float ecur;
float exc;

cout <<"This is the program to do the following. Select as desired: "<<endl;
cout <<"1: Currency"<<endl;
cout <<"2: Inches to MM"<<endl;
cout <<"3: ADD"<<endl;
cout <<"4: Subtract"<<endl;
cout <<"5: Multiply"<<endl;
cout <<"6: Divide"<<endl;
cout <<"7: Name Check"<<endl;

cin >> selection;

 switch (selection)
	{
	case 1:
		cout <<"This is the option to convert currency: "<<endl;
		cout <<"Enter actual currency: ";
		cin >> rcur;
		cin.ignore();

		cout <<"\n"<<endl;
		
		cout <<"Enter exchange rate: ";
		cin >>ecur;
		cin.ignore();

		currency(rcur, ecur, exc);

	
		cout <<"The exchange value for "<<rcur<<" is"<< exc<<endl;
	break;


	case 2:
		cout <<"This is the program to convert inches to mm: "<<endl;
		cout <<"What would you like to do ?" <<endl;
		cout <<"1: INCHES to MM"<<endl;
		cout <<"2: MM to INCHES"<<endl;
		cin >> twoselection;

		switch(twoselection)
		{

			float inches, mms;
			float ina, mma;
			
	
			case 1:
				cout <<":Welcome to INCHES to MM conversion:"<<endl;
				cout <<"Enter inches to convert: ";
				cin >> inches;
				
				mm(inches, ina);
				cout <<"The conversion of "<<inches<<" is"<<ina <<endl;
			break;

			case 2:
				cout <<"Welcome to MM to INCHES conversion"<<endl;
				cout <<"Enter MM to convert: "<<endl;
				cin >> mms;

				in(mms, mma);	
				cout <<"The conversion of "<<mms << " is "<< mma<<endl;
			break;

			
			default:

			break;
		}

	case 3:

		int numbers;
		int count;
		float adder;
		float ans, anss, total;

		total = 0;
		anss = 0;

		cout <<"This adds the value of given numbers: "<<endl;
		cout <<"Enter how many numbers to add? "<<endl;
		cin >> numbers;


		for (count = 1; count <= numbers; count++)
		{
			cout <<"Enter "<<count<<" value: ";
			cin >> adder;
		
			
			total = addm(adder, anss);
		
		}	
			cout <<"The sum is "<< total <<endl;
		
	break;



	case 7:

		char namechk[10];
		char names;
 		char rnames;

		cout <<"This program check if the name is in the database: "<<endl;
		cout <<"Enter the desired name: "<<endl;
		cin.get(namechk, 10, '\n');
		cin.ignore(10, '\n');

		namec(namechk, rnames);


		cout <<"The names "<<"""<<namechk<<"""<<" & "<<"""<<namechks<<"""<<" are "<< rnames <<endl;

	break;

	default:

	break;

	}



}


float currency(float rcur, float ecur, float & excs)
{

	excs = rcur * ecur;

	return excs;

}

float mm (float inches, float & inas)
{

	inas = 25.4 * inches;

	return inas;
}

float in (float mms, float & mmas)
{
	mmas = 0.04 * mms;

	return mms;	

}

float addm(float adder, float &anss)
{
	anss = anss + adder;

	return  anss ;

}

char namec(char namechk, char & rnames)
{

int result;
char namechks[10];

cout <<"Enter another name to check with "<<"""<<namechk<<""\n\n"<<endl;

cin.get(namechks, 10, '\n');
cin.ignore(10,'\n');

result = strcmp(namechk, namechks);

	switch (result)
	{
		case 1:
			cout <<"Its correct"<<endl;
		break;

		case 2:

			cout <<"Its not correct"<<endl;
		break;
		
	}

return rnames;

}


Last edited on
char namec(char namechk, char & names);

You are saying this function takes a char and a char reference, but then you are trying to pass in a character array as the first argument.
And what is the problem? The compiler says clear enough that it can not convert char * to char. It means that you shall pass to the function a char instead of char *.
You defined namechk as

char namechk[10];

that is as an array. And then you are trying to pass it to function namec.

namec(namechk, rnames);

You should decide either you pass to the function a single character or an array that implicitly is converted to a pointer to its first element.
functionall.cpp: In function `int main()':
functionall.cpp:135: error: invalid conversion from `char*' to `char'
functionall.cpp:135: error: initializing argument 1 of `char namec(char, char&)'
You try to pass an array as first argument to namec but you have declared it to take a char as first argument.

functionall.cpp: In function `char namec(char, char&)':
functionall.cpp:196: error: invalid conversion from `char' to `const char*'
functionall.cpp:196: error: initializing argument 1 of `int strcmp(const char*, const char*)'
strcmp expects a const char* but you try to pass it namechk which is a char.

It looks like you want namechk in namec to be of type const char*. Change the type on line 15 and line 187.
Hello,

Thanks for the response.

My intention is not to use pointers as i dont have any knowledge.. I want to stick with function.

My idea behind the program is to pass a value by reference and to a function and there on the function again ask a value and then string compare and return the result if they are similar or not..

on case 7(Line 125 - Line 141)

I am asking a string... upto 10 character..

that value is then passed to the function..

the function again asks a new value and compares and send the result..

nt sure where it went wrong..
Topic archived. No new replies allowed.