gpt4 book ai didi

c - inputSex() 和 inputAge() 的返回值没有返回任何内容?

转载 作者:行者123 更新时间:2023-11-30 14:42:29 25 4
gpt4 key购买 nike

我试图将 int 或 char 输入到函数中并返回该值,但它们都没有返回任何内容。我尝试将 print 语句放入两个函数中,它们都打印出一些内容,但该函数不会返回任何内容。任何信息都会有帮助。提前致谢!

#include "defs.h"
#define MAX_CHAR 32

int nameInput(char firstName[MAX_CHAR + 1], char middleInt[2], char
lastName[MAX_CHAR + 1], char suffix[4]);
int printName(char firstName[MAX_CHAR + 1], char middleInt[2], char
lastName[MAX_CHAR + 1], char suffix[4]);
void emptyStdin(void);
int inputAge(int min, int max);
char inputSex(void);
int printAge(int age);
char printSex(char sex);

int main (void)
{
int choice = 0, age = 0;
char firstName[MAX_CHAR + 1] = {0}, middleInt[2] = {0},
lastName[MAX_CHAR + 1] = {0}, suffix[4] = {0};
char sex = '\0';

do
{
printf("Menu\n");
printf("1. Name\n");
printf("2. Enter Years, Party, Office, and State \n");
printf("3. Enter Age and Sex\n");
printf("4. Enter Contacts \n");
printf("5. Enter Contributions and Lies \n");
printf("6. Display Data \n");
printf("7. Clear all Data \n");
printf("8. Quit\n");
scanf("%d", &choice);

switch (choice)
{
case 1:
emptyStdin();
nameInput(firstName, middleInt, lastName, suffix);
break;
case 2:
break;
case 3:
emptyStdin();
inputAge(21,99);
emptyStdin();
inputSex();
break;
case 4:
break;
case 5:
break;
case 6:
printName(firstName, middleInt, lastName, suffix);
printAge(age);
printSex(sex);
break;
case 7:
break;
case 8:
choice = 8;
break;
default:
printf("Input is invalid\n");
break;
}
} while(choice != 8);

return (0);
}

int nameInput(char firstName[MAX_CHAR + 1], char middleInt[2], char
lastName[MAX_CHAR + 1], char suffix[4])
{
char temp[2] = {0};
int c = 15;
int i = 0;

printf("Type in the politicians first name (max 32 characters): ");
scanf("%s", firstName);
emptyStdin();

printf("Type in the politicians middle initial. If none type 0. (max 1 character): ");
scanf("%s", temp);
c = temp[i];
c = temp[i];

if(c == 0)
{
middleInt[i] = '\0';
}
if(c != 0)
{
strcpy(middleInt, temp);
}
else;
emptyStdin();

printf("Type in the politicians last name (max 32 characters): ");
scanf("%s", lastName);
emptyStdin();

printf("Type in the politicians suffix. If none type 0. (max 3 characters): ");
scanf("%s", suffix);
emptyStdin();
printf("\n\n");
return 1;
}

int printName(char firstName[MAX_CHAR + 1], char middleInt[2], char
lastName[MAX_CHAR + 1], char suffix[4])
{
int i = 0;

printf("*****Unknown/Not Applicable if the output is a 0*****\n\n");
printf("- Politicians first name: ");
for (i = 0; i != ('\n' || '\0'); i++)
{
printf("%s", &firstName[i]);
}

printf("\n");
i = 0;
printf("- Politicians middle initial: ");
if (middleInt[i] != '\0')
{
for (i = 0; i != ('\n' || '\0'); i++)
{
printf("%s", &middleInt[i]);
}
printf("\n");
}
else;

i = 0;
printf("- Politicians last name: ");
for (i = 0; i != ('\n' || '\0'); i++)
{
printf("%s", &lastName[i]);
}
printf("\n");

i = 0;
printf("- Politicians suffix: ");
if (suffix[i] != 0)
{
for (i = 0; i != ('\n' || '\0'); i++)
{
printf("%s", &suffix[i]);
}
}
else;

printf("\n");

return 1;
}

void emptyStdin(void)
{
int c = getchar();

while (c != EOF && c != '\n')
{
{
c = getchar();
}
}

int inputAge(int min, int max)
{
int age = 0, i = 0;
printf("Enter the age of the politician [Between 21 and 99]: ");

i = 1;
for(i = 1; i < 2; i++)
{
scanf("%d", &age);
if (age < min || age > max)
{
printf("That number isnt in the range. Please try again. \n");
i--;
}
else
{
break;
}
}

return age;
}

char inputSex(void)
{
int i = 0;
char sex;

i = 1;
printf("Enter the sex of the politician [Choose either M or F]: ");
for(i = 1; i < 2; i++)
{
scanf("%c", &sex);
if(sex == 'M' || sex == 'F')
{
{
break;
}
else
{
printf("That is not a vaild input. Please try again.");
i--;
}
}
return sex;
}

int printAge(int age)
{
printf("- Age: %d\n", age);
return 1;
}

char printSex(char sex)
{
printf("- Sex: %d\n", sex);
return 1;
}

最佳答案

这是您调用inputAge的地方...

inputAge(21,99);

...注意到缺少什么了吗?您没有将返回值存储在任何地方。如果您希望它将结果存储到 age 中(基于对下面几行的 printAge 的调用),您需要将该行更改为此。

age = inputAge(21,99);

inputSex 的调用也是如此。

关于c - inputSex() 和 inputAge() 的返回值没有返回任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54426247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com