对条件变量使用Char
Using Char for Conditional Variables
我需要帮助添加到一个对年龄进行分类的程序中,以确定个人是否有资格获得人寿保险。规则是添加到以前的程序中,允许用户输入年龄,然后输出他们是未成年儿童、未成年青少年、成年人还是老年人。需要使用字符yes或no来决定是否符合条件。这个规则的增强也将决定资格。以下是迄今为止的规则和代码。
符合年龄17岁或以下18-90岁大于90'n'<------新的增强规则代码:
//12 or under minor child
//13 - 17 minor teenager
//18 - 64 adult
//65 or over senior adult
#include <iostream>
using namespace std;
int main()
{
const int MAX_CHILD = 12, MAX_TEEN = 17, MAX_ADULT = 64; MAX_ADULT_ALLOWED = 90;
int age;
cout << "How old are you?";
cin >> age;
// additional code goes here
if (age <= MAX_CHILD)
cout << "You're a minor child.n";
else if (age <= MAX_TEEN)
cout << "You're a minor teenager.n";
else if (age <= MAX_ADULT)
cout << "You're an adult.n";
else if
cout << "You're a senior adult.n";
else (age <= MAX_ADULT_ALLOWED)
//if (eligible == "y")
//cout << "You are eligible for life insurance package.n";
return 0;
}
我希望这能回答你的问题:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//12 or under minor child
//13 - 17 minor teenager
//18 - 64 adult
//65 or over senior adult
int main()
{
const int MAX_CHILD = 12, MAX_TEEN = 17, MAX_ADULT = 64, MAX_ADULT_ALLOWED = 90;
int age;
char eligible ;
cout << "How old are you?";
cin >> age;
// additional code goes here
if (age <= MAX_CHILD)
{
cout << "You're a minor child.n";
eligible = 'n';
}
else if (age <= MAX_TEEN)
{
cout << "You're a minor teenager.n";
eligible = 'n';
}
else if (age <= MAX_ADULT)
{
cout << "You're an adult.n";
eligible = 'y';
}
else if (age <= MAX_ADULT_ALLOWED)
{
cout << "You're a senior adult.n";
eligible = 'y';
}
else
{
cout << "You're a senior adult.n";
eligible = 'n';
}
if (eligible == 'y')
cout << "You are eligible for life insurance package.n";
else
cout << "You are not eligible for life insurance package.n";
std::system("PAUSE");
return 0;
}
您可以将打印语句cout << "You are eligible for life insurance package.n";
放入满足条件的if
块中,不需要额外的变量即可获得资格。
if (age <= MAX_CHILD){
cout << "You're a minor child.n";
cout << "You are not eligible for life insurance package.n";
//eligible = 'n';
}
else if (age <= MAX_TEEN){
cout << "You're a minor teenager.n";
cout << "You are not eligible for life insurance package.n";
//eligible = 'n';
}
else if (age <= MAX_ADULT){
cout << "You're an adult.n";
cout << "You are eligible for life insurance package.n";
//eligible = 'y';
}
else if(age <= MAX_ADULT_ALLOWED){
cout << "You're a senior adult.n";
cout << "You are eligible for life insurance package.n";
//eligible = 'y';
}
else {
cout << "You are not eligible for life insurance package.n";
//eligible = 'n';
}
或者,如果您想使用一个单独的变量来获得资格,那么只需在满足条件的块内为其分配值y,否则将其分配n(我在代码中对这些行进行了注释),最后检查资格。
相关文章:
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 如何更好地检查两个 char 变量是否在一组值中?
- 将双精度值转换为 char 变量时字符串流如何工作
- C++ 如何检查 char 变量是否未定义(未初始化)
- 在 c++ 中将 Char 数组添加到 Char * 变量
- char指针或char变量的默认值是多少
- 当对字符变量使用toupper()时,所述char变量输出多个字符.我该如何防止这种情况发生
- 在C++中,从构造函数中将字符串文本分配给成员const char*变量时会发生什么
- 程序的执行在 C++ 中输入 char 变量后结束
- 如何创建一个 char *[] 变量,以便将其传递给函数
- 将 2 位整数分配给 char 变量
- 将值设置为 char ** 变量 c++ 时出错
- 如果我们在 C++ 中声明一个起始'&'的 char 变量会发生什么?
- 在此范围C 中未声明字符串和char变量
- C++:如何将 char 变量分配给向量<char>并访问该向量的元素进行比较?
- 仅将输入的第一个字符用于 char 变量
- 在C 中使用两个CHAR变量评估布尔值
- 尚未声明char变量
- (C++)如何验证 char 变量的用户输入
- 为什么比较两次 char 变量比比较一次短变量更快