using a while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zelmila19
    New Member
    • Apr 2008
    • 16

    using a while loop

    Hi can anyone tell me how to do this problem:

    Write a program using a while loop that asks the user to enter their sales for each month until they enter -1 (sentinel value). After this, it should tell the user how many months, the total sales and the average sales.
    If the user enters a negative value as the first input, it should not cause an error.
    The program should be able to produce the output below:

    Please enter your sales for month 1: 200
    Please enter your sales for month 2: 100
    Please enter your sales for month 3: 0
    Please enter your sales for month 4: -1

    For 4 moths:

    Total sales: $300.00
    Average sales: $150.00
    Press any key to continue.......

    or

    Please enter your sales for month 1: -1

    For 0 months:

    Total sales: $0.00
    Average sales: $0.00
    Press any key to continue....... ....


    So far I've come up with:
    #include <iostream>
    #include <iomanip>

    using namespace std;
    using std::fixed;
    using std::setprecisi on;

    int main()
    {
    double sales = 0.0;
    double totalSales = 0.0;
    double average = 0.0;
    int i = 0;

    while (sales !=-1)
    {
    i <= 12; i++;
    cout << "Please enter your sales for month " << i << ": ";
    cin >> sales;
    } //end while

    cout << "For " << i << ": ";


    system ("PAUSE");
    return 0;
    }

    but am confused how it should read how many months there were and where put setprecision(2) and the fixed notation.

    Please help!!!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by zelmila19
    but am confused how it should read how many months there were and where put setprecision(2) and the fixed notation.
    The number of months is the number of times you have been through the while loop -1 (because the last time round was not a month entry but a sentinel value entry).

    setprecission and fixed need to be used in the cout statement just before you output the variables they are to apply to.

    Comment

    Working...