+ 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😊
6 ответов
+ 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!
+ 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
+ 3
thanks it worked with cin.😃
+ 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??
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.
- 1
Rahul Ahah yes. I know C ++ a little bit and wrote by analogy Java). In Java, for example, you could use arr.length☺️