my program should have store = 5 string values - Simpsun GN120, Sonic Lux10, Ultimax G42, Antalpha A200 and Nickov N230 and then make a calculation that will make code for each value. Code would take first 3 letters and 3 last letters of a value.
first code from value: Simpsun GN120
would look like this: Sim120
my biggest issue was that i couldn't make a string array as getting a length of each value in array would crash a program so for now i have made program that will do this calculation but only if string is not array if someone could give me some tips how i could improve my code to make that string in to array
#include <iostream>
using namespace std;
int main()
{
string str = "Simpsun GN120";
int i;
string productCode[5];
for (i = 0; i < str.length(); i++)
{
if (i == 0 || i == 1 || i == 2)
{
productCode[0] += str[i];
}
if (i == str.length() - 1 || i == str.length() - 2 || i == str.length() - 3)
{
productCode[0] += str[i];
}
}
cout << productCode[0];
}
-
Possible duplicate of How to convert string to char array in C++?sYsTeM– sYsTeM2019年10月12日 16:15:27 +00:00Commented Oct 12, 2019 at 16:15
-
1productCode[0] = str.substr(0, 3) + str.substr(str.length() - 3);jignatius– jignatius2019年10月12日 16:19:10 +00:00Commented Oct 12, 2019 at 16:19
-
well i can convert string to char array and so on but the problem is i would like to make that line string str = "Simpsun GN120"; in to array which would look like this string str[5] = {"Simpsun GN120", "Sonic Lux10", "Ultimax G42", "Antalpha A200", "Nickov N230" }; well ofc later on i will change my string array that won't have assign constant size and it will expand as i will have to input each name and store it in array like "Simpsun GN120" and so on.N0way1998– N0way19982019年10月12日 16:22:20 +00:00Commented Oct 12, 2019 at 16:22
-
my current code is working but as i said above only with normal string because if i would like to make string array for str .length wouldn't work to check size of each name in array as i will have to make code for each name individuallyN0way1998– N0way19982019年10月12日 16:22:26 +00:00Commented Oct 12, 2019 at 16:22
-
whats does substr mean? productCode[0] = str.substr(0, 3) + str.substr(str.length() - 3); @jignatiusN0way1998– N0way19982019年10月12日 16:24:48 +00:00Commented Oct 12, 2019 at 16:24
2 Answers 2
It's simple using string class .Run a loop to execute
productCode[i] = str[i].substr(0, 3) + str[i].substr(str[i].length() - 3);
and your work is done.
jignatius Thank you very much for that answer!
using namespace std;
int main()
{
string str[2] = { "Simpsun GN120", "Sonic Lux10" };
int i;
string productCode[5];
for (int i = 0; i < 2; i++)
{
productCode[i] = str[i].substr(0, 3) + str[i].substr(str[i].length() - 3);
}
cout << productCode[0] << endl;
cout << productCode[1];
}
-
Instead of a C-style string array you could use a std::vector (can add or delete this) or std::array (fixed size). That's more C++ style.jignatius– jignatius2019年10月12日 16:54:39 +00:00Commented Oct 12, 2019 at 16:54
-
i think array (fixed size) won't work as later on i will make that user will be able to input as much photography kit as he would like to and add them to str array so size of array will change i have never used vector before and im still trying to learn something about but thank you i will keep it in mind and will try to find out more about vectorN0way1998– N0way19982019年10月12日 16:57:38 +00:00Commented Oct 12, 2019 at 16:57
-
Then std::vector<std::string> is what you want. Glad I was able to help.jignatius– jignatius2019年10月12日 17:13:56 +00:00Commented Oct 12, 2019 at 17:13