【问题标题】:String Vector program exits before input字符串向量程序在输入前退出
【发布时间】:2010-05-11 02:46:13
【问题描述】:

所以,我有一个项目必须添加、删除和打印向量的内容......问题是,当运行程序时,我可以在输入字符串以添加到向量之前退出。我评论了该部分所在的功能。

谢谢!

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

void menu();
void addvector(vector<string>& vec);
void subvector(vector<string>& vec);
void vectorsize(const vector<string>& vec);
void printvec(const vector<string>& vec);
void printvec_bw(const vector<string>& vec);

int main()
{
    vector<string> svector;

    menu();

    return 0;
}
//functions definitions

void menu()
{
    vector<string> svector;
    int choice = 0;

        cout << "Thanks for using this program! \n"
             << "Enter 1 to add a string to the vector \n"
             << "Enter 2 to remove the last string from the vector \n"
             << "Enter 3 to print the vector size \n"
             << "Enter 4 to print the contents of the vector \n"
             << "Enter 5 ----------------------------------- backwards \n"
             << "Enter 6 to end the program \n";
        cin >> choice;

        switch(choice)
        {

                case 1:
                    addvector(svector);
                    break;
                case 2:
                    subvector(svector);
                    break;
                case 3:
                    vectorsize(svector);
                    break;
                case 4:
                    printvec(svector);
                    break;
                case 5:
                    printvec_bw(svector);
                    break;
                case 6:
                    exit(1);
                default:
                    cout << "not a valid choice \n";

            // menu is structured so that all other functions are called from it.
        }

}

void addvector(vector<string>& vec)
{
    string line;

     int i = 0;

        cout << "Enter the string please \n";
        getline(cin, line); // doesn't prompt for input!
        vec.push_back(line);    

}

void subvector(vector<string>& vec)
{
    vec.pop_back();
    return;
}

void vectorsize(const vector<string>& vec)
{
    if (vec.empty())
    {
        cout << "vector is empty";
    }
    else
    {
        cout << vec.size() << endl;
    }
    return;
}

void printvec(const vector<string>& vec)
{
    for(int i = 0; i < vec.size(); i++)
    {
        cout << vec[i] << endl;
    }

    return;
}

void printvec_bw(const vector<string>& vec)
{
    for(int i = vec.size(); i > 0; i--)
    {
        cout << vec[i] << endl;
    }

    return;
}

【问题讨论】:

    标签: c++ function getline


    【解决方案1】:

    在运行程序时,您输入了数字 1 来告诉您的程序您要添加。输入 1 后,按 Enter,这会在输入中添加一个换行符以等待读取。在 addvector 函数中,readline 读取该换行符。

    既然这是家庭作业,那么在你了解问题后,最好自己找出解决方案。

    【讨论】:

    • 哦...我必须使用忽略功能。谢谢!
    【解决方案2】:

    &gt;&gt;getline 通话对彼此并不友好。 '>>' 不会吞下 \n,它最终会在 getline 中产生一个空字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 2021-08-13
      相关资源
      最近更新 更多