什么是我不理解有关函数getline +字符串? [英] What am I not understanding about getline+strings?

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

问题描述

这是使用计算器我的第一次。我一直无法找到我需要的getline的信息。我在一个简单的编程类工程转让,让我们写code是pretty简单。所有我想在这里做的就是把问题和答案,一个用户自定义数字分成两个不同的阵列。我while循环看起来像这样(我是用一个for循环,但切换到而只是为了看它是否会停止破坏):

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为他们工作的其余部分。所以,如果我有5个尺寸1,程序只填充阵列位置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'的,这应该使其工作,除非你的code其他错误。

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 +字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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