### 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:
 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include 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: "<>item[i]; } sumall(); cout<<"The total, including tax, for all the groceries is \$"<
You can break loops like this:
 ``123456789`` `````` 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. } }``````

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.

 ``1234`` ``````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:
 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include 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.