getline跳过第一个输入字符c ++ [英] getline skipping first input character c++

查看:102
本文介绍了getline跳过第一个输入字符c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经做了一段时间了.我已经遍及整个互联网,但没有找到有效的解决方案.每次我在arr [i] .question和arr [i] .answer中输入内容时,都会说我的问题是错误的,如果我不给出问题的答案.我尝试使用cin.ignore(),cin.clear()和cin.sync().我可能在错误的地方使用了它们,但我不确定.我可能会感到困惑,所以只需看一下代码即可.

So I have been making this program for a while now. I have looked all over the internet and none of the solutions I have found work. Every time I put in my input in the arr[i].question and arr[i].answer, it says that my question is wrong without me giving a answer to the question. I have tried using cin.ignore(), cin.clear(), and cin.sync(). I might have been using them in the wrong places but i'm not sure. I might be confusing, so just look at the code.

这是输入格式.

    cin >> count;
cin.ignore();

for(int i =0; i < count; i++){
    cout << "Enter the question.\n" << endl;
    //enter the question

    getline(cin, arr[i].question);
    cin.ignore();

    cout << "Enter the answer.\n" << endl;
    //enter the answer

    getline (cin, arr[i].answer);
    cin.ignore();

}

这是测验您的输出格式.

and here is the out put format to quiz you.

    for(int j =0; j < count; j++){
    cout << "\n" << arr[j].question << endl;
    getline(cin, userguess);

    if(arr[j].answer.compare(userguess) !=0){
        cout << "Wrong. Keep trying!\n";
        incorrect++;
        total++;
    }
    if(arr[j].answer.compare(userguess) ==0){
        cout << "Nice job. Keep it up!\n";
        correct++;
        total++;
    }

每当我输入问题时,它不会在控制台中输出问题,也不让我输入答案.只是说错了. 请帮忙吗?

Whenever I put in my question it doesn't output a question in the console or let me put in an answer. It just says wrong. A little help please?

这是整个代码:

// final PROJECT.cpp : Defines the entry point for the console application.

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <cstring>
#include <cstdio>
#include <stdio.h>
#include <tchar.h>
#include <string.h>
#include <cstdlib>

using namespace std;

struct flashcards{
string question;
string answer;
}FC1, FC2, FC3, FC4, FC5, FC6, FC7, FC8, FC9, FC10, FC11, FC12, FC13, FC14, FC15, FC16, FC17, FC18, FC19, FC20;

int _tmain(int argc, _TCHAR* argv[])

{
system ("color 4B");
string userguess;
string again;
flashcards arr[10000];
int count;
float correct=0;
float incorrect=0;
float total=0;
//one major problem, wont accept spaces.
cout<< "Flash Card Runner\n";
cout<< "This program was made by Jacob Malcy\n";
cout<< "Beta 3.8\n";
cout<< "IN ORDER FOR YOU TO HAVE MULTIPLE WORD FLASH CARDS,\n";
cout << "SKIP NUMBER ONE FLASHCARD QUESTION AND ANSWER!\n";
cout<< "This bug is currently being worked on.\n";
cout << "If you happen to have problems conntact Jacob.\n";
int choice;


cout<< "Would you like to create or test? Enter 0 for create and 1 for test.\n";
cin >> choice;
if(choice==0){
    //Creating new deck of cards
cout<< "How many flashcards do you want?\n";
cin >> count;
cin.clear();

for(int i =0; i < count; i++){
    cout << "Enter the question.\n" << endl;
    //enter the question

    getline(cin, arr[i].question);
    cin.ignore();

    cout << "Enter the answer.\n" << endl;
    //enter the answer

    getline (cin, arr[i].answer);
    cin.ignore();

}
}
else if(choice==1){
    //Reading in new file
    cout << "Reading file...\n";

    string line;
    ifstream myfile ("Save.txt");
    if (myfile.is_open())
    {
        count = 0;

        while ( myfile.good () )
        {
            getline (myfile,line);
            arr[count].question = line;
            cout << line << endl;

        getline (myfile,line);
        arr[count].answer = line;
        cout << line << endl;
        count++;

        }

        myfile.close();
    }

    else cout << "Unable to open the file";
}

do
{

for(int j =0; j < count; j++){
    cout << "\n" << arr[j].question << endl;
    getline(cin, userguess);

    if(arr[j].answer.compare(userguess) !=0){
        cout << "Wrong. Keep trying!\n";
        incorrect++;
        total++;
    }
    if(arr[j].answer.compare(userguess) ==0){
        cout << "Nice job. Keep it up!\n";
        correct++;
        total++;
    }


}
cout<< "The number of correct questions answered was: \n" << correct << endl;
cout<<"The number of total questions answered was: " << total <<"\n";
    cout<< "The number of incorrect questions answered was: \n" << incorrect << endl;
    //cout<< total;
    float percent = (correct/total)*100;
    cout<< "The total percent you got right is: \n" << percent << "% correct" << endl;
    system("pause");
    cout << "Would you like to run the quiz again?\n"
    << "Type y or Y to run this again. If not, enter any other letter.\n";
    cin >> again;
    if((again == "y") || (again == "Y")){
    correct=0;
    incorrect=0;
    total=0;
    }

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

ofstream myfile ("Save.txt");
if (myfile.is_open())
{

    for(int i =0; i <count; i++){

        myfile << arr[i].question << "\n";
        myfile << arr[i].answer << "\n";
    }
    myfile.close();

    }
else cout << "Unable to save file";

cout << "Your finished with the quiz. Goodbye!\n";

system("PAUSE");
return 0;
}

当我运行它时,它会这样显示

When I run it, it comes out like this

    Flash Card Runner
This program was made by Jacob Malcy
Beta 3.8
IN ORDER FOR YOU TO HAVE MULTIPLE WORD FLASH CARDS,
SKIP NUMBER ONE FLASH CARD QUESTION AND ANSWER!
This is a bug is currently being worked on.
if you happen to have problems, conntact Jacob.
Would you like to create or test? Enter 0 for create and 1 for test.

如果我输入零:

How many flashcards do you want?

说我输入两个2

Enter the question.

Hi ho
Enter the answer.

Merry oh

enter the question.

fi fa
enter the answer.

fo fum

然后它会跳到:

Wrong. Keep trying!

erry oh

在输入错误之前,它应该显示第一个问题,并给我答案的机会. 在我可以之前,它只是说错了.然后显示答案,但缺少第一个字符.你去那里.

Before wrong, it should display the first question, and give me the oppertunity to answer. It just says wrong before i can. then it displays the answer with the first character missing. There you go.

推荐答案

我想您是在getline之后立即调用ignore来摆脱尾随的换行符.

I presume you're calling ignore right after getline to get rid of the trailing newline character.

不要那样做. std::getline已经从流中提取了换行符(并将其丢弃),因此您将忽略下一行的第一个字符.

Do not do that. std::getline already extracts the newline character from the stream (and discards it), so you're ignoring the first character of the next line.

这篇关于getline跳过第一个输入字符c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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