我试图使用do while循环重复我的程序的某一部分,它拒绝正确执行 [英] I am trying to use a do while loop to repeat a certain portion of my program and it refuses to execute properly

查看:100
本文介绍了我试图使用do while循环重复我的程序的某一部分,它拒绝正确执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,因为标题说它拒绝执行的东西在do函数下,即使我可以告诉所有的参数为一个重复已经满足。到目前为止,我得到当我运行程序是一些沿线的...
你想搜索另一个名称吗?
请输入Y为yes和n为no:$ b $

Okay so as the title said its refusing to execute the stuff right under the "do" function even though as far as i can tell all the parameters for a repeat have been fulfilled. So far what i get when i run the program is something along the lines of... "Would you like to search another name? Please enter Y for yes and n for no:" looping over and over when i press 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;
}

编辑:
发布整个程序,请原谅任何语法错误,

Posted whole program, please excuse any grammatical mistakes, I'm just trying to get the program down before i go in there and make it pretty.

推荐答案

你的循环的尾巴这样做:

The tail of your loop does this:

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

如果输入一个字符串,它将使用字符串,但在输入流中留下结尾的换行符。因此,当您在输入 Y y 后返回到循环顶部,并执行以下操作:

which will consume your string if you entered one, but leave the trailing newline in the input stream. Thus when you return to the top of the loop after entering Y or y, and do this:

cout << "Please Enter a name to be searched:" << endl;
getline(cin, sSearch);

getline 将提取一个空行。

如何从输入流中使用未读的换行符由您自己决定。你可能会最终使用 .ignore(),就像你在程序中做的那样。或者使用 getline 来使用 cQuestion 。你有选择。选择一个有效的方法。

How to consume the unread newline from the input stream is up to you. You will likely just end up using .ignore() as you did prior in your program. Or use getline to consume cQuestion. You have options. Pick one that works.

另外,我会强烈地 建议您在假设他们工作之前。这是一个艰难,但必要的习惯,打破。像这样:

And as a side note, I would strongly advise you check your stream operations for success before assuming they "just worked". That is a hard, but necessary, habit to break. Something like this:

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 while循环重复我的程序的某一部分,它拒绝正确执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