473,480 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

using a while loop

16 New Member
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::setprecision;

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!!!
Apr 19 '08 #1
1 2057
Banfa
9,065 Recognized Expert Moderator Expert
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.
Apr 19 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
10713
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
4
2698
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display...
11
6546
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
10
12796
by: Orion | last post by:
Hey, I was wondering if it was possible to determine if you hit 'EOF' using fseek? I'm using fseek to traverse through the file from start to end and capturing the data into a linked list...
13
1758
by: Bev in TX | last post by:
We are using Visual Studio .NET 2003. When using that compiler, the following example code goes into an endless loop in the "while" loop when the /Og optimization option is used: #include...
6
17128
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
17
3031
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
1
7725
by: Thiero | last post by:
Hi I posted s thread but did have any reply, I am a new programmer and really wants someone to help me on how to use TreeMap for this code cos I want to it to be able to handle the options from 6...
4
2065
by: GeekBoy | last post by:
I am reading a file of numbers using for loops. The numbers are in a grid as follows: 8 36 14 11 31 17 22 23 17 8 9 33 23 32 18 39 23 25 9 38 14 38 4 22 18 11 31 19 16 17 9 32 25 8 1 23
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.