Есть программа, работает отлично, но когда она завершает работу, то выдаётся такой вот debug-error: "Runtime check failure: Stack around the variable 'buffer' was corrupted." Как исправить эту проблему? Или хотя бы скрыть эрор ? Код:
| Код | #include <tchar.h> #include <stdlib.h> #include <conio.h> #include <iostream> #include <iomanip> #include <string.h> int _tmain(int argc, _TCHAR* argv[]) { const int n = 3; const int m = 30; char buffer[n][m]; int Words[n]; //Сколько слов в строке? do { std::cout << "Input strings." << std::endl; int i = 0; while (i < n) { const int BufferLength = 1000; char tmp[BufferLength]; std::cout << "#" << i << ":"; std::cin >> std::noskipws; std::cin.getline(tmp, BufferLength, '\n'); std::cin.clear(); tmp[m] = '\0'; bool IsSpace = false; bool HasSpaceInside = false; //Теперь проверяем не противоречит ли данная строка условиям задачи. for (int j = 0; j < BufferLength && tmp[j] != '\0'; j++) { if (tmp[j] == ' ') IsSpace = true; if (IsSpace && tmp[j] != ' ' && tmp[j] != '\0') { HasSpaceInside = true; break; } } if (HasSpaceInside) { i++; strcpy_s(buffer[i], tmp); } else std::cout << "Bad string. Try again." << std::endl; }
int MaxWordCount = -1; for (int i = 0; i < n; i++) //Считаем сколько слов в каждой строке { //Заодно вычисляем максимальное количество слов int WordCount = 0; bool IsLetter = false;//Это буква? for (int j = 0; j < m; j++) { char c = buffer[i][j]; if ((c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A))//Это буква! IsLetter = true; else //Это не буква. { if (IsLetter) WordCount++;//Предыдущий символ был буквой, значит, мы только что прочитали слово. IsLetter = false; if (c == '\0') break; //Конец строки, закончим её обрабатывать! } } Words[i] = WordCount; if (WordCount > MaxWordCount) MaxWordCount = WordCount; } std::cout << "Strings with the biggest quantity of words are:" << std::endl; for (int i = 0; i < n; i++) if (Words[i] == MaxWordCount) std::cout << "#" << i << ":" << buffer[i] << std::endl; std::cout << "More? ('y' - yes)" << std::endl; } while (_getch() == 'y'); return 0; }
|
|