getline 跳过第一个输入字符 c++ [关闭]

2024-04-16

所以我制作这个程序已经有一段时间了。我查遍了互联网,但没有找到有效的解决方案。每次我在 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

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


我猜你正在打电话ignore就在之后getline摆脱尾随的换行符。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

getline 跳过第一个输入字符 c++ [关闭] 的相关文章

随机推荐

  • 根据javascript中深度嵌套对象中的值过滤数组

    我有具有以下结构的数组 var topics id 1 name topic title 1 sub categories id 1 name category title 1 indicators id 1 name indicator
  • Swift 的 Decimal 精度问题

    根据文档here https developer apple com documentation foundation decimal Swift 3 4 Decimal 类型是以 10 为基数桥接到 NSDecimalNumber 的表示
  • 如何将 Get-WMIObject 查询中的数据解析为字符串?

    我有以下代码行 get wmiobject class win32 computersystem select object username 它返回 用占位符编辑 username DOMAIN jsmith 需要做什么来删除填充并给我一
  • ActiveRecord“销毁”方法在 Ruby on Rails 中返回布尔值?

    我正在使用 Ruby on Rails 3 我想知道什么类型的返回将具有以下代码 user destroy 我需要它来处理案件success and fault以这样的方式 if user destroy puts True else pu
  • 在 Azure Web 应用程序上找不到证书

    我将 Web 应用程序部署为 Azure 应用服务上的 Web 应用程序 我将一些证书上传到 Azure 门户 因为 Web 应用程序通过 SSL 运行 并且我们使用另一个证书来执行一些解密 对于后一种情况 我有一种方法 在本地工作正常 来
  • 我应该将“orderby group.key”放在这个 LINQ 语句中的什么位置?

    这段代码 string words car boy apple bill crow brown var groups from w in words group w by w 0 into g select new FirstLetter
  • 如何区分刷新触发的Unload事件还是窗口关闭触发的Unload事件?

    刷新操作和窗口关闭操作都可以触发卸载事件 有没有办法区分实际触发它的操作 在我的情况下 我想忽略刷新操作 您能给我一些解决办法吗 我注意到已经有这种question https stackoverflow com questions 568
  • 在 WinForm 上禁用最小化和最大化?

    WinForms 在右上角有三个框 分别用于最小化 最大化和关闭窗体 我想要做的是删除最小化和最大化 同时保持关闭 我还想使关闭最小化表单而不是关闭它 如何才能做到这一点 The Form有两个属性称为MinimizeBox and Max
  • 如何检查何时为特定 dag 安排了下一次 Airflow DAG 运行?

    我已设置气流并运行一些 DAG 安排每天一次 0 0 我想检查下次计划运行特定 dag 的时间 但我看不到可以在管理员中执行此操作的位置 如果你愿意 你可以使用Airflow s CLI 有next execution option htt
  • 如何在 gcc 内联汇编中声明和初始化局部变量而不使用扩展内联 asm?

    我知道这是一个非常基本的问题 但我真的很困惑 事实上我绝对是 GCC 语法的新手 我想要拥有局部变量 带有标签的堆栈地址 而不使用扩展内联汇编 类似 Intel 语法中的以下代码 DATA1 DB 100 MOV AL DATA1 我猜这是
  • MySQL / 经典 ASP - 参数化查询

    在绝对紧急的情况下 我正在尝试浏览我的网站并添加参数化查询 我是新手 刚刚了解它们 我的问题是 我对连接类型知之甚少 并且我看到的所有示例都使用另一种连接方法 这让我感到困惑 我并不是特别想改变连接到数据库的方式 因为它位于很多页面上 我只
  • 如何使用 C# 从 Excel 工作表中删除 VB 代码?

    有谁知道怎么删除all使用 C 的 VB 代码形成 Excel 工作簿 这段代码不起作用 它删除第一个 最后一个 VBComponent 但在第二个VBComponent上引发ArgumentException VBProject proj
  • 形成 Mockito“语法”

    Mockito 看起来像是一个非常可爱的 Java 存根 模拟框架 唯一的问题是我找不到任何关于使用他们的 API 的最佳方法的具体文档 测试中常用的方法包括 doXXX Stubber when T OngoingStubbing the
  • 如何绘制 UIBezierPath

    这就是我想做的 我有一个 UIBezierPath 我想将它传递给某种方法来绘制它 或者简单地从创建它的方法中提取它 我不确定如何指示应在哪个视图中绘制它 所有绘图方法都必须以 void drawRect CGRect rect 我可不可以
  • Visual Studio 2017 无法修改 - 需要重新启动

    我已经尝试更新和修改 Visual Studio 2017 两天了 但每次运行 Visual Studio 安装程序时 我都会收到以下消息 需要重新启动 如果需要 任何剩余的设置都将恢复 重启后 显然我重新启动了大约10次 知道我必须删除什
  • 外部 jQuery 根本不执行

    下面的代码放在里面时完全可以工作运行页面上的标签 我后来把代码移到了外面 js用于组织目的的文件导致代码停止工作 当应触发某些事件时没有任何反应 我确保脚本包含在给定页面上 此外 我通过 查看源 确保链接有效 当我单击脚本的路径时 脚本会在
  • Pycharm 在移动现有虚拟环境或删除并创建新虚拟环境后忽略新虚拟环境

    如果我在创建项目时允许PyCharm创建虚拟环境 则删除或移动venv文件夹 它不会让我选择一个新文件夹 我可以进入设置中的 Python 解释器菜单 然后选择我自己创建的现有解释器 注意 brokenInterpreter oldFold
  • 在 VBS 中使用环境变量的值时出现问题

    我是 VBScript 新手 编写了一个可以修改 XML 文件的小脚本 但我在将计算机名称放入 XML 时遇到问题 我从以下位置获取了计算机名称HOST使用这些行的环境变量 Set wshShell CreateObject WScript
  • DEP0800:升级到 VS 2015 Update 3 后无法部署 UWP 应用

    升级到 VS 2015 Update 3 后 我很难让我的 UWP 应用程序在调试模式下部署 2 gt 检查是否安装了所需的框架 2 gt 框架 Microsoft VCLibs 140 00 Debug x86 当前未安装应用程序包版本1
  • getline 跳过第一个输入字符 c++ [关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 所以我制作