C++ 是什么导致程序在我继续执行另一个功能之前停止?
我正在进行二进制搜索,以便在该程序排序后使用,我不确定在按字母顺序排序后如何停止崩溃 Main.cppC++ 是什么导致程序在我继续执行另一个功能之前停止?,c++,string,binary-search,C++,String,Binary Search,我正在进行二进制搜索,以便在该程序排序后使用,我不确定在按字母顺序排序后如何停止崩溃 Main.cpp #include <iostream> #include "ReadString.h" #include "SortString.h" #include "SearchString.h" using namespace std; int main() { int i; int column = 500; int row = 20; char
#include <iostream>
#include "ReadString.h"
#include "SortString.h"
#include "SearchString.h"
using namespace std;
int main()
{
int i;
int column = 500;
int row = 20;
char *inputsearch;
cout << "How many names will you enter? ";
cin >> row;
while (row > 20 || row < 0)
{
cout << "Error: Please do not enter more than 20 or less than 0: ";
cin >> row;
}
char **p = ReadString(row, column);
cout << "Printed: " << endl;
for (i = 0; i < row; i++)
{
cout << *(p + i) << endl;
}
StringSort(p, row);
cin >> inputsearch;
cout << inputsearch;
SearchString(p, inputsearch);
for (i = 0; i < row; i++)
delete[] p[i];
delete[] p;
return 0;
}
SortString.cpp
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include "SortString.h"
using namespace std;
void StringSort(char **str, int row)
{
char t[20];
int i, j, k;
for (i = 1; i < row; i++)
{
for (j = 1; j < row; j++)
{
//compare each
if (strcmp(str[j - 1], str[j])>0)
{
strcpy(t, str[j - 1]);
strcpy(str[j - 1], str[j]);
strcpy(str[j], t);
}
}
}
cout << "Strings (Names) in alphabetical order : \n";
for (i = 0; i < 5; i++)
{
cout << str[i] << "\n";
}
getch();
}
#include <string.h>
#include "SearchString.h"
int SearchString(char **p, char *inputsearch)
{
int First;
int Middle;
int Last;
First = 0;
Last = strlen(*p) - 1;
do {
Middle = (First + Last) / 2;
if (inputsearch == p[Middle])
return Middle;
else
if (inputsearch > p[Middle])
First = Middle + 1;
else
Last = Middle - 1;
} while (First <= Last);
return -1;
}
SearchString.cpp
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include "SortString.h"
using namespace std;
void StringSort(char **str, int row)
{
char t[20];
int i, j, k;
for (i = 1; i < row; i++)
{
for (j = 1; j < row; j++)
{
//compare each
if (strcmp(str[j - 1], str[j])>0)
{
strcpy(t, str[j - 1]);
strcpy(str[j - 1], str[j]);
strcpy(str[j], t);
}
}
}
cout << "Strings (Names) in alphabetical order : \n";
for (i = 0; i < 5; i++)
{
cout << str[i] << "\n";
}
getch();
}
#include <string.h>
#include "SearchString.h"
int SearchString(char **p, char *inputsearch)
{
int First;
int Middle;
int Last;
First = 0;
Last = strlen(*p) - 1;
do {
Middle = (First + Last) / 2;
if (inputsearch == p[Middle])
return Middle;
else
if (inputsearch > p[Middle])
First = Middle + 1;
else
Last = Middle - 1;
} while (First <= Last);
return -1;
}
当你这样做的时候
cin>>字符串[i];
在ReadString.cpp中,您不能确保输入长度加上null终止符少于大小为“column”的数组所允许的字符数。如果键入的字符数超过列的宽度,则会损坏freestore堆。当您使用调试器逐行检查代码时,每次检查一行所有变量的值,直到调试器检测到崩溃为止,你做了什么观察?你的缓冲区大小不一致。有时候是500。有时候是20。我怀疑某处缓冲区溢出。但是,既然你使用C++,你就应该使用<代码> STD::String 和<代码> STD::vector < /C>。
#include <string.h>
#include "SearchString.h"
int SearchString(char **p, char *inputsearch)
{
int First;
int Middle;
int Last;
First = 0;
Last = strlen(*p) - 1;
do {
Middle = (First + Last) / 2;
if (inputsearch == p[Middle])
return Middle;
else
if (inputsearch > p[Middle])
First = Middle + 1;
else
Last = Middle - 1;
} while (First <= Last);
return -1;
}
#ifndef Search_String_H
#define Search_String_H
int SearchString(char **, char *);
#endif