Why i can't send char with array to the class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why i can't send char with array to the class?

ex: ma obj1(text[0]); ma is a class, text is string. there is a problem in conversion from char* to const char*

28th Nov 2017, 8:45 AM
joelio alex
joelio alex - avatar
21 Answers
+ 2
@Kinshuk is right. So in your constructor definition you would have something like this: public: ma (const string& line) { parseLine (line); } parseLine would be a function in the class that handles separating the string into individual chars.
28th Nov 2017, 1:09 PM
Zeke Williams
Zeke Williams - avatar
+ 4
You may directly pass the string "237+6" into your constructor, and then iterate through your string to seperate the numbers and the operators inside their respective types. It will be tedious to use different objects for the same.
28th Nov 2017, 12:32 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
'\0' is used to terminate a string. I didn't understand what you mean by 1 row input. Isn't: 234+7 a 1 row input?
28th Nov 2017, 12:24 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Which char? "237+6" is a string... Edit - It is better to perform the elimination in the constructor itself. Creating numerous objects is a waste of memory.
28th Nov 2017, 12:28 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
If the class constructor accepts a string (char*), there is no way you can use a char to initialize it. text[0] is just a char, and is the first element/character in the string. You need to pass text itself, or any other char* array.
28th Nov 2017, 12:12 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
Pass the array text like this: ma obj1(text);
28th Nov 2017, 12:16 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
Then either you change your constructor's argument to a char, or pass your array element like this: char cht[2]={text[0],'\0'}; ma obj1(cht);
28th Nov 2017, 12:20 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
Then why do you need to use char? You need a string!
28th Nov 2017, 12:25 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
so what should i do?
28th Nov 2017, 12:15 PM
joelio alex
joelio alex - avatar
+ 1
i only need one by one char of that text
28th Nov 2017, 12:17 PM
joelio alex
joelio alex - avatar
+ 1
what is \0 used for?
28th Nov 2017, 12:22 PM
joelio alex
joelio alex - avatar
+ 1
yes
28th Nov 2017, 12:25 PM
joelio alex
joelio alex - avatar
+ 1
in my mind, i ought to eliminate the input one by one
28th Nov 2017, 12:27 PM
joelio alex
joelio alex - avatar
+ 1
so i need send the char to the class
28th Nov 2017, 12:28 PM
joelio alex
joelio alex - avatar
+ 1
2,3,7,6 is a char
28th Nov 2017, 12:29 PM
joelio alex
joelio alex - avatar
+ 1
and convert it with atoi()
28th Nov 2017, 12:29 PM
joelio alex
joelio alex - avatar
+ 1
oke, i got something
28th Nov 2017, 12:31 PM
joelio alex
joelio alex - avatar
+ 1
thanks for ur suggestions
28th Nov 2017, 12:32 PM
joelio alex
joelio alex - avatar
0
@Kinshuk Vasisht: actually i want to make "1 row input" calculator with classes
28th Nov 2017, 12:21 PM
joelio alex
joelio alex - avatar
0
that's good idea, but i want do my own attempt
28th Nov 2017, 12:34 PM
joelio alex
joelio alex - avatar