Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在没有修改的情况下将C++键盘/CIN缓冲区的内容打印到控制台?_C++ - Fatal编程技术网

如何在没有修改的情况下将C++键盘/CIN缓冲区的内容打印到控制台?

如何在没有修改的情况下将C++键盘/CIN缓冲区的内容打印到控制台?,c++,C++,我一直在开发一个程序,它至少对我来说对cin及其功能有相当大的作用,我一直很难在整个程序中跟踪键盘缓冲区中的内容。这给我带来了一些错误——我想知道是否有一种功能或技术可以用来将cin/键盘缓冲区的内容打印到屏幕上,这样我就可以知道程序中该点的内容 我已经为任何感兴趣的人附上了源代码。这是我的一门课的家庭作业,所以我为那些认为这太容易而懒得问的人道歉 // CHAPTER 4 HOMEWORK - PROBLEM 16 - RUNNING THE RACE /* Write a program t

我一直在开发一个程序,它至少对我来说对cin及其功能有相当大的作用,我一直很难在整个程序中跟踪键盘缓冲区中的内容。这给我带来了一些错误——我想知道是否有一种功能或技术可以用来将cin/键盘缓冲区的内容打印到屏幕上,这样我就可以知道程序中该点的内容

我已经为任何感兴趣的人附上了源代码。这是我的一门课的家庭作业,所以我为那些认为这太容易而懒得问的人道歉

// CHAPTER 4 HOMEWORK - PROBLEM 16 - RUNNING THE RACE
/* Write a program that asks for the names of three runners and
   the time it took each of them to finish a race. The program
   should displace who came in first, second, and third place.
   Input validation: only accept positive numbers for the times*/

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{


    // Introduction for the user
    cout << "\t\t\t    ~ RUNNING THE RACE ~\n"
         << "\t\t\t    --------------------\n\n"
         << "Welcome! In this program, you can enter the names of three runners,\n"
         << "and the times at which they finished a race. The program will organize\n"
         << "the names and times from first to last.\n\n"

         << "Are you ready to begin?\n"
         << "Press the Enter key to continue . . . ";

    cin.get();

    // declare variables and string objects
    string runnerName1, runnerName2, runnerName3;
    int minutes1, minutes2, minutes3;
    double seconds1, seconds2, seconds3;

    // RUNNER 1
    cout << "\nFirst we'll deal with runner 1.\n"
         << "Please enter runner 1's full name: ";
    getline(cin, runnerName1);
    cout << "Okay! Now for " << runnerName1 << "'s time.\n"
         << "First, enter " << runnerName1 << "'s minutes (a whole number): ";
    cin >> minutes1;
    cout << "And now enter " << runnerName1 << "'s seconds: ";
    cin >> seconds1;


    // RUNNER 2
    cout << "\nNext we'll deal with runner 2.\n"
         << "Please enter runner 2's full name: ";
    cin.ignore(); // because of the remaining space in the keyboard buffer leftover from the last cin >> statement
    getline(cin, runnerName2);
    cout << "Good! Now for " << runnerName2 << "'s time.\n"
         << "Enter " << runnerName2 << "'s minutes (a whole number): ";
    cin >> minutes2;
    cout << "Now enter " << runnerName2 << "'s seconds: ";
    cin >> seconds2;


    // RUNNER 3
    cout << "\nFinally, runner 3.\n"
         << "Please enter runner 3's full name: ";
    cin.ignore();
    getline(cin, runnerName3);
    cout << "What is " << runnerName3 << "'s time?\n"
         << "First, enter " << runnerName3 << "'s minutes (a whole number): ";
    cin >> minutes3;
    cout << "Now enter " << runnerName3 << "'s seconds: ";
    cin >> seconds3;

    // display the gathered data in a useful manner
    cout << "___________________________________________\n" // border

         << "RESULTS:\n\n"

         << "NAME:\t\t\tTIME:\n";

    // make sure any extra seconds entered above 60 gets added to the number of minutes
    while (seconds1 >= 60.0)
    {
        seconds1 -= 60.0;
        minutes1 += 1;
    }

    while (seconds2 >= 60.0)
    {
        seconds2 -= 60.0;
        minutes2 += 1;
    }

    while (seconds3 >= 60.0)
    {
        seconds3 -= 60.0;
        minutes3 += 1;
    }

    // now for the results . . . 
    cout << runnerName1 << "\t" << minutes1 << ":" << seconds1 << endl;
    cout << runnerName2 << "\t" << minutes2 << ":" << seconds2 << endl;
    cout << runnerName3 << "\t" << minutes3 << ":" << seconds3 << endl;

    cin.ignore();
    cin.get();
    return 0;
}

此外,这是基于VS 2012 Express构建的,适用于具有多个版本的任何人。我没有在程序中使用任何windows独占代码,例如systempause,因此它可以与其他编译器配合使用。

在此处发布代码,而不是作为链接。您应该能够使用调试器检查cin的内部详细信息。您真的需要一个可移植的API吗?作为一般建议,您可能希望对这段代码使用一个循环,而不是将同一代码块复制三次。@Barmar这可以通过VS中的DataTips功能实现吗?您知道替代IDE中的类似功能吗?至于可移植性,我想我并没有这样想——两者都可以。让它在代码中工作仍然是一个很酷的技巧。对不起,我对VS一无所知。我不知道为什么您需要在代码中这样做,因为您似乎只想这样做来解决您在处理输入时遇到的问题。这就是调试器的用途。