Working with counter-controlled while loops

Write your question here.

Exercise 1. Write an algorithm (pseudocode) to read a set of sales data items from standard input and calculate and output their total and their average. Prompt user to enter number of data items.

Below is the code that I have written so far but I keep getting an error. What am I doing wrong?

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

double total, average;
int days;

cout << "How many days do you have sales for?";
cin >> days;
for (int sales = 1; sales <= days; sales++)
{

cout << "Enter the number of sales: ";
cin >> sales;
total += sales;
average = total / days;
}
cout << fixed << showpoint << setprecision(2);
cout << "The total sales are: $" << total << endl;
cout << "The averages sales are: $" << average << endl;

system("pause");

return 0;
}
You need to learn to use [code] [/code] tags and you need to give a better problem description than "i keep getting an error". Compiler error? Linker error? Segfault? Logic error? What input? What output? What is it doing wrong? What was it supposed do?
Last edited on
Notice total += sales that is equivalent to total = total + sales
But what happens in the first iteration of the for loop? total is being assigned total + sales, but what is total's value?

Just initialize total to 0 and you should be good. ;)
(double total=0, average;)
i get "cout is ambiguous" as an error
I will try that Nwb
This is what I have now and I need to.......

Exercise 2. Create a test data set to verify your algorithm. How many cases are needed? Explain.

How do I create the test data? ( I know this may sound like a stupid question)



#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

double total=0, average;
int days;

cout << "How many days do you have sales for? ";
cin >> days;
for (int count = 1; count <= days; count++)
{
double sales;
cout << "Enter the number of sales for day " << count << ": ";
cin >> sales;
total += sales;
average = total / count;
}
cout << fixed << showpoint << setprecision(2);
cout << "The total sales are: $" << total << endl;
cout << "The averages sales are: $" << average << endl;

system("pause");

return 0;
}
Edit your post and put repaste your code in code tags like this:

[code]
your code goes here
[/code]

otherwise it's unreadable.
Last edited on
"cout is ambiguous" is a common compiler error I have had it happen to me twice, I don't know the exact reason why it happens.. But rebooting my compiler seemed to work (I use Microsoft VS 2017).

Your exercise 2 involves nothing but giving sample inputs and checking whether the program and its logic works. That's it.

I think what they want you to do is handle unexpected situations like entering a negative value, not entering a number etc. See what errors you can think of and handle them. If you didn't get something you can always ask use. ;)
Topic archived. No new replies allowed.