阵列名称存储程序在每次输入后崩溃 新手C++用户试图制作一个使用数组来存储名字的程序。输出是一个菜单,您可以输入一个命令,它执行上述功能。问题是,当我试图从菜单中输入命令时,程序崩溃。编译时我没有收到任何错误或警告,因此我不知道它为什么会崩溃

阵列名称存储程序在每次输入后崩溃 新手C++用户试图制作一个使用数组来存储名字的程序。输出是一个菜单,您可以输入一个命令,它执行上述功能。问题是,当我试图从菜单中输入命令时,程序崩溃。编译时我没有收到任何错误或警告,因此我不知道它为什么会崩溃,c++,C++,非常感谢您的任何反馈/帮助 #include <iostream> #include <string> using namespace std; //Prototypes--------------------------------------------------------------------------------------------------------------------------------------------------------

非常感谢您的任何反馈/帮助

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

//Prototypes----------------------------------------------------------------------------------------------------------------------------------------------------------------
void addName (string nameList[], const int listMax); //Adds a name
void removeName (string nameList[], const int listMax); //Removes a name
void findName (string nameList[], const int listMax); // Searches for a name in the saved list
void showList (string nameList[], const int listMax); //Shows a list of names currentllistMax stored
void sortList (string nameList[], const int listMax); //Sorts saved names in alphabetical order


//Global Declartions--------------------------------------------------------------------------------------------------------------------------------------------------------
const int listMax = 50;

// MAIN FUNCTION -------------------------------------------------------------------------------------------------------------------------------------------------------------
int main(){
    char input; // Variable for when the user inputs their menu selection
    string nameList[listMax]; //string called names with the size being namesMax which is 50


    cout << "Enter a Command " << endl; //Prompt to choose command from menu
    cout << "<A> - Add a name to the database(Max 10)." << endl; //getNames
    cout << "<R> - Remove a name from the database" << endl; //removeName
    cout << "<F> - Search for a name in the database" << endl; //findName
    cout << "<L> - Shows current state of list" << endl; //displalistMaxNames
    cout << "<S> - Sorts the database." << endl; //sortNames
    cout << "<Q> - Ends the program." << endl; //Exits the program entirellistMax
    cin >> input; //Saves the menu choice input 

    if (input == 'A' ||input == 'a') 
        {
            addName(nameList, listMax); 
        }
    else if (input == 'R' || input == 'r')
        {
            removeName(nameList, listMax);
        }
    else if (input == 'F' || input == 'f')
        {
            findName(nameList, listMax);
        }
    else if (input == 'L' || input == 'l')
        {
            showList(nameList, listMax);
        }
    else if (input == 'S' || input == 's')
        {
            sortList(nameList, listMax);
        }
    else if (input == 'Q' || input == 'q')
        {
            return 0; 
        }
}

// ADD NAMES FUNCTION --------------------------------------------------------------------------------------------------------------------------------------------------------
void addName(string nameList[], const int listMax){
    int x;
    for (x = 0; x < listMax; x++) //x = 0 > if x is less than listMax, increment blistMax 1
    {
        cout << "Enter a name to be added to the database: ";
        cin >> nameList[listMax];
    }
    return;
}

// REMOVE NAMES FUNCTION -----------------------------------------------------------------------------------------------------------------------------------------------------
void removeName(string nameList[], const int listMax){
    string clearName; //EmptlistMax String
    string removeName;
    cout << "Enter the name to be removed from the database: ";
    cin >> removeName; //The name entered saved to removeName for further computation

    for (int x = 0; x < listMax; x++) // x = 0, if x is less than listMax; increment blistMax 1
    {
        if(nameList[x] == removeName) // Goes through everlistMax arralistMax level searching to see if it compares to the name saved to removeName
        {
            nameList[x] = clearName; // If name is found, assign value of clearnName, which is an emptlistMax string, thus removing it from the arralistMax.
        }
        else if(x == listMax - 1) // If it goes through the entire arralistMax and doesnt find a match it outputs that the name could not be found.
        {
            cout << "Name not found"; 
        }
    }
    return;
}

// SEARCH FUNCTION -----------------------------------------------------------------------------------------------------------------------------------------------------------
void findName(string nameList[], const int listMax){
    int x = 0;
    int z;
    bool f = false;
    string searchName;

    cout << "Enter a name to search for";
    cin >> searchName;

    while (!f && x <= listMax) //While f and x are equal to / less than listMax
    {
        int listMax = (x + z) / 2; 
        if (nameList[listMax] == searchName) // if our arralistMax is on level listMax then its on the list
        {
            f = true;
            cout << nameList[listMax] << "is on the list!" <<endl;
        }
        else if (nameList[listMax] > searchName)
            z = listMax - 1;
        else
            x = listMax + 1;
    }
    return;
}

// DISPLAY FUNCTION ----------------------------------------------------------------------------------------------------------------------------------------------------------
void showList(string nameList[], const int listMax){
    cout << nameList[listMax];
    return;
}

// SORT NAMES FUNCTION -------------------------------------------------------------------------------------------------------------------------------------------------------
void sortList(string nameList[], const int listMax){
    bool sort;
    do
    {
        sort = 0;
        for (int x = 0; x < listMax - 1; x++)
        {
            if (nameList[x] > nameList[x + 1])
            {
                nameList[x].swap(nameList[x+1]);
                sort = 1;
            }
        }
    }
    while (sort == 1);
    return;
}
#包括
#包括
使用名称空间std;
//原型----------------------------------------------------------------------------------------------------------------------------------------------------------------
void addName(字符串名称列表[],常量int listMax)//添加一个名称
void removeName(字符串名称列表[],常量int listMax)//删除名称
void findName(字符串名称列表[],常量int listMax);//在保存的列表中搜索名称
void showList(字符串名称列表[],常量int listMax)//显示当前存储的名称列表
void排序列表(字符串名称列表[],常量int listMax)//按字母顺序对保存的名称进行排序
//全球申报--------------------------------------------------------------------------------------------------------------------------------------------------------
常数int listMax=50;
//主要功能-------------------------------------------------------------------------------------------------------------------------------------------------------------
int main(){
char input;//用户输入菜单选择时的变量
string nameList[listMax];//调用名称的字符串,其大小为namesMax,即50

coutVisualStudio告诉我,代码因访问未分配的内存而崩溃

您多次使用
list[listMax]
,这不应该也不合适。
长度为
len
的数组
a
中的最后一项是
a[len-1]
,而不是
a[len]
(因为数组索引从C中的0开始)


顺便说一下,它还告诉我在
findName()中
在未初始化时使用
z
,这是未定义的行为。

使用调试器。您应该共享崩溃详细信息。我在编译时没有收到任何错误或警告,因此我不知道它崩溃的原因。--编译时没有错误的程序只意味着语法正确。这并不意味着您在pr中的逻辑正确gram是正确的。
cin>>nameList[listMax];
-看到那行有什么错误吗?那行有完美的语法,但当那行被执行时会发生可怕的事情。这就是为什么相信没有编译器错误就意味着一个工作程序都是错误的。找一本好书。我把
nameList[listMax]
改为
nameList[listMax-1]
addName
函数中,现在它在使用后不会崩溃。但是在
findName
中它仍然崩溃,我是否也要将
nameList[listMax]
更改为
nameList[listMax-1]