0

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];

}
Trenton McKinney
  • 29,033
  • 18
  • 54
  • 66
N0way1998
  • 11
  • 4
  • Possible duplicate of [How to convert string to char array in C++?](https://stackoverflow.com/questions/13294067/how-to-convert-string-to-char-array-in-c) – sYsTeM Oct 12 '19 at 16:15
  • 1
    productCode[0] = str.substr(0, 3) + str.substr(str.length() - 3); – jignatius Oct 12 '19 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 Oct 12 '19 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 individually – N0way1998 Oct 12 '19 at 16:22
  • whats does substr mean? productCode[0] = str.substr(0, 3) + str.substr(str.length() - 3); @jignatius – N0way1998 Oct 12 '19 at 16:24
  • sub-string. https://en.cppreference.com/w/cpp/string/basic_string/substr – TheUndeadFish Oct 12 '19 at 16:38

2 Answers2

1

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];
}
N0way1998
  • 11
  • 4
  • 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 Oct 12 '19 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 vector – N0way1998 Oct 12 '19 at 16:57
  • Then std::vector is what you want. Glad I was able to help. – jignatius Oct 12 '19 at 17:13
1

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.