我对 getline+strings 有什么不了解? [英] What am I not understanding about getline+strings?

查看:21
本文介绍了我对 getline+strings 有什么不了解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 stackoverflow.我一直无法找到我需要的关于 getline 的信息.我在一个简单的工程传输编程课程中,所以我们编写的代码非常简单.我在这里要做的就是将用户定义数量的问题和答案放入两个不同的数组中.我的 while 循环看起来像这样(我使用了 for 循环,但切换到 while 只是为了看看它是否会停止中断):

This is my first time using stackoverflow. I've been unable to find out the information I need regarding getline. I'm in a simple programming class for engineering transfers so the code that we write is pretty simple. All i'm trying to do here is put a user-defined number of questions and answers into two different arrays. My while loop looks like this (I was using a for loop but switched to while just to see if it would stop breaking):

int main ()
{
    srand((unsigned)time(0));
    string quest1[100], answ1[100];
    int size1, x = 0, num, count1, visit[100], shuffle[100];
    fstream flashcard1; 

    cout << "flashcard.cpp by NAME\n" << endl;
    cout << "This program allows user to manipulate questions and answers for studying.\n" << endl;
    cout << "\nHow many flash cards will be entered(MAX 100)? ";
    cin >> size1;
    cout << endl;

    while(x < size1)
    {
        cout << "Enter Question: ";
        getline(cin , quest1[x]);
        cout << endl;
        x = x++;

        /*
        cout << "Enter Answer: " << endl;
        getline(cin,answ1[x]);
        cout << endl;
        flashcard1.open("flashcard1.dat", ios::app);
        flashcard1 << quest1[x] << " " << answ1[x] << endl;
        flashcard1.close();
        cout << "Data Stored." << endl;
        */
    }
}

我记下了答案输入部分以及仅用于调试的将数据保存到文件中.当我运行程序时,它会跳过第一个问题的 getline,显示输入问题"的第二个循环,getline 适用于其余的问题.因此,如果我的 size1 为 5,则程序仅填充数组位置 1-4.请帮忙.这是一个简单的闪存卡程序,它的作用与您创建闪存卡以学习和洗牌一样.

I noted out the answer entering part as well as the saving data to the file just for debugging. When I run the program it skips the getline for the first question, displays the second loop of "Enter question" and the getline works for the rest of them. So if I have a size1 of 5, the program only fills array positions 1-4. Please help. This is a simple flash card program that will do the same thing as if you were to create flash cards to study and shuffle them.

推荐答案

它似乎跳过第一次迭代的原因是因为当你这样做时

The reason it's appearing to skip the first iteration is because when you do

cin >> size1;

您输入一个数字并按 Enter 键.cin 读取整数并将换行符留在缓冲区中未读,这样当你调用getline时,就好像你立即按下了回车键键,getline 不读取任何内容(因为它在读取换行符之前停止),丢弃换行符,并将空字符串放入 quest1[0].这就是 getline 的其余部分正确"工作的原因.

You enter a number and hit the Enter key. cin reads the integer and leaves the newline character unread on the buffer, so that when you call getline, it's as if you immediately hit the enter key, and getline reads nothing (because it stops before reading the newline character), discards the newline, and puts the empty string in quest1[0]. And that's why the rest of the getlines work "correctly".

在你的循环上方添加 cin.ignore('\n') 以摆脱挥之不去的 '\n',这应该使它工作,除非其他代码中的错误.

Add cin.ignore('\n') above your loop to get rid of the lingering '\n', and that should make it work, barring other errors in your code.

并且不要忘记将 x = x++ 更改为 x++ 以避免 UB.

And don't forget to change x = x++ to just x++ to avoid UB.

这篇关于我对 getline+strings 有什么不了解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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