【问题标题】:getline skipping first input character c++ [closed]getline跳过第一个输入字符c ++ [关闭]
【发布时间】:2013-04-12 02:57:03
【问题描述】:

所以我制作这个程序已经有一段时间了。我浏览了整个互联网,但没有找到任何解决方案。每次我在 arr[i].question 和 arr[i].answer 中输入我的输入时,它都会说我的问题是错误的,而我没有给出问题的答案。我尝试过使用 cin.ignore()、cin.clear() 和 cin.sync()。我可能在错误的地方使用它们,但我不确定。我可能会感到困惑,所以请看代码。

这是输入格式。

    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();

}

这是测验你的输出格式。

    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++;
    }

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

编辑:这是整个代码:

// 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;
}

当我运行它时,它会变成这样

    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

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

【问题讨论】:

  • cin.sync 不能保证按照它所说的去做,所以我不会依赖这种行为。
  • 我根本不清楚你在说什么。可以发布一个完整的程序并清楚地说明您输入的内容以及您在控制台中看到的内容。只需剪切和粘贴整个程序和整个控制台会话。 jrok 是正确的, cin.ignore() 在您使用它的所有场合都是不正确或次优的。 cin.sync() 和 cin.clear() 无关紧要。我猜你在没有了解问题的情况下阅读了互联网上的所有这些帖子。
  • 好吧,这里是整个代码。
  • Jrok,当我摆脱任何 cin.ignore(); 时,它只是跳过所有输入,只说明输出。是的,我正试图摆脱换行符。我只是想不通它为什么会跳过。

标签: c++ loops getline


【解决方案1】:

我猜你是在 getline 之后调用 ignore 来去掉尾随的换行符。

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

【讨论】:

  • 是的!!!!!!!!!!!!!!!!!!!!!
猜你喜欢
  • 1970-01-01
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多