Thread: ending lines

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Question ending lines

    I need to output the following to a screen:

    SUN MON TUE WED THU FRI SAT
    1 2 3 4
    5 6 7 8 9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31

    this is my code so far:

    #include<stdio.h>
    #include<conio.h>

    main()
    {

    int first_day,
    days_in_month;


    printf("Enter day on which the 1st falls 1 = Sunday 2 = Monday etc : ");
    scanf("%d", &first_day);

    printf("How many days the month? : ");
    scanf("%d", &days_in_month);

    printf("\n SUN MON TUE WED THU FRI SAT\n");

    int columns = 1;


    while (columns <= first_day -1)
    {
    printf(" ");
    columns += 1;
    }

    int day_index = 1;

    while (day_index <= days_in_month)
    {
    if (columns = 7)
    {
    printf("%4d", day_index);
    columns = 1;
    printf("\n");
    }
    else
    {
    printf("%d", day_index);
    columns += 1;
    }
    day_index += 1;
    }
    getch();

    }
    It's not ending the line as I need it to. Any suggestions?




  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include<stdio.h> 
    #include<conio.h> 
    int main() { 
        int first_day, days_in_month; 
        int columns = 1; 
        int day_index = 1;      // should be declared here - this is C, not C++
        printf( "Enter day on which the 1st falls 1 = Sunday 2 = Monday etc : " ); 
        scanf( "%d", &first_day ); 
        printf( "How many days the month? : " ); 
        scanf( "%d", &days_in_month ); 
        printf( "\n SUN MON TUE WED THU FRI SAT\n" ); 
        while(columns <= first_day -1) { 
            printf( "    " );
            columns += 1; 
        } 
        while(day_index <= days_in_month) { 
            if(columns == 7)   // should be == 
                { 
                printf( "%4d", day_index ); 
                columns = 1; 
                printf( "\n" ); 
            } else { 
                printf( "%4d", day_index );   // print this in a 4 width as well
                columns += 1; 
            } 
            day_index += 1; 
        } 
        getch( ); 
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  3. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 08:56 PM
  4. Reading lines from a file
    By blackswan in forum C Programming
    Replies: 9
    Last Post: 04-26-2005, 04:29 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM