mardi 22 septembre 2015

C program is printing to much

I am trying to run this program which asks the user for the two dates in the format mm/dd/yy. After the two are entered the program is supposed to compare the two dates to see which one is larger. The problem I am running into is that when i enter in the same date the code spits out that "The dates are the same." But it continues to print 1/1/1 is earlier than 1/1/1 (assuming 1/1/1 is the date entered twice). When the code is the same how do I skip the next code? I want the program to end after the same dates are entered.

Below is the code that I have so far. I am also using bool and not sure if I am using this correctly.

// Preprocessor directives
#include <stdio.h>
#include <stdbool.h>


// Call main function
int main (void)
{
    // Declare variables
    int d1, m1, y1, d2, m2, y2;
    bool first_date = true;

    // Prompt user to enter information
    printf("Enter first date (mm/dd/yy): ");
    scanf("%d /%d /%d", &m1, &d1, &y1);

    printf("Enter second date (mm/dd/yy): ");
    scanf("%d /%d /%d", &m2, &d2, &y2);

    // if/else statements for comparison
    if (y1 < y2)
        first_date = true;
    else if (y1 > y2)
        first_date = false;
    else if (m1 < m2)
        first_date = true;
    else if (m1 > m2)
        first_date = false;
    else if (d1 < d2)
        first_date = true;
    else if (d1 > d2)
        first_date = false;
    else 
        printf("The dates are the same.\n");

    if (first_date == true)
    {
        printf("%d/%d/%d is earlier ijijthan %d/%d/%d\n", m1, d1, y1, m2, d2, y2);
    }
    else if (first_date == false)
    {
        printf("%d/%d/%d is earlier than %d/%d/%d\n", m2, d2, y2, m1, d1, y1);
    }



    // End program
    return(0);
}

Aucun commentaire:

Enregistrer un commentaire