0
I am a beginner and my question may seem simple.... but whatever 😋 Does anyone know how to calculate the sum of a do while loop
So basically I created a loop that loops 5x... I asked the user to enter 5 prices. using an If statement I determine if an additional fee will be added... I need to sum up and display the users entries w/additional fees.
7 Respuestas
+ 2
use an array to store every user input
while i<5
store user input in the array[i]
if the array[i] is more than the price you want to apply the fee to
array[i] += fee;
increase I for the next loop (i++)
all prices wit fee will be in the array
sum the elements of the array to get what you want.
Showing your code could get you a more detailed and precise answer
+ 2
No, it's a do-while loop: the empty block is unnecessary, but not embarrassing...
In fact, your code works... but not in the sololearn code playground :(
Really you can do it to work in code playground:
1/ comment the line with the system("pause"); command ( unnecessary in code playground, and cause an error -- and prefer the use of 'cin' to pause/wait for an user input ;)
2/ starting to run, a window prompt to ask the entry you want provide to your appli': you need ( and cannot do another way ) fill in all the entries your app will require, once by line in a single multiline entry...
+ 1
Show your already written code ;)
+ 1
my bad I deleted my wrong answer.
but I think the if else evaluating the inputs
after the while statement should be included in the do-while loop. As it is, the operations on the inputs will be carried out only on the last two entries of o and s
0
#include <iostream>
using namespace std;
int main()
{
int o, s;
int sum;
int total;
int a = 0;
int num = 5;
do {
cout << "Please enter the number of items ordered \n" << endl;
cin >> o;
cout << "Please enter the price of each item ordered \n" << endl;
cin >> s;
a++;
} while (a < 5);
{
}
total = o * s;
if (s > 200) {
cout << "Shipping is Free \n" << endl;
}
else {
if (s < 200) {
cout << "Shipping and Handling is: " << (o * 10) << endl;
cout << "Total Cost: " << (o * s) +total << endl;
}
}
system("pause"); //This command will force the display to pause to allow you to read the display
return 0;
}
0
I really appreciate the help thanks!
0
I am now getting an unmatched error on line 34. I deleted the bracket on line 27. the code does run the loop correctly but it is not given me total sum.