C
C
Chipu2018-09-06 09:05:18
C++ / C#
Chipu, 2018-09-06 09:05:18

C++ error while executing a program, how to fix it?

// ConsoleApplication3.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
//

#include "pch.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string s;
  getline(std::cin, s);
  int probel = 0;
  //Перед или после запятой пробел не ставится.
  for (int i = 0; i < s.size(); i++) 
  {
    if (s[i]==' '|| s[i]==',')
    {
      probel++;
    }
  }
  int massiv = 0;
  string strochka = "";
  string *a = new string[probel];
  for (int i = 0; i < s.size(); i++) 
  {
    if (s[i] != ' ' && s[i] != ','&&s[i]!='.'&&s[i]!='?'&&s[i]!='!')
    {
      strochka += s[i];

    }
    else
    {
      a[massiv] = strochka;
      strochka = "";
      massiv++;
    }
    
    
  }
  for (int i=0;i<probel;i++)
  {
    cout << a[i] << endl;
  }

  cout << probel<<"\t"<<sizeof(s);
  system("Pause");
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-09-06
@res2001

Due to the fact that the conditions are different in the first and second loop, you have a smaller array of strings (a[]) than it is accessed in the second loop. From here - an output for limits of an array and failure of the program.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question