[英]Beginner “while” program in C++
I have to solve a problem where I calculate the salary of employees where they get 10 euros per hour for the first 40 hours and then every additional hour they get 15 euros. 我必须解决一个问题,在该问题中,我计算员工的薪水,在最初的40个小时中,他们每小时获得10欧元,然后每增加1个小时,他们获得15欧元。 I have solved the problem but my console prints the answer in an endless loop and I don't know where I am wrong.
我已经解决了问题,但是我的控制台无休止地打印了答案,但我不知道我哪里写错了。
int hours;
double salary;
int main()
{
cout << "Enter amount of hours worked" << endl;
cin >> hours;
while (hours <= 40)
{
salary = hours * 10;
cout << "Salary of the employee is: " << salary << endl;
}
while (hours > 40)
{
salary = (40 * 10) + (hours - 40) * 15;
cout << "Salary of the employee is: " << salary << endl;
}
system("pause");
return 0;
}
Change your while
s to if
s. 将您的
while
更改为if
。
The condition inside while
would be always true
, because hours
will always be less than 40, as no modification of hours
inside the while
condition hence resulting in an infinite loop . while
内的条件将始终为true
,因为hours
始终小于40,因为while
内的hours
没有修改,因此导致无限循环 。
Modified code: 修改后的代码:
int hours;
double salary;
int main()
{
cout << "Enter amount of hours worked" << endl;
cin >> hours;
if (hours <= 40)
{
salary = hours * 10;
cout << "Salary of the employee is: " << salary << endl;
}
else //I have removed the condition because if hours is not less than 40,
// it has to be greater than 40!
{
salary = (40 * 10) + (hours - 40) * 15;
cout << "Salary of the employee is: " << salary << endl;
}
system("pause");
return 0;
}
Since you are hell bent on getting a while
loop solution, 由于您全神贯注于获取
while
循环解决方案,
Code: 码:
int hours;
int counthour = 0;
double salary;
int main()
{
cout << "Enter amount of hours worked" << endl;
cin >> hours;
while (counthour <= hours)
{
if(counthour <= 40)
salary += 10;
else
salary += 15;
counthour++;
}
cout << "Salary of the employee is: " << salary << endl;
system("pause");
return 0;
}
In order for the following loop to not be infinite 为了使以下循环不是无限的
while (hours <= 40)
{
salary = hours * 10;
cout << "Salary of the employee is: " << salary << endl;
}
Something in the loop is going to have to modify hours
in a way that will cause hours <= 40
to be false
. 循环中的某些内容将必须以导致
hours <= 40
false
的方式修改hours
。
right now, only salary
is getting modified in that loop. 目前,只有
salary
在该循环中被修改。
You're using while loops like they're if statements, kind of. 您正在使用while循环,就像它们是if语句一样。
Use two variables, one to take the amount of hours worked and one to start at 0 and count each hour paid. 使用两个变量,一个变量用于计算工作时间,另一个变量从0开始计算每个小时的工资。
int hours;
int hourscounted = 0;
double salary;
int main()
{
cout << "Enter amount of hours worked" << endl;
cin >> hours;
while (hourscounted <= hours)
{
if(hourscounted < 40)
{
salary = salary + 10;
}
else
{
salary = salary + 15;
}
hourscounted++;
}
cout << "Salary of the employee is: " << salary << endl;
system("pause");
return 0;
}
Change while to if :P And tip for future - do not use endl, unless you really need to do it. 更改为if:P的时间,并为将来做些提示-请勿使用endl,除非您确实需要这样做。 And you can have printing (cout) outside of loop/if-statement.
并且您可以在循环/ if语句之外进行打印(裁剪)。 It will never end, the while loop, it depends on something, that is not changed.
它永远不会结束,而while循环则取决于某些不变的东西。
EDIT: After reading your code and problem in more detail, it seems you should replace while
by if
to satisfy the logic you are looking at. 编辑:读你的代码和问题进行更详细后,似乎就应该更换
while
通过if
为了满足你正在寻找的逻辑。
If your code prints in an endless loop, you should always check that why the conditions for its termination are not being met. 如果代码循环打印,则应始终检查是否未满足终止条件。
In your case, your loops terminate when hours < 40
(1st case) and hours > 40
(2nd case). 在您的情况下,循环在
hours < 40
(第一种情况)和hours > 40
(第二种情况)时终止。 You are not modifying hours in the loop, and so it is stuck in an infinite cycle. 您没有在循环中修改小时数,因此它陷入了无限循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.