Question
Need help with logic error in c++ code. My for loop only finds the average of the last user input instead of the TOTAL inputs
Need help with logic error in c++ code. My for loop only finds the average of the last user input instead of the TOTAL inputs of the program.
problem below:
Five students at a local middle school volunteered to sell fresh baked cookies to raise funds to increase the number of computers for the computer lab. Each student reported the number of boxes he/she sold. Cost of each box of cookies was $4.50. Write a program that will output the total number of boxes of cookies sold, the total revenue generated by selling the cookies, and the average number of boxes sold by the students. Prompt the user to enter the following information: the StudentID number and the number of boxes sold.
SAMPLE RUN (text in blue is user entered)
Enter ID for student 1 : 101
Enter number of boxes sold by 101 : 6
Enter ID for student 2 : 102
Enter number of boxes sold by 102 : 5
Enter ID for student 3 : 103
Enter number of boxes sold by 103 : 9
Enter ID for student 4 : 104
Enter number of boxes sold by 104 : 10
Enter ID for student 5 : 105
Enter number of boxes sold by 105 : 7
Total Revenue = $166.5
Average Boxes Sold by 5 students = 7.4
my code:
code below:
#include
using namespace std;
//start of main function
int main()
{
//declare inputs and outputs
int StudentID;
double BoxesSold;
double TTLRevenue; //total revenue for cookie sales
double AVGbox; // Average of cookie sales
double cost = 4.50; //the cost of 1 box of cookies
int total = 5; //total number of students selling cookie boxes
for (int counter=1; counter <= 5; counter++) //number of student entries = 5
{
//instruct user to input ID number for students 1-5
cout << "Enter ID number for student " << counter < cin >> StudentID; //instruct user to enter # of boxes sold by user's specific ID number cout << "Enter number of boxes sold by " << StudentID << endl; cin >> BoxesSold; } //calculate the value for total revenue TTLRevenue = BoxesSold * cost; //calculate Average # of boxes sold by all students AVGbox = BoxesSold / total; //output total revenue for user cout << "Total Revenue = " << TTLRevenue << endl; //output Average boxes sold by students cout << "Average boxes sold by " << total << " students" << AVGbox << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started