Finding sum of User Inputed Values - c++

I need help adding up several user inputed values using C++
double total; // Accumulates total
double price; // Gets next price from user
int numItems; // Number of items
int counter = 1; // Loop control counter
cout << "How many items do you have? ";
cin >> numItems;
cout << endl;
while (counter <= numItems) {
total = 0;
cout << "Enter the price of item " << counter << ": ";
cin >> price;
cout << endl;
total += price;
counter++;
}
cout << "The total price is: " << total << endl;
When I run my code, I end up getting the sum of only one value from the user

Yes, just as Chetan Ranpariya said:
total = 0; should be before while loop.

Related

Trying to end for loop

I am trying to create a loop that allows the user to enter as many elements to the array as they would like then sum up those elements. I need the loop to terminate when the user enters a negative number. How would I go about terminating this?
double sum = 0;
double group[] = { 0 };
for (int i = 0; i >= 0; i++) {
cout << "Please enter employee salary. Enter negative number to end." << endl;
cout << "Employee " << i + 1 << ": $";
cin >> group[i];
if (i < 0) {
break;
}
sum += group[i];
}
cout << "The total salary ouput for Ernest Inc is: $" << fixed << showpoint << setprecision(2) << sum << endl;
I need the loop to terminate when the user enters a negative number.
For that a while loop would be better than for. You should also use vector which allows arbitrary number of items.
Something like this:
vector<double> group;
double salary;
while (true)
{
cout << "Please enter employee salary. Enter negative number to end." << endl;
cout << "Employee " << i + 1 << ": $";
cin >> salary;
if (salary<0)
{
break;
}
group.push_back(salary);
sum += salary;
}

How do I stop a sentinel value from being read into an array when determining total and average of the array?

I am not sure how to stop my sentinel value from being read into my array which is causing an error with my total and average calculation. Can anyone help?
Here is the while loop:
while (grade != -1)
{
cin >> grade;
gradesArray[count] = grade;
total += gradesArray[count];
average = total / count;
count++;
}
cout << "You have entered " << count << " grades." << endl;
cout << "The average of these grades is " << average << endl;
while (std::cin >> grade && grade != -1)
Here is my modified code, but the average outputs an average that is too high.
#include<iostream>
using namespace std;
int main()
{
int count = 0, grade = 0;
double total = 0, average;
const int numGrades = 20;
int gradesArray[numGrades];
cout << "Welcome to the grade calulator!" << endl;
cout << "Enter up to twenty grades, when done enter -1: " << endl;
while (cin >> grade && grade != -1)
{
gradesArray[count] = grade;
total += gradesArray[count];
average = total / count;
++count;
}
cout << "You have entered " << count << " grades." << endl;
cout << "The average of these grades is " << average << endl;
system("pause");
return 0;
}

Calories counting loop

Question:
Create a program that first asks the user to enter the number of items he/she has eaten today and then to enter the number of calories for each item. It then calculates the number of calories he/she has eaten for the day and displays the value.
#include <iostream>
using namespace std;
int main()
{
int numberOfItems;
int count; // loop counter for the loop
int caloriesForItem;
int totalCalories;
cout << "How many items did you eat today? ";
cin >> numberOfItems;
cout << "Enter the number of calories in each of the " << numberOfItems << " items eaten:" >> endl;
while (caloriesForItem)
{
cout << "Please enter item calories:";
cin >>caloriesForItem;
count == numberOfItems;
}
cout << "Total calories eaten today = " << totalCalories;
return 0;
}
In the end I'm not sure how I would make it read and calculate as it says it must be a while loop.
You have all of your pieces already. You have a count for your loop, a totalCalories tracker, and a variable to hold the current items caloriesForItem. Every iteration of your loop must increase the count, and every iteration must retrieve a new value for caloriesForItem. You can add this each time to totalCalories.
#include <iostream>
using namespace std;
int main()
{
int numberOfItems;
int caloriesForItem;
// These two variables need to have a start value
int count = 0;
int totalCalories = 0;
cout << "How many items did you eat today? ";
cin >> numberOfItems;
cout << "Enter the number of calories in each of the " << numberOfItems << " items eaten:" >> endl;
// Loop for as many times as you have items to enter
while (count < numberOfItems)
{
cout << "Please enter item calories:";
// Read in next value
cin >> caloriesForItem;
// Add new value to total
totalCalories += caloriesForItem;
// Increase count so you know how many items you have processed.
count++;
}
cout << "Total calories eaten today = " << totalCalories;
return 0;
}
You were pretty close though.
(I added comments in the appropriate places to explain whats going on.)
Lastly I want to add that you pretty much can convert any for loop to a while loop using this themplate:
for( <init_var>; <condition>; <step> ) {
// code
}
becomes
<init_var>;
while( <condition> ) {
// code
<step>;
}
Example:
for(int i = 0; i < 10 i++) {
cout << i << endl;
}
becomes
int i = 0;
while(i < 10) {
cout << i << endl;
i++;
}
Take a look at this code:
#include <iostream>
using namespace std;
int main()
{
int numberOfItems, caloriesForItem, totalCalories=0;
cout << "How many items did you eat today? ";
cin >> numberOfItems;
cout << "Enter the number of calories in each of the " << numberOfItems << " items eaten:" << endl;
while (numberOfItems--)
{
cout << "Please enter item calories:";
cin >> caloriesForItem;
totalCalories += caloriesForItem;
}
cout << "Total calories eaten today = " << totalCalories;
return 0;
}
It's a sufficient way to do what you wanted, if you don't understand, let me know!

