0
Multi-Array and String C++
Hello sololearners, how can i make any string to accept on a multi-array? Make the string as a value of an array. Code: string Symbols = "@"; int Digits[1][1] = {Symbols}; cout << Digits[0][0];
8 odpowiedzi
+ 2
Well, this one creates a char array to copy a std::string content
#include <algorithm> // std::copy
#include <iostream>
#include <cstring> // std::strcpy
int main()
{
std::string str { "Hello world!" };
char* data { new char[ str.size() + 1 ] };
std::copy( str.begin(), str.end(), data ); // C++ way
//std::strcpy( data, str.c_str() ); // C way
std::cout << data <<"\n";
delete [] data; // don't forget to clean up
return 0;
}
* Credits:
https://stackoverflow.com/questions/12361669/converting-an-stdstring-into-a-char-array
https://www.geeksforgeeks.org/convert-string-char-array-cpp/
+ 2
But <Digits> is array of integer. I think this way you might want to use array of string/char instead.
+ 2
Let me get this straight first,
<Symbol> is a string, you want to create an array to contain each character from <Symbol>.
Or is it something different?
+ 2
No worries, you'll get the hang of it 👍
Here are references for std::copy and std::strlen
https://en.cppreference.com/w/cpp/algorithm/copy
https://en.cppreference.com/w/cpp/string/byte/strlen
+ 1
Deng, this is way too advance XD. Just for appreciation, imma mark it as the answer. Will try to implement it, thank you!
0
What value did you expect in <Digits>? <Symbols> itself doesn't have any digit.
I need more details on what you actually want here. So maybe you can put more examples in the Description.
0
When user input any kind of symbol/character which is a string, it will be the value of the multi-array.
Ex.
Please enter a symbol :$
Digits[0][0] = "quot;
So, when i call the multi-array Digits the value would be the string inputted.
0
Yes, what would be the syntax for that?
This is basically what I want to do but its error:
String Digits[0][0] = Symbol;