简体   繁体   English

C程序正在大量打印

[英]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. 我正在尝试运行该程序,要求用户输入两个日期,格式为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). 但是它将继续打印1/1/1早于1/1/1(假设1/1/1是输入两次的日期)。 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. 我也在使用bool,不确定是否正确使用了它。

// 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. 这是一个逻辑问题,您需要3个状态而不是2个状态。 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. 当程序到达if (first_date ...部分时,由于first_datetrue ,因此您的程序行为正确。

A quick fix would be to add another if statement around your last two if cases and use another bool. 一个快速的解决方案是在最后两个if情况附近添加另一个if语句,然后使用另一个布尔值。 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. 我认为这对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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