C程序结构分配无效八达数字



程序规格:

设计并实施一个将创建和使用三个的程序 类型人的不同变量。

使用typedef命令创建一个日期的结构。

为具有以下字段的人创建一个结构。

名称[这将是一个字符串]出生日期[这将是日期]性别 [这将是一个char]年incmome [这要么是浮子或 双,您的选择]创建三个类型人的变量。创建一个 填充每个人的数据的功能(全部3个)。 创建一个功能,以输出有关每个人的所有数据 良好的格式。

数据验证:

必须验证输入的所有日期。确保您说明 每个月的天数,正好有12个 几个月,每四年有一个leap年。

每个人的名称将存储在句子案例中。

性别将为m,f或o。

年收入在0美元至100万美元之间。

   /*Programmer: John B.*/
/*Date: 2/26/19*/
/*Program to demonstrate structs for a date and person with specified fields.*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH nothingFlush()
void nothingFlush() {
    char nothing;
    while (scanf("%c", &nothing) == NULL);
}
typedef struct {
    int month;
    int day;
    int year;
} DATE;
typedef struct {
    char name[100];
    char lastName[100];
    DATE dob;
    char gender;
    float anualIncome;
} PERSON;
//Prototype Functions
void displayDate(DATE birthday);
void displayWorkers(PERSON wOne, PERSON wTwo, PERSON wThree);
DATE getDate();
float getAnualIncome(PERSON *wOne, PERSON *wTwo, PERSON *wThree);
void getThings(PERSON *wOne, PERSON *wTwo, PERSON *wThree);
//Main
main() {
    DATE birthday;
    PERSON workerOne, workerTwo, workerThree;
    getThings(&workerOne, &workerTwo, &workerThree);
    displayWorkers(workerOne, workerTwo, workerThree);
    PAUSE;
}//End Main
//Write Functions
void displayDate(DATE birthday) {
    printf("nt%i/%i/%in", birthday.month, birthday.day, birthday.year);
}//End Display Date
void displayWorkers(PERSON wOne, PERSON wTwo, PERSON wThree) {
    CLS;
    strcpy(wOne.name, "Austin");
    strcpy(wOne.lastName, "Warner");
    strcpy(wTwo.name, "Lee");
    strcpy(wTwo.lastName, "Cousins");
    strcpy(wThree.name, "McKinley");
    strcpy(wThree.lastName, "Alitolof");
    printf("ntFirst: %sn", wOne.name);
    printf("ntLast: %sn", wOne.lastName);
    displayDate(wOne.dob);
    printf("ntGender: %cn", wOne.gender);
    printf("ntAnual Income: %.2fn", wOne.anualIncome);
    printf("ntFirst: %sn", wTwo.name);
    printf("ntLast: %sn", wTwo.lastName);
    displayDate(wTwo.dob);
    printf("ntGender: %cn", wTwo.gender);
    printf("ntAnual Income: %.2fn", wTwo.anualIncome);
    printf("ntFirst: %sn", wThree.name);
    printf("ntLast: %sn", wThree.lastName);
    displayDate(wThree.dob);
    printf("ntGender: %cn", wThree.gender);
    printf("ntAnual Income: %.2fn", wThree.anualIncome);
}//End Display Workers
float getAnualIncome(PERSON *wOne, PERSON *wTwo, PERSON *wThree) {
    float result;
    do {
        printf("ntEnter The Anual Income Of The Worker: ");
        scanf_s("%f", &result); FLUSH;
        if (result < 0 || result > 1000000)
            printf("Invalid Number--- Try Againn");
    } while (result < 0 || result > 1000000);
    return result;
}
DATE getDate() {
    DATE result;
    do {
        printf("ntEnter Year: ");
        scanf_s("%i", &result.year); FLUSH;
        if (result.year < 1900 || result.year > 5000)
            printf("tInvalid Number--- Try Againn");
    } while (result.year < 1900 || result.year > 5000);
    do {
        printf("ntEnter Month: *****Please enter single digit months as a single digit***** ");
        scanf_s("%i", &result.month); FLUSH;
        if (result.month < 0 || result.month > 12)
            printf("tInvalid Number--- Try Againn");
    } while (result.month < 0 || result.month > 12);
    do {
        printf("ntEnter Day: ");
        scanf_s("%i", &result.day); FLUSH;
        if (result.day < 1 || result.day > 31) {
                printf("tInvalid Number--- Try Againn");
        }
        if (result.month == 2 || result.month == 02) { //Check for leap year
            if (((result.year % 4 == 0) && result.year % 100 != 0) ||
                (result.year % 400 == 0)) {
                while (result.day < 1 || result.day > 29) { //Leap year feb dates 1-29
                    printf("tLeap Year--- Try Againn");
                    printf("ntEnter Day: ");
                    scanf_s("%i", &result.day); FLUSH;
                }
            }
            else {
                while (result.day < 1 || result.day > 28) { //Leap year feb dates 1-29
                    printf("tInvalid Number--- Try Againn");
                    printf("ntEnter Day: ");
                    scanf_s("%i", &result.day); FLUSH;
                }
            }
        }
        if (result.month == 4 || result.month == 04 || result.month == 6 || // Check if month has only 30 days
            result.month == 06 || result.month == 9 || result.month == 09 || result.month == 11) { //Invalid Octal Digit??
            while (result.day < 1 || result.day > 30) {
                printf("tInvalid Day--- Try Againn");
                printf("ntEnter Day: ");
                scanf_s("%i", &result.day); FLUSH;
            }
        }
    } while (result.day < 1 || result.day > 31);
    return result;
}//End Get Date
char getGender(PERSON *wOne, PERSON *wTwo, PERSON *wThree) {
    char result;
    do {
        printf("ntEnter The Gender For The Worker: ");
        scanf_s("%c", &result); FLUSH;
        if (result != 'F' && result != 'f' && result != 'M' && result != 'm' && 
            result != 'O' && result != 'o')
            printf("ntERROR-- Try Again...n");
    } while (result != 'F' && result != 'f' && result != 'M' && result != 'm' &&
        result != 'O' && result != 'o');
    return result;
}//End Get Gender
void getThings(PERSON *wOne, PERSON *wTwo, PERSON *wThree) {
    CLS;
    printf("ntEnter The Date Of Birth Of Austin: n");
    wOne->dob = getDate();
    wOne->gender = getGender(&wOne, &wTwo, &wThree);
    wOne->anualIncome = getAnualIncome(&wOne, &wTwo, &wThree);
    printf("ntEnter The Date Of Birth Of Lee: n");
    wTwo->dob = getDate();
    wTwo->gender = getGender(&wOne, &wTwo, &wThree);
    wTwo->anualIncome = getAnualIncome(&wOne, &wTwo, &wThree);
    printf("ntEnter The Date Of Birth Of McKinley: n");
    wThree->dob = getDate();
    wThree->gender = getGender(&wOne, &wTwo, &wThree);
    wThree->anualIncome = getAnualIncome(&wOne, &wTwo, &wThree);
}//End Get Things

因此,我的程序运行非常稳定,但是由于结果,我确实遇到了一个错误,因为结果== 09(在其中检查一个月仅30天,第139行)。我将单位数月包括在9和09的情况下,以防人们以这种方式将它们放置。直到我超过7之前,它才成为问题,它不会阻止该数字进入,并且允许仅30岁的天数为31。希望这足以让某人理解,我试图提供尽可能多的信息。感谢他们的帮助!

result.month == 09中的 0开始一个八进制整数常数但其余的( 9)并不是八分之一。因此编码错误。改用result.month == 9

scanf_s("%i",...更改为scanf_s("%d",...以读取输入小数。

可能存在其他问题。

最新更新