什么是错误的原因,而在这个C程序返回一个结构? [英] What is the reason for error while returning a structure in this C program?

查看:71
本文介绍了什么是错误的原因,而在这个C程序返回一个结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计划旨在实现这一目标。

(A)写一个名为C函数大()返回传递给它的任何两个日期的较后日期。例如,如果日期2001年10月9日和2001年11月3日被传递到大(),第二个日期将被退回。

(B)创建是为(A)在一个完整的单元写入较大的()函数。存储由大()在一个单独的日期结构中返回的日期结构和显示返回的数据结构的成员值。

我工作的这个问题我的C语言课程。我拥有了一切顺利(我认为),但我一直得到更大的日期是:0/0/0不管我输入的内容。所以我就开始修修补补,现在我无法摆脱的语法错误,或找出我的0/0/0问题。显然,日期不会使它回到通过。我仍然在pretty在这个新的(和结构很新),所以任何帮助将真棒!

这是给我的语法错误的行靠近底部的main():

  DATES导致[NUM] =大(日期用户[NUM]);

在code全:

 的#include<&stdio.h中GT;
NUM的#define 2
结构日期
{
       INT月;
       日整型;
       INT年;
};
typedef结构日期日期;
DATES较大(日期[NUM]);
日期的更多;
诠释的main()
{
    DATES用户[NUM]    的printf(你将进入两个日期,程序将返回较大\\ n);
    而(用户[0] .month< 1 ||用户[0] .month> 12)
         {printf的(\\ n请输入第一个月,1-12:);
         scanf函数(%d个,&安培;用户[0] .month);}
    而(用户[0]·天< 1 ||用户[0]·天> 31)
          {printf的(\\ n请输入第一天,1-31:);
          scanf函数(%d个,&安培;用户[0]·天);}
    而(用户[0] .year< 1)
          {printf的(\\ n请输入元年:);
          scanf函数(%d个),&安培;用户[0] .year);}
    的printf(\\ nDate进入数:%d /%D /%d个\\ N,用户[0] .month,用户[0]·天,用户[0] .year);    而(用户[1] .month< 1 ||用户[1] .month> 12)
         {printf的(\\ n请输入第一个月,1-12:);
         scanf函数(%d个,&安培;用户[1] .month);}
    而(用户[1]·​​天< 1 ||用户[1]·​​天> 31)
          {printf的(\\ n请输入第一天,1-31:);
          scanf函数(%d个,&安培;用户[1]·​​天);}
    而(用户[1] .year< 1)
          {printf的(\\ n请输入元年:);
          scanf函数(%d个),&安培;用户[1] .year);}
    的printf(\\ nDate输入:%D /%D /%d个\\ n \\ n,用户[1] .month,用户[1]·​​天,用户[1] .year);    DATES导致[NUM] =大(日期用户[NUM]); / *问题就在这里* /    的printf(更大的日期%D /%D /%d个\\ n \\ n,导致[0] .month,导致[0]·天,结果[0] .year);
    系统(暂停);
    返回0;
}DATES较大(日期的[NUM])
{
      INT days0;
      INT days1;      days0 =(更多[0] .month * 31)+(更多[0]·天)+(更多[0] .year * 365);
      days1 =(更多[1] .month * 31)+(更多[1]·天)+(更多[1] .year * 365);      如果(days1> days0)
         {更[0] =更多[1];}返回(更多[0]);
}


解决方案

有你program.I许多逻辑和语法错误纠正了所有的语法部分,必须在<$ C $的逻辑指出重大缺陷C>,而 loops.You似乎混淆了&GT; &LT; 在以下条件:

 而(用户[0] .month&LT; 1 ||用户[0] .month&GT; 12); //错误而(用户[0] .month大于1 ||用户[0] .month&LT; 12); //正确

月比 12 小于 1 或更高只在火星,而是在地球上,由于污染,他们之间的 1 12 。我已经纠正了那些并指出这在comments.And你应该使用循环,而不是重复同样的事情对数组的每个元素 DATES

AAAAAGHHH 这是充满errors.Here的的工作version.It实现,你在你的问题,询问用户提到2日期2的目标,并找出哪一个更大/晚。我不包括 BC ,只有广告 years.Won't事,除非你想知道一些尼安德特人比任何潜伏,函数的提前或推迟生放大返回一个指针的答案,这是存储在一个新的数据结构 largerdate 并打印出来。

下面是修改后的code简要说明:

