NEED HELP WITH "WHILE LOOP"

This is what I have to do:
1.Create a variable named total (double floating point), a constant double variable named TAX=1.13.

2.Create and 10 element array of doubles (with a constant size declarer) and use an initialization list to set all the elements to zero.

3.Query the user to enter the price of up to ten items in the array (using a while loop). Have the user enter -99 to end entering data (if they choose to enter less than 10 items).

4.Call a function named sumall (which has a prototype before the main function and is defined after the main function) and pass the array and array length into it. The sumall function sums all the elements of the array and returns the total. Assign the output of the function to the variable ‘total’.

5.Have the computer tell the user the total of the bill plus tax.

Problem I can't seem to put a while loop in that tells the program to stop when -99 is entered I get errors or the program doesn't work. Any suggestions will help thanks!!

This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iomanip>
using namespace std;

double total;
const double TAX = 1.13;
double item[10];
int i = 0;

double sumall()
{
for(int i = 0; i < 10; i++)
{

total += item[i];

}
total = total * TAX;
return 0;
}

int main()
{
cout<<"Please enter the prices: "<<endl;

for(int i = 0; i < 10; i++)
{
cin>>item[i];
}
sumall();
cout<<"The total, including tax, for all the groceries is $"<<setprecision(2) <<fixed<<total<<endl;
system("PAUSE");
return 0;
}
You can break loops like this:
1
2
3
4
5
6
7
8
9
 
while(true) //Infinite loop 
{
    std::cin >> n; //Ask the user to enter a value 
    if(n == -1) //If the value is -1
    {
         break; //We exit the while loop. 
    }
}

I'm sure you can adapt that to your program.

By the way, you did not do step 2 or 4 in your assignment yet. Do you know what you're doing with those?
Global constants OK. Avoid global variables like you have here.

1
2
3
4
double total;
const double TAX = 1.13;
double item[10];
int i = 0;


Your instructions call for a function prototype you didn't make one. http://www.cplusplus.com/doc/tutorial/functions2/


Shell:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>

using namespace std;

const int SIZE = 10;
const double TAX = 1.13;

//Prototype
double sumAll(double[], int);

int main()
{
	double total;
	double items[SIZE] = { 0 };//Init. array
	double price = 0;
	int index = 0;

	while (price != -99 && index < SIZE){
		//Code Here
                //items[index]
		index++;
	}
	//Finish Up
	return 0;
}

//Function
double sumAll(double items[], int size){
	double total;
	//Code here
	return total;
}
Last edited on
Topic archived. No new replies allowed.