2017-09-25 60 views
0

好日子问题正确执行

我完成分配的问题,并以书面形式,我无法弄清楚如何纠正代码所遇到的问题。

问:

IN-surance是SA公司,为A级车的保险。您需要编写一个名为question5.cpp 的程序来帮助公司计算其客户的每月保费。客户应支付标准费用 R850.00每月。除了标准收费,费用如下应用:

经验:经验丰富的司机支付低于经验丰富的司机大致如下: Ø0 - 2年内到位R150.00额外 O 3 - - 5年薪酬R80 .00额外 o 6年及更多年仅支付R30额外费用。 年龄:年龄较老的公民支付的费用如下: o 18 - 28:支付R50.00额外费用。 o 29 - 45:支付R30.00额外费用。 o 46 - 60:支付R20.00额外费用。 o 61及以上支付R30.00额外费用。

性别:男性客户支付额外的R10.00。

婚姻状况:单身或离婚(可用)的人与他们的伴侣(不可用)结婚或生活 支付更多。 o可用支付R40.00额外。

编写以下功能: driversGroup:此功能以其参数驾驶体验为参数,并返回客户必须支付的额外费用。

ageGroup:此函数以驱动程序的年龄作为参数,并返回客户必须支付的额外费用。年龄不应低于18岁。

isMale:该函数以驱动程序的性别为参数,如果是男性,则返回真值。功能 只能接受男性为'M'或'm',女性为'F'或'f'。

•isAvailable:该函数以驱动程序的婚姻状态为参数,如果客户为单一或离婚,则返回真值。该功能应该只接受'S'或's'为单身,'D'或'd'为离婚,'M'或'm'为已婚夫妻,'L'或'l'为同伴伴侣。

computePremium:该函数以经验,年龄,性别和婚姻状况为参数,并返回支付的总保费。这个函数应该使用你上面写的函数。

添加main()函数以演示如何使用这些函数。

我的代码:

#include <iostream> 
#include <cmath> 
#include <string> 

using namespace std; 

// Takes driving exp and returns the additional fee 
int driversgroup(int experience) 
{ 
    if (experience > 0 && experience < 2) 
    { 
     return 150;} 
    else if (experience > 3 && experience < 5) 
    { 
     return 80;} 
    else if(experience > 6){ 
     return 30;} 
} 
// Takes the driving age and returns the additional fee must not be <18 
int ageGroup(int age) 
{ 
    if (age >= 61){ 
    return 30;} 
    else if (age >= 46){ 
    return 20;} 
    else if (age >= 29){ 
    return 30;} 
    else if (age >= 18){ 
    return 50;} 

} 
//Takes the gender of driver and test for male and return additnal fee 
bool isMale(char gender) 
{ 
if (gender =='M' || gender== 'm') 
{ 
    return 10; 
} 
else 
{ 
    return 0; 
} 
} 
//Takes the marital status of driver and returns addtional fee 
int isAvailable(int marital) 
{ 
if (marital == 'S' || 's' || 'D' || 'd'){ 
return 40; } 
} 
// Computes Total premium paid 
int computePremium(int age, int experience, int gender, int marital) 
{ 

    int Totalpremium = driversgroup(experience) + ageGroup(age) + 
    isMale(gender) + isAvailable(marital); 

    cout << "Your Total premium is R" << Totalpremium << endl; 
} 


int main() 
{ 
    int age; 
    int experience; 
    char gender; 
    char marital; 


cout << "What is your age?" << endl; 
cin >> age; 
cout << "How long have you been driving? (in years)" << endl; 
cin >> experience; 
cout << "Are you male or female? (M or F)" << endl; 
cin >> gender; 
cout << "Are you married?(S for single, D for divorce, M for married or L 
for living with partner)" << endl; 
cin >> marital; 
if(marital != 'm' || 'M' || 'd' || 'D' || 's' || 'S' || 'l' || 'L'){ 
cout << "Try again, invalid entry" << endl; 
cin >> marital;} 

computePremium(age,experience,gender,marital); 

} 

问题我遇到:

1)当程序运行时,它问我是否输入小写字母,它返回我把在婚姻状况“再试一次。 , 输入无效”。我如何正确地说出婚姻状况,以便让我输入小写字母和大写字母?

2)看来我的价值没有被正确计算。任何想法,我的错误可能是什么?

谢谢

+8

'如果(婚姻== 'S' || 'S' || 'd' || 'd'){'不做你认为它的事情(提示:这将总是评估为“真”)。另外正确的做法是(学会)调试你的代码(使用调试器)。进一步你的'computePremium'函数调用*未定义的行为*(缺少'return'语句)。你应该先阅读[一本好的入门书](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – UnholySheep

+1

并使用'-Wall'编译器标志和' -Werror'。 – Yakk

+1

比较之前,您可以使用'std :: toupper'或'std :: tolower'将'婚姻'转换为小写或大写。 –

回答

0

我回答你的问题,因为不像其他许多SO问题,你尝试过的东西:

  1. 你的问题是在这里:if(marital != 'm' || 'M' || 'd' || 'D' || 's' || 'S' || 'l' || 'L') 这个结果始终为true。您必须针对每种可能的解决方案检查优胜状态: (if marital != 'S' && marital != 's' ...)isAvailable(int marital)函数的同样问题。此外,正如托马斯马修斯所说,您可以使用std::toupperstd::tolower来减少您执行的检查次数。

  2. 你的一个问题是在这里:bool isMale(char gender):如果表达式评估为真,则返回1 10.您应该更改功能int isMale(char gender)。另一个问题是isAvailable函数:如果marital不是'S','s','D''d'该函数将不返回有用的值。

此外,采取UnholySheep建议和(学习)调试你的代码,并阅读一本好初学者的书。

我建议思考和测试各种情况,以解决您的应用程序中的逻辑问题。示例:如果提供的age/experience是负数,会发生什么情况?这是正确的行为吗?

0

尝试,而不是bool isMalechar型),int isMaleint型) 这应该与你的计算解决问题。 也记得Const Int,它等于850,因为这是统一费率,所以需要将其作为计算的一部分。

尝试:

if (experience >= 0 && experience <= 2) 
{ 
    return 150; 
} 
else if and so on 

int ageGroup(int age) 

else if (age >= 46 && age <= 60) 


if (gender == 'M') 
    cout << "Are you married?" 
cin >> marital; 

if (marital == 'M' && marital == 'm' && marital == 'L' && marital == 'l') 
    cout << "No Extra charge" << endl; 

希望这有助于有点

+0

非常感谢。使用了我获得的所有帮助,并设法让我的程序完美执行 – Geo