新手,求解一C语言问题

Writetheprogramclassifywhichshouldasktheuserfortheirgenderandage,andthenclassifythatp... Write the program classify which should ask the user for their gender and age, and then classify that person as either boy, man, girl, or woman. A person under 18 years of age is boy or girl, otherwise man or woman, depending on their gender. Your program should have a function get_gender_and_age which should read the gender and age from the user. The function should perform input validation: a valid gender can only be m, M, f or F. A valid age should be at least 0, and an age over 150 is considered to be a wrong input. When any invalid input is received, the function should repeat to get input until input is valid. The function should return the gender and age values to the main function through output parameters. After this function returns, the main function should print a message to classify the person.

Examples:

1. Valid input:

Enter gender (m/f): m
Enter age: 21
You are a man

2. Repeating input when invalid input is received:

Enter gender (m/f): h
ERROR: Wrong gender code!
Enter gender (m/f): m
Enter age: 210
ERROR: Invalid age!
Enter age: sjkd
ERROR: Wrong input, not a number!
Enter age: 21
You are a man

对这个 问题有几个不明白的地方:1.如何在输入非数字的年龄后输出错误信息(ERROR: Wrong input, not a number!);2.如何控制重复输入部分(
Enter gender (m/f): h
ERROR: Wrong gender code!
Enter gender (m/f): m )

因为自己写的程序在测试中一直出错,希望能够得到完整的程序源码,请各位不吝赐教。
The purpose of this exercise set is to practice:

* writing C programs with functions that have output parameters

目前只学了判断语句,重复结构,指针,函数等内容,所以很复杂的话就读不懂了……见谅
展开
 我来答
sai_84
2010-10-22 · TA获得超过531个赞
知道小有建树答主
回答量:264
采纳率:100%
帮助的人:412万
展开全部
判断非数字的年龄可以通过先输入字符串,然后判断是否每一个字符都在'0'到'9’之间。
重复输入通过一个while循环,OK就break, ERROR就continue.

如下:
#include <stdio.h>
#include <string.h>

#define BOY 0
#define GIRL 1
#define MAN 2
#define WOMAN 3

char szGender[] = "mMfF";
char* szResult[] = {"boy", "girl", "man", "woman"};

int get_gender_and_age()
{
char gender;
int age, i;
char szTemp[256];

// Get gender
while (1)
{
printf("Enter gender (m/f):");
scanf("%s", szTemp);

// Verify gender
if ((strlen(szTemp) > 1) || !strchr(szGender, szTemp[0]))
{
// Retry
printf("ERROR: Wrong gender code!\n");
continue;
}
// OK
gender = szTemp[0];
break;
}

// Get age
while (1)
{
printf("Enter age:");
scanf("%s", szTemp);

// Verify age
// Check input is number
for (i = 0; i < strlen(szTemp); i++)
if (szTemp[i] < '0' || szTemp[i] > '9')
break;
if (i < strlen(szTemp))
{
printf("ERROR:Wrong input, not a number!\n");
continue;
}

// Convert string to number
age = atoi(szTemp);

if (age < 0 || age > 150)
{
// Retry
printf("ERROR: Invalid age!\n");
continue;
}
break;
}

if (age < 18)
return (gender == 'm' || gender == 'M') ? BOY : GIRL;
else
return (gender == 'm' || gender == 'M') ? MAN : WOMAN;
}

void main()
{
int result = get_gender_and_age();
printf("You are a %s\n", szResult[result]);
}
百度网友a3722676e
2010-10-22 · TA获得超过1215个赞
知道小有建树答主
回答量:957
采纳率:0%
帮助的人:853万
展开全部
#include <iostream>
using namespace std;

#define boy 1
#define girl 2
#define man 3
#define woman 4

class human
{
public:
int get_gender_and_age(char, int);
};
int human::get_gender_and_age(char chG, int nA)
{
if (chG == 'm' && nA < 18)
return boy;
else if(chG == 'm' && nA >= 18)
return man;
else if(chG == 'f' && nA < 18)
return girl;
else if(chG == 'f' && nA >= 18)
return woman;
}
bool show(char szArr[])
{
int iRow = 0;
int nArrLen = strlen(szArr);
char a ='0', b = '9';
for (; iRow < nArrLen; ++iRow)
{
if (szArr[iRow] < a || szArr[iRow] > b)
return true;
}
return false;
}
int main(void)
{
char chGender;
char szAge[10] = {0};
int nAge, iRet;
human h;

while(1)
{
InputGender:
cout<<"Enter gender (m/f):";
cin>>chGender;
if (chGender != 'm' && chGender != 'f')
{
cout<<" ERROR: Wrong gender code!"<<endl;
goto InputGender;
}
InputAge:
cout<<"Enter age:";
cin>>szAge;
if (show(szAge))
{
cout<<"ERROR: Wrong input, not a number!"<<endl;
goto InputAge;
}
nAge = atoi(szAge);
if (nAge >= 150 || nAge < 0)
{
cout<<" ERROR: Invalid age!"<<endl;
goto InputAge;
}
iRet = h.get_gender_and_age(chGender, nAge);
if (iRet == boy)
cout<<"You are boy"<<endl;
else if(iRet == girl)
cout<<"You are girl"<<endl;
else if(iRet == man)
cout<<"You are man"<<endl;
else if(iRet == woman)
cout<<"You are woman"<<endl;
}
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式