user2970001
user2970001

Reputation: 65

C++ calender, closes before I can see output

So basically I created this calender and when it outputs you can briefly see that it's right but it just closes down, can someone help me to keep it open to display the output? I know I should know what this is but this took me a long time to do and my mind just isn't in the right place. thanks


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

Upvotes: 0

Views: 184

Answers (4)

Paweł Stawarz
Paweł Stawarz

Reputation: 4012

Since noone mentioned it - you can also do system("pause") on the Windows system family. It does what you need in the most 'elegant' way.

Upvotes: 0

Codecat
Codecat

Reputation: 2241

I usually just use getchar() to wait for user input, which is shorter than std::cin.get().

Upvotes: 0

David G
David G

Reputation: 96810

Use std::cin.get() to keep the window open.

Upvotes: 1

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153820

The easiest way is probably to ask for some input right before the end of main:

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

This will wait for the enter key to be hit: since there was just some numeric input done, there is still, at least, a newline in the input buffer. The first line gets rid of any already entered line. The second line then waits for the enter key.

Upvotes: 0

Related Questions