C++ Input and Output problems

My problem is that when i compile and run the program, the hours doesnt have "hours" listing 1,2,3 as the loop continues and also the loop calculation is the same for each line.
this is what the program looks like
http://postimg.org/image/htk194eah/
the calculation is wrong and the hours is suppose to say 1,2...5
I would like it to look something like this
http://postimg.org/image/pnkvab1j1/
this is what i have so far:
int main()
{
// Variables
int speed;
int time;
int distance;
// Obtain the speed
cout << "Please input the speed of the vehicle " ;
cin >> speed;
while(speed < 0) // while statement
{
cout << "Please refrain from using a negative number ";
cin >> speed;
}
cout << "Please enter the time, represented in hours, travelled" <<endl;
cin >> time;
// Obtain the time
while(speed < 1)
{
cout<< "Please use a number greater than 1 " <<endl;
cin >> time;
}
// Calculation
distance = speed * time;
cout << endl;
cout << "Hour(s) " << "\t" << "Distance Travelled" << endl;
cout << "____________________________________________" << endl;
// "for" Loop statement
for(int count =1; count <= time; count++)
{
cout << " " << "\t\t" << speed*time << endl;
}
system ("PAUSE");
return 0;
}
When you print in your for loop, speed = 20 and time = 5 always, so it always prints 100 (in the example you give).
You want to be printing speed*count (in this case).
Again you print "" for the hours, which is an empty string, you want to be printing count.
cout << count << "\t\t" << speed*count << endl;
Here is the correct program. Your program was very good. But you have a very small mistake.
#include<iostream.h>
main()
{
// Variables
int speed;
int time;
int distance;
// Obtain the speed
cout << "Please input the speed of the vehicle " ;
cin >> speed;
while(speed < 0) // while statement
{
cout << "Please refrain from using a negative number ";
cin >> speed;
}
cout << "Please enter the time, represented in hours, travelled ";
cin >> time;
// Obtain the time
while(speed < 1)
{
cout<< "Please use a number greater than 1 ";
cin >> time;
}
// Calculation
cout << endl;
cout << "Hour(s) " << "\t" << "Distance Travelled" << endl;
cout << "____________________________________________" << endl;
// "for" Loop statement
for(int count =1; count <= time; count++)
{
cout << count << "\t\t" << speed * count << endl;
}
system ("PAUSE");
return 0;
}

How to find the average of user inputs

so here is my code (stripped of the headers because that is irrerevelant.)
int main() {
float program = 0;
float scores = 0;
float test = 0;
float testScores = 0;
float e = 1;
float exam = 0;
float programAverage = 0;
cout << "Enter the number of assignments that were graded: ";
cin >> program;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
}
cout << "Enter the number of test: ";
cin >> test;
for (int e = 1; e <= test; e++){
cout << "Enter the score for test # " << e << ": "; cin >> testScores;
}
cout << "Enter the final exam score: ";
cin >> exam;
programAverage = (scores/program);
cout << "Program Average: " << programAverage << endl;
}
the last part I am having problems because whenever I compile my program the compiler just remembers the last number the user entered and does not average it. How can i get it to add all the user input numbers together and then average?
int main() {
float program = 0;
float scores = 0;
float test = 0;
float testScores = 0;
float e = 1;
float exam = 0;
float programAverage = 0;
float scoresSum = 0; // variable that adds up all the input scores
cout << "Enter the number of assignments that were graded: ";
cin >> program;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
scoresSum += scores; // adds up all the scores
}
cout << "Enter the number of test: ";
cin >> test;
for (int e = 1; e <= test; e++){
cout << "Enter the score for test # " << e << ": "; cin >> testScores;
}
cout << "Enter the final exam score: ";
cin >> exam;
programAverage = (scoresSum/program); // divide the total score out of program number
cout << "Program Average: " << programAverage << endl;
}
So the problem was that you didn't add up the input scores.
The variable "scores" only has the value of the last input score.
You have to set up a variable to sum up all the input score so far, such as scoresSum in the code.
And add up the score every time a score is submitted.
You can easily find the difference between your code and mine by looking at the line with comment.
float _sum=0;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
_sum+=i;
}
programAverage = (_sum/program);
cout << "Program Average: " << programAverage << endl;
Well yeah because of this loop, scores always has the last value entered:
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
}
An average is defined as a sum divided by the number of instances. You're not summing, you just keep overwriting "scores" with the last read value when you do cin >> scores. So the problem can be restated as "How can I sum all the numbers the user entered in?" Computers do exactly what you tell them to, and you need to figure out how to exactly tell it to sum all the entered scores.
Well how would you do that in real life? You'd keep a running tally of all the scores, maybe by adding them with a calculator. You'd first initialize a count:
double sum = 0.0;
Then after the line for `cout << "Enter the score..." you add to sum:
sum = sum + scores;
Or C++ has handy shorthand notation
sum += scores