C++ 我试图使用do while循环来重复程序的某一部分,但它拒绝正确执行

C++ 我试图使用do while循环来重复程序的某一部分,但它拒绝正确执行,c++,visual-c++,C++,Visual C++,好的,正如标题所说,它拒绝执行“do”函数下的内容,即使就我所知,重复的所有参数都已实现。到目前为止,当我运行这个程序时,我得到的是一些类似于。。。 “是否要搜索其他名称? 请输入Y表示“是”,输入n表示“否”: 当我按下y键时,循环一次又一次 #include <iostream> #include <string> #include <iomanip> #include <vector> #include <algorithm> #

好的,正如标题所说,它拒绝执行“do”函数下的内容,即使就我所知,重复的所有参数都已实现。到目前为止,当我运行这个程序时,我得到的是一些类似于。。。 “是否要搜索其他名称? 请输入Y表示“是”,输入n表示“否”: 当我按下y键时,循环一次又一次

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;

int main()
{
    vector <string> vName, vID, vClass;
    string sName, sID, sClass, sSearch, cQuestion;
    int iSize, iStudent;
    // Display initial vector size
    iSize = vName.size();

    cout << "Student list starts with the size:" << iSize << endl;

    // Get size of list from user

    cout << "How many students would you like to add?" << endl;
    cin >> iStudent;
    cin.ignore();
    // Get names, ids, and classes

    for (int i = 0; i < iStudent; i++)
    {
        cout << "Student" << i + 1 << ":\n";
        cout << "Please enter the student name: ";
        getline(cin, sName);
        vName.push_back(sName);

        cout << "Enter ID number ";
        getline(cin, sID);
        vID.push_back(sID);

        cout << "Enter class name ";
        getline(cin, sClass);
        vClass.push_back(sClass);

    }
    // Display header

    cout << "The list of students has the size of: " << iStudent << endl;
    cout << "The Student List" << endl;
    cout << "\n";
    cout << "Name:" << setw(30) << "ID:" << setw(38) << "Enrolled Class : " << endl;
    cout << "--------------------------------------------------------------------------";
    cout << "\n";

    // for loop for displying list
    for (int x = 0; x < vName.size() && vID.size() && vClass.size(); x++)
    {

        cout << vName[x] << "\t \t \t" << vID[x] << "\t \t \t" << vClass[x] << endl;
    }

    // Sorting function
    cout << "\n";
    cout << "The Student List after Sorting:" << endl;
    cout << "\n";

    sort(vName.begin(), vName.end());

    for (int y = 0; y < vName.size(); y++)
    {
        cout << vName[y] << endl;
    }

    cout << "\n";

    // Search function

    do
    {
        cout << "Please Enter a name to be searched:" << endl;
        getline(cin, sSearch);
        if (binary_search(vName.begin(), vName.end(), sSearch))
        {


            cout << sSearch << " was found." << endl << endl;
        }

        else
        {

            cout << sSearch << " was not found." << endl << endl;


        }

        cout << "Would you like to search another name?" << endl << endl;

        cout << "Please enter Y for Yes and N for No:" << endl << endl;
        cin >> cQuestion;

    } while (cQuestion == "Y" || cQuestion == "y");


    cout << "Thank you for using this program!" << endl;

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
向量vName,vID,vClass;
字符串sName、sID、sClass、sSearch、cQuestion;
智力,是学生;
//显示初始向量大小
iSize=vName.size();

cout如果cQuestion是一个字符数组,则需要使用strcmp或stricmp将其与另一个字符串(即本例中的“Y”和“Y”)进行比较。如果cQuestion是单个字符,则需要与“Y”和“Y”进行比较(即使用单个引号)

< C++ > C++中的字符串不是第一类,因此它们没有其他的基本操作类型,如int和浮点。
如果您只是将cQuestion的类型更改为std::string,那么您的代码应该可以工作,但如果您想使用chars,则需要更改引号样式。

循环的尾部执行以下操作:

cout << "Please enter Y for Yes and N for No:" << endl << endl;
cin >> cQuestion;
getline
将提取一个空行

如何使用输入流中未读的换行符取决于您。您可能会像之前在程序中那样使用
.ignore()
。或者使用
getline
使用
cQuestion
。您有选择。选择一个有效的

作为补充,我强烈建议您在假设流操作“刚刚工作”之前检查流操作是否成功。这是一个很难打破的习惯,但也是必要的。类似这样:

do
{
    cout << "Please Enter a name to be searched:" << endl;
    if (!getline(cin, sSearch))
        break;

    if (binary_search(vName.begin(), vName.end(), sSearch))
    {
        cout << sSearch << " was found." << endl << endl;
    }
    else
    {
        cout << sSearch << " was not found." << endl << endl;
    }

    cout << "Would you like to search another name?" << endl << endl;
    cout << "Please enter Y for Yes and N for No:" << endl << endl;

} while (getline(cin,cQuestion) && (cQuestion == "Y" || cQuestion == "y"));
do
{

难道你还没有展示你的完整程序,所以我只能猜测,但我的猜测是cQuestion是一个字符,你应该写“Y”而不是“Y”在这种情况下。使用所有警告和调试信息编译,然后使用调试器。它实际上是一个字符串。我一直在尝试字符数据类型和非字符串。我最近的迭代尝试将其转换为字符串数据类型…我将继续发布整个程序。哦,哇,我不敢相信这是一个缓冲区的东西。我不会继续WhozCraig,我还是个C++新手,我还没弄清楚如何正确地调试程序而不是点击F5。谢谢大家!“ZeZeZee你知道怎么接受答案,对吧?”ZeZee就不用担心了。只是FIY,花几分钟的时间阅读以下内容:这对解决你的问题很有帮助,对避免IPPR是很有帮助的。本论坛的操作问题。下面的一般帮助部分也值得一读(包含前面的链接)。无论如何,很高兴你的问题得到解决。祝你好运。
do
{
    cout << "Please Enter a name to be searched:" << endl;
    if (!getline(cin, sSearch))
        break;

    if (binary_search(vName.begin(), vName.end(), sSearch))
    {
        cout << sSearch << " was found." << endl << endl;
    }
    else
    {
        cout << sSearch << " was not found." << endl << endl;
    }

    cout << "Would you like to search another name?" << endl << endl;
    cout << "Please enter Y for Yes and N for No:" << endl << endl;

} while (getline(cin,cQuestion) && (cQuestion == "Y" || cQuestion == "y"));