C程序结构分配无效的八进制数字

问题描述 投票:0回答:1

程序规格:

设计和实现一个程序,它将创建并使用PERSON类型的三个不同变量。

使用typedef命令为DATE创建结构。

使用以下字段为PERSON创建结构。

name [这将是一个字符串] birthdate [这将是一个DATE]性别[这将是一个char] annualIncome [这将是float或double,你的选择]创建三个PERSON类型的变量。创建一个为每个人(所有3个人)填充数据的函数。创建一个函数,以一种很好的格式化方式输出关于每个人的所有数据。

数据验证:

输入的所有日期必须经过验证。确保记录每个月的天数,事实上正好有12个月,每四年有一个闰年。

每个PERSON的名称将以句子形式存储。

性别将是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("\n\t%i/%i/%i\n", 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("\n\tFirst: %s\n", wOne.name);
    printf("\n\tLast: %s\n", wOne.lastName);
    displayDate(wOne.dob);
    printf("\n\tGender: %c\n", wOne.gender);
    printf("\n\tAnual Income: %.2f\n", wOne.anualIncome);

    printf("\n\tFirst: %s\n", wTwo.name);
    printf("\n\tLast: %s\n", wTwo.lastName);
    displayDate(wTwo.dob);
    printf("\n\tGender: %c\n", wTwo.gender);
    printf("\n\tAnual Income: %.2f\n", wTwo.anualIncome);

    printf("\n\tFirst: %s\n", wThree.name);
    printf("\n\tLast: %s\n", wThree.lastName);
    displayDate(wThree.dob);
    printf("\n\tGender: %c\n", wThree.gender);
    printf("\n\tAnual Income: %.2f\n", wThree.anualIncome);

}//End Display Workers

float getAnualIncome(PERSON *wOne, PERSON *wTwo, PERSON *wThree) {
    float result;

    do {
        printf("\n\tEnter The Anual Income Of The Worker: ");
        scanf_s("%f", &result); FLUSH;
        if (result < 0 || result > 1000000)
            printf("Invalid Number--- Try Again\n");
    } while (result < 0 || result > 1000000);

    return result;
}

DATE getDate() {
    DATE result;

    do {
        printf("\n\tEnter Year: ");
        scanf_s("%i", &result.year); FLUSH;
        if (result.year < 1900 || result.year > 5000)
            printf("\tInvalid Number--- Try Again\n");
    } while (result.year < 1900 || result.year > 5000);

    do {
        printf("\n\tEnter 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 Again\n");
    } while (result.month < 0 || result.month > 12);

    do {
        printf("\n\tEnter Day: ");
        scanf_s("%i", &result.day); FLUSH;
        if (result.day < 1 || result.day > 31) {
                printf("\tInvalid Number--- Try Again\n");
        }
        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 Again\n");
                    printf("\n\tEnter 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 Again\n");
                    printf("\n\tEnter 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 Again\n");
                printf("\n\tEnter 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("\n\tEnter The Gender For The Worker: ");
        scanf_s("%c", &result); FLUSH;
        if (result != 'F' && result != 'f' && result != 'M' && result != 'm' && 
            result != 'O' && result != 'o')
            printf("\n\tERROR-- 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("\n\tEnter The Date Of Birth Of Austin: \n");
    wOne->dob = getDate();
    wOne->gender = getGender(&wOne, &wTwo, &wThree);
    wOne->anualIncome = getAnualIncome(&wOne, &wTwo, &wThree);

    printf("\n\tEnter The Date Of Birth Of Lee: \n");
    wTwo->dob = getDate();
    wTwo->gender = getGender(&wOne, &wTwo, &wThree);
    wTwo->anualIncome = getAnualIncome(&wOne, &wTwo, &wThree);

    printf("\n\tEnter 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

所以我的程序运行得非常可靠,但是因为result.month == 09(它检查一个月只有30天,第139行),我收到一个错误,说无效的八进制数字。我把9和09的单个数字包括在内,以防人们以这种方式进行。直到我达到7以上它才成为一个问题。如何解决此问题以允许将月份输入为9或09.当有人输入09时,result.month的值为0或一些奇怪的数字,它不会阻止输入数字,并且允许09的日期为31,当它应该只有30时。希望这对于某人理解是足够清楚的,并且我试图提供尽可能多的信息。谢谢他们的帮助!

c date struct digits octal
1个回答
0
投票

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

scanf_s("%i",...更改为scanf_s("%d",...以仅读取十进制输入。

可能存在其他问题。

© www.soinside.com 2019 - 2024. All rights reserved.