循环自动为每个date.There没有必要重复相同的code每个日期为您did.If的日期数输入上升,这将是tedious.Further,在做的,而循环要求在specified.If那个笨蛋做了错误的范围日期用户,他在被骂得而循环的条件下确保挺举必须输入again.The两个日期都存储在一个数组用户[] 和类型的基地址日期* 作为参数传递给函数大()这两个日期相比较,返回类型的指针日期* 较大date.This被用于更大的日期存储在一个名为 largerdate ,然后打印出新的数据结构。

 的#include&LT;&stdio.h中GT;
NUM的#define 2
结构日期
{
       INT月;
       日整型;
       INT年;
};
typedef结构日期日期;
日期*较大(日期*);
//日期的更多; //不是因为需要你正在做的函数定义相同INT主要(无效)
{
    DATES用户[NUM],largerdate;
    INT I;
     的printf(你将进入两个日期,程序将返回较大\\ n);
    对于(i = 0; I&LT;民;我++)// for循环是很方便的时候NUM增加。
       {
           做
           {
        的printf(\\ n请输入月份数%D,1-12:\\ n,I);
         scanf函数(%d个,&安培;用户[I] .month);
         如果(用户[I] .month&LT; 1 ||用户[I] .month&GT; 12)
         的printf(我告诉你输入1-12 JERK!\\ n之间的数字);
           }而(用户[I] .month&LT; 1 ||用户[I] .month&GT; 12);
          做
           {
        的printf(\\ n请输入日期编号%D,1-31:\\ n,I);
         scanf函数(%d个,&安培;用户[I]·天);
         如果(用户[I]·天&LT; 1 ||用户[I]·天&GT; 31)
         的printf(我告诉你输入1-31 JERK!\\ n之间的数字);
           }而(用户[I]·天&LT; 1 ||用户[I]·天&GT; 31);
           做
           {
        的printf(\\ n请输入年份号%D,大于一:\\ n,I);
         scanf函数(%d个,&安培;用户[I] .year);
         如果(用户[I] .year&LT; 1)
         的printf(我告诉你输入一个大于1的数JERK!\\ n);
           }而(用户[I] .year&LT; 1);
    的printf(%d个输入\\ nDate号码是:%D /%D /%d个\\ n,I + 1,用户[I] .month,用户[I]·天,用户[I] .year);   } // for循环ends.It避免重复的日期同样的事情[1]
   largerdate = *较大(用户);
   的printf(\\ n \\ n此大/稍后的日期为%d /%D /%d个\\ N,largerdate.month,largerdate.day,\\
   largerdate.year);
    系统(暂停);
    返回0;
}日期*较大(日期*更多)
{
      INT days0;
      INT days1;      days0 =(更多[0] .month * 31)+(更多[0]·天)+(更多[0] .year * 365);
      days1 =(更多[1] .month * 31)+(更多[1]·天)+(更多[1] .year * 365);      如果(days1&GT; days0)
     返回更多+ 1;
      其他
      返回更多;
}

My program intends to achieve this

(A) Write a C function named larger() that returns the later date of any two dates passed to it. For example, if the dates 10/9/2001 and 11/3/2001 are passed to larger(), the second date would be returned.

(B) Create the larger() function that was written for (A) in a complete unit. Store the date structure returned by larger() in a separate date structure and display the member values of the returned data structure.

I am working on this problem for my C Language course. I had everything going well (I thought), except that I kept getting "The larger date is: 0/0/0" no matter what I entered. So I started tinkering, and now I cannot get rid of the syntax error, or figure out my 0/0/0 problem. Obviously the dates aren't making it back through. I'm still pretty new at this (and very new at structs), so any help would be awesome!

The line that's giving me the syntax error is near the bottom of main():

DATES result[NUM] = larger(DATES user[NUM]);

The code in full:

#include <stdio.h>
#define NUM 2
struct Dates
{
       int month;
       int day;
       int year;
};
typedef struct Dates DATES;
DATES larger(DATES[NUM]);
DATES more;
int main()
{
    DATES user[NUM];

    printf("You will enter two dates, and the program will return the larger.\n");
    while (user[0].month < 1 || user[0].month > 12)
         {printf("\nPlease enter the first month, 1-12: ");
         scanf("%d", &user[0].month);}
    while (user[0].day < 1 || user[0].day > 31)
          {printf("\nPlease enter the first day, 1-31: ");
          scanf("%d", &user[0].day);}
    while (user[0].year < 1)
          {printf("\nPlease enter the first year: ");
          scanf("%d)", &user[0].year);}
    printf("\nDate entered: %d/%d/%d.\n", user[0].month, user[0].day, user[0].year);

    while (user[1].month < 1 || user[1].month > 12)
         {printf("\nPlease enter the first month, 1-12: ");
         scanf("%d", &user[1].month);}
    while (user[1].day < 1 || user[1].day > 31)
          {printf("\nPlease enter the first day, 1-31: ");
          scanf("%d", &user[1].day);}
    while (user[1].year < 1)
          {printf("\nPlease enter the first year: ");
          scanf("%d)", &user[1].year);}
    printf("\nDate entered: %d/%d/%d.\n\n", user[1].month, user[1].day, user[1].year);

    DATES result[NUM] = larger(DATES user[NUM]);  /* Problem here */

    printf("The larger date is %d/%d/%d.\n\n", result[0].month, result[0].day, result[0].year);
    system("pause");
    return 0;
}

