+ 1

How to read a string and assing it to an array

hi guys, can you help me with this please, I have a char array and I need to pass it to a function like this char str[100] = "11A"; toDeci(str, base); And it works, but I want the user to be able to input a value and add it to the array like: char str[100]; gets(str); // this way doesn't work and it is not recommended toDeci(str); Thanks😊

8th Apr 2018, 2:54 AM
Victor_*
Victor_* - avatar
6 Antworten
+ 4
Victor_*, You can also go with the string data-type ;) #include <iostream> int main() { std::string str; std::cin >> str; // input the string toDeci(str, base); return 0; } PS: Aidos Zhakupov, your code is correct if the length of string is known!
8th Apr 2018, 3:26 AM
777
777 - avatar
+ 4
std::string Use the string class and then you can use strcat(s1, s2) or s1 + s2 to concatenate the string together. https://www.tutorialspoint.com/cplusplus/cpp_strings.htm
8th Apr 2018, 3:32 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
thanks it worked with cin.😃
8th Apr 2018, 4:08 AM
Victor_*
Victor_* - avatar
+ 1
I see that you use string str; I need my str to be an array (str[]) because my function waits for an array, what's the difference between char str[] and string str??
8th Apr 2018, 4:29 AM
Victor_*
Victor_* - avatar
0
Victor_* you pass a string without using an array. After the user enters a value, it passes the value to the method. This method is sometimes useful and simple.
8th Apr 2018, 4:32 AM
Aidos Zhakupov
Aidos Zhakupov - avatar
- 1
Rahul Ahah yes. I know C ++ a little bit and wrote by analogy Java). In Java, for example, you could use arr.length☺️
8th Apr 2018, 4:18 AM
Aidos Zhakupov
Aidos Zhakupov - avatar