0

this code is to take an equation from the user in the form of a string and then the program configures the confessions and store them in a dynamic array;

now I need a way to store the string (S) in a dynamic array

int main()
{
    char input[1000];
    cout<<"Enter equation: "<<endl;
    cin.getline(input,1000);
    char *s;
        s=strtok(input,"x+");
        while(s!=NULL)
        {
            cout<<s<<endl;
            s=strtok(NULL, "x+");
        }
    
}
  • 1
    Sorry to have to tell you this, but this is barely C++ code at all - it is C., and that's making the job much harder. I would recommend that you obtain and study one of [these](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Then have another go and then (if you're still stuck), ask a new question. Thanks. – Paul Sanders May 12 '22 at 21:30
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 12 '22 at 21:58
  • Discuss `for(int i=0; i – user4581301 May 12 '22 at 22:15
  • @AhmedHesham I see nothing in this code that should be crashing, so please describe the crashes you are experiencing, preferably with a [mcve]. In any case, `strtok()` modifies the input string while iterating, thus your `for` loop will not behave as expected. Why do you even have a `for` loop at all? – Remy Lebeau May 12 '22 at 22:18
  • 2
    If you don't tear the string into pieces you won't have to put it back together again. Don't use `strtok`. C++ strings have search functions that can pick out pieces of a string without doing violence to it, and with only a bit more code than with `strtok`. – Pete Becker May 12 '22 at 22:20
  • I don't know what that outer loop is for, but when I ran this, no crashes. – Joseph Larson May 12 '22 at 22:25
  • Looks like you're using traditional character arrays here. Maybe using std::string (from the std. c++ library) might help. Also, there are **stringstreams** in c++ and that might be what you're looking for. If you really want to go one character at a time, you can still `for(char c : strobj)` this. simple as that. – Sreyas Adury May 13 '22 at 12:03

0 Answers0