-1

As soon as I add the below code this programs ends showing this error message:

Process returned -1073741819 (0xC0000005)

If I run those code separately then both of them work.

I used sstream and array too but combined they do not work properly.

#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

  int main()
  {

      string input = "touch world.txt this.txt is.txt sentence.txt";

       string word;
        int length = 0;
        for(int a = 0;a<input.length();a++){
            if(input[a] == ' '){
                length++;
            }
        }
        string filesNameArr[length];
        int number = 0;

        //                    hello world this is sentence
        for(auto x:input)
        {
            if(x==' ')
            {
                filesNameArr[number] = word;

                 word.erase();
                 number++;
            }

            else
                  word=word+x;
        }
        filesNameArr[number] = word;

    number = 0;
    //when i add the below code it generates error and stops
              ofstream outFile[41];

    stringstream sstm;
    for (int i=0;i<41 ;i++)
    {
        sstm.str("");
        sstm << "subnode" << i;
        outFile[i].open(sstm.str().c_str());
    }

return 0;
  }



Phantasm
  • 17
  • 7
  • 2
    I didn't downvote, but how does downvoting and being able to answer relate? If one thinks that the question is poorly written it is a valid reason to downvote. – t.niese Aug 23 '20 at 09:34
  • @t.niese Many peoples downvote without seeing the question. Not all peoples are same but some feel/think that they should down vote begineers. I see most of the begineers are downvoted. – Phantasm Aug 23 '20 at 10:25
  • 3
    There are for sure some people that down-vote without looking at the question, but those most certainly also don't read such a comment or don't care about that. Proper formatting and a clear description of the question helps more than adding such a comment. A good looking question shows basic effort of the one asking the question. That is for example a reason why I edited your question. Code with bad indentation and formatting increases the probability of down-votes, as those are annoying to read and show a lack of effort in writing the question. – t.niese Aug 23 '20 at 10:31
  • 1
    And if you edit your question, reverting all the edits to improve the formatting, instead of just adding more details retaining the previous formatting improvements, it also something that increases the probability of down-votes. – t.niese Aug 23 '20 at 10:42
  • 1
    Most people are like me. But you always need to keep in mind, everyone answering here is doing that in their spare time, without being paid. And SO tries to be Q&A platform with a certain quality standard, for both questions and answers. There are way more people asking questions then being able to give good answers. It is therefore even more important that the questions are in a reasonable form so that as many as possible can be answered. So you need to make a decision if you write a comment like that or if you answer some questions instead of it. – t.niese Aug 23 '20 at 15:09

1 Answers1

2

length is one less than the number of words in your string as you are only counting the number of spaces. This means your final filesNameArr[number] = word causes undefined behaviour and will probably corrupt the stack.

string filesNameArr[length]; uses a variable length array which is not valid c++. If you use a std::vector instead you can skip the initial counting of the words completely:

std::vector<std::string> filesNameArr;
for(auto x:input)
{
    if(x==' ')
    {
      filesNameArr.push_back(word);
      word.erase();
    }
    else
    {                  
      word+=x;
    }
}
filesNameArr.push_back(word);

You can use std::stringstreams built in ability to read words from strings to make this even simpler:

std::stringstream sinput(input);
std::vector<std::string> filesNameArr;
std::string word;
while (sinput >> word)
{
  filesNameArr.push_back(word);
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • How can I change the console text color CODE I USED void Color(int color) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); } and system("color 90); to color but I want just to color specific text with one color. How can i do that Example: root this where root should have color blue and this should have blue color on the same line. IS IT POSSIBLE?? – Phantasm Aug 24 '20 at 10:42
  • 1
    @Phantasm If you have a new question open a new question, the comments aren't the right place – Alan Birtles Aug 24 '20 at 11:19
  • IT SAYS :: You have reached your question limit so i cant ask question @alan Please Help me – Phantasm Aug 25 '20 at 09:13
  • I think if you delete https://stackoverflow.com/questions/63435539/can-we-make-windows-application-of-shapes-different-than-rectangle you'll get some reputation back – Alan Birtles Aug 25 '20 at 09:25
  • https://meta.stackexchange.com/questions/273741/stack-overflow-you-have-reached-your-question-limit – Alan Birtles Aug 25 '20 at 10:46