C++日历,在我看到输出之前关闭

C++ calender, closes before I can see output

本文关键字:输出 日历 我看 C++      更新时间:2023-10-16

所以基本上我创建了这个日历,当它输出时,您可以简要地看到它是正确的,但它只是关闭,有人可以帮我保持打开状态以显示输出吗?我知道我应该知道这是什么,但这花了我很长时间才做,我的思想只是不在正确的地方。谢谢


#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include "float.h"
#include <stack>
using namespace std;
using std::stack;
int calendar[6][7];
void cal(int y, int z) // y is number of days and z is the number corresponding 
{                              // to the first day
    int n = 1;
    for (int j = z - 1; j<7; j++)
    {
        calendar[0][j] = n;
        n++;
    }
    for (int i = 1; i<6 && n <= y; i++)
    {
        for (int k = 0; k<7 && n <= y; k++)
        {
            calendar[i][k] = n;
            n++;
        }
    }
}
int main()
{
    int d;
    int day;
    cout << "Enter number of days : ";
    cin >> d;
    cout << "Enter first day of the month(1 for monday 7 for sunday..) : ";
    cin >> day; cout << "n";
    cal(d, day);
    cout << "M       T       W       T       F       S       S" << endl;
    cout << "n";
    for (int i = 0; i<6; i++)
    {
        for (int j = 0; j<7; j++)
        {
            cout << calendar[i][j] << "t";
        }
        cout << "" << endl; 
    }
}

使用 std::cin.get() 使窗口保持打开状态。

由于没有人提到它 - 您也可以在Windows系统系列上做system("pause")。它以最"优雅"的方式满足您的需求。

最简单的方法可能是在 main 结束之前请求一些输入:

std::cin.ignore(std::numeric_limits<std::streamsize>::max());
std::cin.ignore();

这将等待 Enter 键被点击:由于只完成了一些数字输入,因此输入缓冲区中至少还有一个换行符。第一行删除任何已输入的行。然后,第二行等待回车键。

我通常只用getchar()来等待用户输入,比std::cin.get()短。