阵列名称存储程序在每次输入后崩溃

时间:2017-10-26 20:14:45

标签: c++

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

非常感谢任何反馈/帮助

#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;
}

1 个答案:

答案 0 :(得分:1)

VisualStudio告诉我代码崩溃以访问未分配的内存。

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

顺便说一下,它还告诉我,在findName()中,z使用public class Register extends AppCompatActivity { protected SnapToSellDataSource mDataSource; public String sFullname; public String sEmail; public String sMobileNumber; public String sPassword; EditText full_name, email, mobile_number, pwd, copwd; Button registerButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); mDataSource = new SnapToSellDataSource(Register.this); full_name = (EditText) findViewById(R.id.editText); email = (EditText) findViewById(R.id.editText2); mobile_number = (EditText) findViewById(R.id.editText3); pwd = (EditText) findViewById(R.id.editText4); copwd = (EditText) findViewById(R.id.editText5); registerButton = (Button) findViewById(R.id.button); registerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Register register = new Register(); String editPassword = pwd.getText().toString(); String editConfirmPassword = copwd.getText().toString(); if(editPassword.equals(editConfirmPassword)) { //This isn't overwriting the null class variables I //instantiated so that I can pass them to the class below sFullname = full_name.getText().toString(); sEmail = email.getText().toString(); sMobileNumber = mobile_number.getText().toString(); sPassword = pwd.getText().toString(); mDataSource.insertUser(register); } } }); } } 而未初始化时,这是未定义的行为。