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

时间:2015-10-06 18:48:08

标签: c++

我一直致力于一项对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专用代码(例如系统(&#34;暂停&#34;)),所以它应该可以与其他编译器一起使用。

0 个答案:

没有答案