矢量名称存储列表程序突然不显示列表

时间:2017-11-12 21:33:12

标签: c++

新手C ++用户试图练习程序构建。这个程序的重点只是带有向量的简单名称存储。

我之前的程序https://pastebin.com/MG1hHzgK非常适合添加名字。

此升级版本应该输入First Last个名称,然后在添加到列表之前将其转换为Last, First名称。

我的问题是,输入名称后,它们不会添加到列表中。我以前的程序和当前程序之间的差异都在函数addNames中,对我来说,它看起来是正确的,但显然不是。

非常感谢任何提示或帮助。

#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

// Prototypes
string addNames(vector <string>& nameList);
string removeName(vector <string>& nameList); 
int findName (vector <string>& nameList);
void showList(vector <string>& nameList);
void commandList(vector <string>& nameList);
void inputCall(vector <string>& nameList);
void sortList(vector <string>& nameList);


int main() 
{
vector <string> nameList;
commandList(nameList);
}

void commandList(vector <string>& nameList) 
{   
    cout << "\nPress any key to continue..." << endl;
    getch();
    system("cls");

    cout << "Enter a Command " << endl;
    cout << "<A> - Add names to the list" << endl;
    cout << "<R> - Remove a name from the list" << endl;
    cout << "<F> - Search for a name on the list" << endl;
    cout << "<L> - Show current state of the list" << endl;
    cout << "<S> - Sort the list" << endl;
    cout << "<Q> - Ends the program" << endl; 
        inputCall(nameList);
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------  
string addNames(vector <string>& nameList)
{
    string input;
    int pos = input.find(' ');

        nameList.clear();
        for (;true;)
        {
            cout << endl;
            cout << "Enter a Name or 'Stop' to end name entry: " << endl;
            getline(cin, input);

            if (input == "Stop" || input == "stop"){
                commandList(nameList);


        }   else if(pos != -1) {
                string first = input.substr(0, pos);
                string last = input.substr(pos + 1);
                input = last + "," + first;     
                    nameList.push_back(input);
                    commandList(nameList);
        }        
}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------   
string removeName(vector <string>& nameList)
{    
    string x;

    cout << endl;
    cout << "Enter the name to remove: " << endl;
    cin >> x;  

    for (int i=0; i < nameList.size(); ++i)  {      
        if (nameList[i]== x) nameList[i]="";
        }
            commandList(nameList); 
}

// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------  
int findName (vector <string>& nameList)
{
    string target;
    int i, x=0;
    int p=0;

    cout << endl;
    cout << "Enter a name to search for: " << endl; 
    cin >> target;

    if (target == "Quit" || target == "quit") {exit(0); 
    }
        for (int i=0; i < nameList.size(); i++)
        {
        if (nameList[i] == target) 
        {   
        cout << endl;
        cout << "The entered name is listed as #" << p+1 << '.' << endl;
        commandList(nameList);
        return p;
    }
    if (nameList[i] == "") {
        p--;
    }
        p++;
    }
    cout << endl;
    cout << "Name not found!" << endl;
    commandList(nameList);
    return -1;
} 

// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------     
void showList(vector <string>& nameList)
{
    cout << endl;
    cout << "The current state of the list is: " <<endl;

    for (int i=0; i<nameList.size(); i++)
    if(nameList[i] !="")
        cout << nameList[i] << endl;

    commandList(nameList);
}  

void sortList(vector <string>& nameList) 
{
string temp;
    for (int i=0; i < nameList.size()-1; i++)
    {      
        for (int j=0; j < (nameList.size()-i-1); j++)
        {

        if (nameList[j] > nameList[j+1])
            {
                 temp = nameList[j];
                 nameList[j] = nameList[j+1];
                 nameList[j+1] = temp;
            }
        }
    }
    cout << endl;
    cout << "The list has been sorted alphabetically." << endl;

commandList(nameList);
} 

// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------    
void inputCall(vector <string>& nameList) // Function to complement the menu for switch casing
{
    bool running = true;

    char input;
        do {
            input = getch();
            switch(input)
            {
            case 'a': addNames(nameList);break;
            case 'A': addNames(nameList);break;
            case 's': sortList(nameList);break;
            case 'S': sortList(nameList);break;
            case 'l': showList(nameList);break;
            case 'L': showList(nameList);break;
            case 'f': findName(nameList);break;
            case 'F': findName(nameList);break;
            case 'r': removeName(nameList);break;
            case 'R': removeName(nameList);break;
            case 'q': exit(0);break;
            case 'Q': exit(0);break;
            default : cout << "Unknown Command: Enter a command from the menu." << endl; continue;
            }
    } while (running);
}

1 个答案:

答案 0 :(得分:1)

如果你插入 位于pos = input.find(' ');

之上的else { }中的( if(pos != -1) )

您的代码可以使用