DATES larger(DATES more[NUM])
{        
      int days0;
      int days1;

      days0 = (more[0].month*31)+(more[0].day)+(more[0].year*365);
      days1 = (more[1].month*31)+(more[1].day)+(more[1].year*365);

      if (days1 > days0)
         {more[0] = more[1];}

return (more[0]);
}

解决方案

There are many logical and syntactical errors in your program.I corrected all the syntactical part and must point out the major flaw in the logic of the while loops.You seem to have confused > and < in the following conditions:

while (user[0].month < 1 || user[0].month > 12);  //Wrong

while (user[0].month > 1 || user[0].month < 12);  //Correct

Months are less than 1 or greater than 12 only in Mars,but on earth,due to pollution,they are between 1 and 12.I have corrected those and pointed that out in comments.And you should use a for loop instead of repeating the same thing for each element of the array DATES

AAAAAGHHH It was so full of errors.Here's the working version.It achieves the 2 objectives that you mentioned in your question-Ask user for 2 dates,and find which one is bigger/later.I am not including BC, only AD years.Won't matter unless you want to know if some neanderthal man was born earlier or later than any of us.And the function larger returns a pointer to the answer,which is stored in a new datastructure largerdate and printed out.

Here's a brief explanation for the revised code:

The outer for loop automates the input for each date.There's no need to repeat the same code for each date as you did.If the number of dates goes up,it will be tedious.Further,the do-while loop asks the user for the date in the range specified.If that jerk makes an error,he is yelled at and the condition of that loop makes sure the jerk has to enter again.The two dates are stored in an array user[] and the base address of type DATE* is passed as an argument to the function larger() which compares the two dates and returns a pointer of type DATE* to the larger date.This is used to store the greater date in a new datastructure called largerdate and then printed out.

#include <stdio.h>
#define NUM 2
struct Dates
{
       int month;
       int day;
       int year;
};
typedef struct Dates DATES;
DATES  *larger(DATES*);
//DATES more;     //Not needed as you are doing the same in function definition

int main(void)
{
    DATES user[NUM],largerdate;
    int i;
     printf("You will enter two dates, and the program will return the larger.\n");
    for(i=0;i<NUM;i++)   //for loop is handy when NUM increases.
       {
           do
           {
        printf("\nPlease enter the month number %d, 1-12:\n ",i);
         scanf("%d", &user[i].month);
         if(user[i].month<1||user[i].month>12)
         printf("I told you enter a number between 1-12 JERK!!\n");
           }while(user[i].month<1||user[i].month>12);
          do
           {
        printf("\nPlease enter the day number %d, 1-31:\n ",i);
         scanf("%d", &user[i].day);
         if(user[i].day<1||user[i].day>31)
         printf("I told you enter a number between 1-31 JERK!!\n");
           }while(user[i].day<1||user[i].day>31);
           do
           {
        printf("\nPlease enter the year number %d,greater than one:\n ",i);
         scanf("%d", &user[i].year);
         if(user[i].year<1)
         printf("I told you enter a number greater than 1 JERK!!\n");
           }while(user[i].year<1);
    printf("\nDate number %d entered is: %d/%d/%d.\n", i+1,user[i].month, user[i].day, user[i].year);

   } //for loop ends.It avoids repeating the same thing for DATES[1]


   largerdate=*larger(user);
   printf("\n\nThe larger/later date is %d/%d/%d\n",largerdate.month,largerdate.day,\
   largerdate.year);


    system("pause");
    return 0;
}

DATES *larger(DATES *more)
{
      int days0;
      int days1;

      days0 = (more[0].month*31)+(more[0].day)+(more[0].year*365);
      days1 = (more[1].month*31)+(more[1].day)+(more[1].year*365);

      if (days1 > days0)
     return more+1;
      else
      return more;
}

这篇关于什么是错误的原因,而在这个C程序返回一个结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