我有一个代码,它无法将Bonus读作变量。
这是我的代码:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char Bonus,Salary,TotalSalary;
int num1, num2;
cout <<"Enter the hours of work for week"<<endl;
cin >> num1;
cout <<"Enter your hourly rate"<<endl;
cin >>num2;
Salary = num1 * num2;
if (Bonus >= 45)
cout <<"Your bonus is 500 pesos"<<endl;
else if (Bonus>40&&Bonus<=45)
cout <<"Your bonus is 250 pesos"<<endl;
else if (Bonus>45&&Bonus<=40)
cout <<"Your bonus is 150 pesos"<<endl;
TotalSalary=Salary + Bonus;
cout <<"Your basic salary is" <<Salary <<"with a bonus of" <<Bonus <<"and a total of"<<TotalSalary<<endl;
system("pause");
return 0;
}
我希望你能提前帮助我解决我的问题。
答案 0 :(得分:1)
因为您的Bonus
和Salary
以及TotalSalary
变量是char
。因此,您必须将它们声明为int
变量。 Bonus
未经初始化即可使用0
调用https://en.wikipedia.org/wiki/Undefined_behavior,因此您可以将其初始化为Bonus
。此外,您必须要求用户输入cout <<"Enter the Bonus"<<endl;
cin >> Bonus;
`#include <iostream>
#include <string>
using namespace std;
int main ()
{
int Salary,TotalSalary;
int num1, num2, Bonus = 0;
cout <<"Enter the hours of work for week"<<endl;
cin >> num1;
cout <<"Enter your hourly rate"<<endl;
cin >>num2;
cout <<"Enter the Bonus"<<endl;
cin >> Bonus;
Salary = num1 * num2;
if (Bonus >= 45)
cout <<"Your bonus is 500 pesos"<<endl;
else if (Bonus>40&&Bonus<=45)
cout <<"Your bonus is 250 pesos"<<endl;
else if (Bonus>45&&Bonus<=40)
cout <<"Your bonus is 150 pesos"<<endl;
TotalSalary=Salary + Bonus;
cout <<"Your basic salary is " <<Salary <<" with a bonus of " <<Bonus <<" and a total of "<<TotalSalary<<endl;
system("pause");
return 0;
}
它应该有用。
{{1}}
答案 1 :(得分:0)
代码的一些问题:
Salary = num1 * num2;
Bonus
,Salary
和TotalSalary
定义为char
,而它们也应定义为int
。
Bonus
未经初始化使用,会调用undefined behavior。
编译器应该已经警告过你。确保警告已开启且处于最高级别。
有关if statements
的一些详细信息:
if (Bonus >= 45)
cout << "Your bonus is 500 pesos" << endl;
else if (Bonus>40 && Bonus<=45)
// minor detail, but can't be equal to 45 again
cout <<"Your bonus is 250 pesos" << endl;
else if (Bonus>45 && Bonus<=40)
// mayor detail: can never be >45 *and* <=40 at the same time
cout <<"Your bonus is 150 pesos" << endl;
您可以将其更改为:
// You probably want to check Salary instead of Bonus?
// although the values (like 45) might be off
Salary = num1 * num2;
if (Salary >= 45)
Bonus = 500;
else if (Salary > 40)
Bonus = 250;
else
Bonus = 150;
cout << "Your bonus is " << Bonus << " pesos" << endl;
完整示例:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int num1 = 0,
num2 = 0,
Bonus = 0,
Salary = 0,
TotalSalary = 0;
cout << "Enter the hours of work for week: ";
cin >> num1;
cout << "Enter your hourly rate: ";
cin >> num2;
Salary = num1 * num2;
// values (like 45) might be off here
if (Salary >= 45) Bonus = 500;
else if (Salary > 40) Bonus = 250;
else Bonus = 150;
cout << "Your bonus is " << Bonus << " pesos" << endl;
TotalSalary = Salary + Bonus;
cout << "Your basic salary is " << Salary
<< " with a bonus of " << Bonus
<< " and a total of " << TotalSalary << endl;
system("pause");
return 0;
}
答案 2 :(得分:0)
HY!
定义<?php
namespace Easytrip2\Form\Type;
use Easytrip2\Form\AbstractEasytrip2Type;
use Easytrip2\Form\Select\CountrySelectType;
use Easytrip2\Form\Select\GeopointSelectType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class StateType extends AbstractEasytrip2Type {
/**
*
* {@inheritDoc}
*
* @see \Symfony\Component\Form\AbstractType::buildForm()
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add ( 'name', 'text', array (
'label' => 'State name'
) );
$builder->add ( 'code', 'text', array (
'label' => 'State code'
) );
$builder->add ( 'unloc', 'text', array (
'label' => 'State unloc code'
) );
// TODO : the validation on this form appears to not be done, thus i try to save (as it is considered as valid) a object which is null, thus fail in the setters.
$builder->add ( 'country', new CountrySelectType ( $this->app ), array (
'label' => false,
'cascade_validation' => true
) );
/**
* $builder->add ( 'hub', new GeopointSelectType ( $this->app, 'HUB' ), array (
* 'label' => 'Select a hub if necessary'
* ) );
*/
}
public static function getRefNames() {
$return = array ();
$countries = CountrySelectType::getRefNames ();
// $hubs = GeopointSelectType::getRefNames ();
$last;
foreach ( $countries as $value ) {
$return [] = array (
'in' => 'state_' . $value ['in'],
'out' => 'state_' . $value ['out']
);
}
/*
* foreach ( $hubs as $value ) {
* $return [] = array (
* 'in' => 'state_' . $value ['in'],
* 'out' => 'state_' . $value ['out']
* );
* }
*/
return $return;
}
/**
*
* {@inheritDoc}
*
* @see \Symfony\Component\Form\AbstractType::configureOptions()
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults ( array (
'data_class' => 'Easytrip2\Domain\State'
) );
}
/**
*
* {@inheritDoc}
*
* @see \Symfony\Component\Form\FormTypeInterface::getName()
*/
public function getName() {
return 'state';
}
}
的位置(类型为char)
在
Bonus
此时,if(Bonus>=45)
可能在运行时给出任何可能的值。
没有像Bonus = 0这样的默认赋值。
答案 3 :(得分:0)
Bonus
没有值,而且是char
。
您的变量类型应为int
,我怀疑您要将Bonus
设置为依赖于Salary
的值:
int Bonus = 0;
int Salary = 0;
int TotalSalary = 0;
// ...
if (Salary >= 45)
Bonus = 500;
else if (Salary > 40)
Bonus = 250;
else
Bonus = 150;
TotalSalary = Salary + Bonus;
// ...