简体   繁体   中英

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

It's a logic problem, you need 3 states instead of two. Try like this

int result; /* 1 for <, 2 for > and 0 for equality */

result = 0; /* By default, they compare equal unless ... */
if (y1 < y2)
    result = 1;
else if (y1 > y2)
    result = 2;
else if (m1 < m2)
    result = 1;
else if (m1 > m2)
    result = 2;
else if (d1 < d2)
    result = 1;
else if (d1 > d2)
    result = 2;

switch (result)
{
case 1:
    printf("%d/%d/%d is earlier ijijthan %d/%d/%d\n", m1, d1, y1, m2, d2, y2);
    break;
case 2:
    printf("%d/%d/%d is earlier than %d/%d/%d\n", m2, d2, y2, m1, d1, y1);
    break;
case 3:
    printf("The dates are the same.\n");
    break;
}

Your program was behaving correctly since first_date was true when the program reached the if (first_date ... part.

A quick fix would be to add another if statement around your last two if cases and use another bool. Then, when your program prints that the dates are the same, the bool could be updated. The modified program would look something like this:

// 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;
    bool same = false; //NEW BOOL

    // 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");
        same = true; //CHANGE VALUE IF SAME
    }

    if(same == false){
        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);
}

I think that this would work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM