Sum Multiple Value At a Time
-
Hi, I am a beginner in C++. Today my first program is summed two value. Question: Write a program that inputs the number of hours that an employee works and the employee wage. Then display the employee gross pay(Be sure promote to input). I have a question here I input two value and calculate their result and display, but I want to input more than two value. How to input more than two value and calculate them?
#include
using namespace std;
int main()
{
int i, m, j, n, k, l;
cout <<"Enter Your Work Hour:.\n";
cin >>i >>m;
cout << "Enter Your Salary:.\n";
cin >> k >> n;
j = i+m;
l = k+n;
cout << "Your Total Work Hour is:" << j < -
Hi, I am a beginner in C++. Today my first program is summed two value. Question: Write a program that inputs the number of hours that an employee works and the employee wage. Then display the employee gross pay(Be sure promote to input). I have a question here I input two value and calculate their result and display, but I want to input more than two value. How to input more than two value and calculate them?
#include
using namespace std;
int main()
{
int i, m, j, n, k, l;
cout <<"Enter Your Work Hour:.\n";
cin >>i >>m;
cout << "Enter Your Salary:.\n";
cin >> k >> n;
j = i+m;
l = k+n;
cout << "Your Total Work Hour is:" << j <By 'more than two' do you mean a variable number of inputs, or a fixed number?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Hi, I am a beginner in C++. Today my first program is summed two value. Question: Write a program that inputs the number of hours that an employee works and the employee wage. Then display the employee gross pay(Be sure promote to input). I have a question here I input two value and calculate their result and display, but I want to input more than two value. How to input more than two value and calculate them?
#include
using namespace std;
int main()
{
int i, m, j, n, k, l;
cout <<"Enter Your Work Hour:.\n";
cin >>i >>m;
cout << "Enter Your Salary:.\n";
cin >> k >> n;
j = i+m;
l = k+n;
cout << "Your Total Work Hour is:" << j <You'd need to use an array and a loop. Something like:
int hours[5] = {0};
for (int x = 0; x < 5; x++)
{
cout << "Enter hours worked for day " << (x + 1) << ": ";
cin >> hours[x];
}"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
By 'more than two' do you mean a variable number of inputs, or a fixed number?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
Variable number of input..
-
You'd need to use an array and a loop. Something like:
int hours[5] = {0};
for (int x = 0; x < 5; x++)
{
cout << "Enter hours worked for day " << (x + 1) << ": ";
cin >> hours[x];
}"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Hi David, I got it thanks for your help.
-
Variable number of input..
In keeping with Davids theme, you could specify an array that's larger than you would ever need, and have the user enter the number of entries first, then setup an input loop based on that. Or setup a loop that runs the same number of times as your array size and have the user enter a value (like -1) that denotes the end of the data at which point you would exit the loop, then process the the data entered prior to the -1 entry.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle