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
63.1k41 gold badges169 silver badges210 bronze badges
asked Oct 12, 2019 at 16:00
6
  • Possible duplicate of How to convert string to char array in C++? Commented Oct 12, 2019 at 16:15
  • 1
    productCode[0] = str.substr(0, 3) + str.substr(str.length() - 3); Commented 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. Commented 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 individually Commented Oct 12, 2019 at 16:22
  • whats does substr mean? productCode[0] = str.substr(0, 3) + str.substr(str.length() - 3); @jignatius Commented Oct 12, 2019 at 16:24

2 Answers 2

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.

answered Oct 12, 2019 at 17:17
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];
}
answered Oct 12, 2019 at 16:33
3
  • 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. Commented 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 vector Commented Oct 12, 2019 at 16:57
  • Then std::vector<std::string> is what you want. Glad I was able to help. Commented Oct 12, 2019 at 17:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.