Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have posted this before but i have redone it now and made it in a way so i can post the entire code. This is the code:
C++
#include <string>
#include <iostream>

#define csr std::string
#define cout std::cout //i know this is bad practice but i will fix this later.

cstr* split(cstr str, char separator, int arraySize) { //this code was taken from a tutorial i found online
  cstr* strings = new cstr[arraySize];
  int currIndex = 0, i = 0;
  int startIndex = 0, endIndex = 0;
  while (i <= str.length()) {
    if (str[i] == separator || i == str.length()) {
      endIndex = i;
      cstr subStr = "";
      subStr.append(str, startIndex, endIndex - startIndex);
      strings[currIndex] = subStr;
      currIndex += 1;
      startIndex = endIndex + 1;
    }
    i++;
  }
}

int main(void) {
  cstr hey = "hello hello shovelface";
  cstr* arr = new cstr[10];
  arr = split(hey, ' ', 10);
  cstr out = arr[0];
  cout << arr[0] << endl;
  delete arr;
  system("pause");
  return 0;
}


And that didn't work so i commented out all the lines in main to see where it failed:
1. All commented out except line 1 'cstr hey = "hello hello shovelface";' = WORKS
2. Uncommented line 2 (where new array is created) = WORKS
3. Uncommented line 3 (where split function is called) = WORKS
4. Uncommented line 4 (where array element is assigned to variable) = FAILS

I want it to print the first element before the first space, in other words create an array of every word in the string and print out the first word ("hello");

Maybe i have to reference it with the address or something. I don't remember quite how it works i believe it's something with the asterisk or '&' sign. Thank you for the help.

What I have tried:

I have tried commenting out line by line to find the issue. I don't understand the issue.
Posted
Updated 6-Dec-21 22:46pm
v4
Comments
CPallini 7-Dec-21 3:19am    
This is not the complete code. Where is cstr definition?
Stefan_Lang 7-Dec-21 4:48am    
In the other threads he's opened...

(it's simply a #define alias for std::string - and yes I told him he shouldn't use #define)
CPallini 7-Dec-21 4:57am    
OK,
Thank you very much, Stefan.
Stefan_Lang 7-Dec-21 5:16am    
In addition to what I have written in my solution, here is a very insightful article on the question of how to split a string in C++:
https://www.fluentcpp.com/2017/04/21/how-to-split-a-string-in-c/

While some of the things discussed there are probably beyond your current understanding, Jonathans blog is a very good source of modern C++ programming. It's always worth a visit if you want to learn more about C++ and modern programming techniques.

You need to use the debugger and step through the code to see exactly what happens at each step. Trying to guess what happens versus what you think might happen is a waste of your time.
 
Share this answer
 
Comments
OriginalGriff 7-Dec-21 3:38am    
It shouldn't even compile! :laugh:
Richard MacCutchan 7-Dec-21 5:17am    
:)
Regarding your code, one of the worst issues is that you don't understand the concepts of arrays and pointers very well: you repeatedly allocate arrays of pointers, when actually you intended (probably) to allocate arrays of char instead!

Of course, there are multiple problems with this, and the least of it is that your code does very silly things:

1. you're using arrays and allocations and pointers without understanding these concepts very well
2. You're using a tutorial that apparently suggests doing these things, although you shouldn't do any of that, because C++ offers much cleaner, better and easier ways!
3. You keep shooting questions about code that doesn't make sense at all, not even for a tutorial! It's so painful that the only meaningful advice to give is don't do that!


For these reasons, this is my actual advice:

This code is very bad and outdated. You can learn nothing from that than bad programming. If that is from a tutorial, stop right there and find an up-to-date C++ tutorial that does not use old C-style headers and char arrays! That way lies only pain! C++ is already a difficult language to learn, there is no reason to make it even harder!

For a start, you can go here: C++ Language - C++ Tutorials[^]

If you find other sources, watch out for these signs of bad and outdated material:
1. including system headers that end in .h : these are old C headers that still exist for backward compatibility, but these shouldn't be used any more! There is a lot of code on the web that still uses them, mainly because they were copied and pasted dozens of times, from original code that dates back to the last millenium! Both the style being used back then and the functions contained within do no longer conform with modern standards!

2. Do not use #define! The problem with #define is that it's incredibly easy to break not only your functions but many other functions too, in many unexpected ways. It takes a very high level of understanding to use #define responsibly, and with minimal risk. It takes an even higher level of understanding to fix bugs caused by bad uses of #define!

3. Do not use pointers, and be wary of tutorials that use them: raw pointers are a dangerous trap. They are the most common root for program exceptions and crashes. C++ offers smart pointers such as std::shared_ptr and std::unique_ptr which are safe to use unless you start being creative in an unhealthy manner. See <memory> - C++ Reference[^]

4. Do not use standard C-style arrays. While you're learning, std::vector should fulfil all of your needs in most cases. Here is a list of C++ containers for different purposes: Containers - C++ Reference[^]
 
Share this answer
 
Comments
k5054 7-Dec-21 7:31am    
5ed. I wish I could give you more ;).
Stefan_Lang 7-Dec-21 8:25am    
Thank you
Your split function doesn't return anything: so arr is never given a value (and it shouldn't compile at all!)

You need to add
C++
return strings;
to the end of the function.
 
Share this answer
 
Comments
Member 14769677 7-Dec-21 4:30am    
Oh my god, how could i forget something so simple xD Thank you
OriginalGriff 7-Dec-21 5:43am    
:laugh: We all do it! I often read what I meant to write instead of what I did ... :blush:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900