Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Nested Loops (http://www.go4expert.com/forums/nested-loops-t21842/)

Mollera 19Apr2010 22:57

Nested Loops
 
Hi! I'm lost. I'm trieng to write the program. It just won't work. I posted the code below.
Code:

#include <iostream>
using namespace std;
int main ()
{
    const int NrOfShops = 3;
    float price;
    char sweets, userAnswer;
    bool answer;
   
// declare and initialize totals
float totChoc = 0,0, totChips = 0.0, totIce = 0.0, totOther = 0.0 totSweets = 0.0;
// loop over the number of shops
for (int i = 1; i <= 3; i++)
{
    answer = true;
    cout << "Enter the sweets boaught at Shop nr: " << i << endl;
   
// sentinel while loop prompting for kind of sweets bought
while (i <= 3 && sweets == 0)
        {
        cout << "c=chocolate, h = chips, i = ice-cream, o = other and 0 to end: and price: " << endl;
        cin >> sweets, price;
        }
// enter the kind of sweets bought (c=chocolate,
// ch = chips, i = ice-cream, o = other) as well as price
// Update the total cost per kind of sweet
                switch (userAnswer)
                {
                case 'c':
                      (userAnswer++) * price;
                     
                      break; 
                case 'h':
                      (userAnswer++) * price;
                      break;
                case 'i':
                      (userAnswer++) * price;
                      break;
                case 'o':
                      (userAnswer++) * price;
                      break;
                }
}
// display total spent for each kind of sweet
// update total spent on all sweets so far
// reset totals of sweets
// display total spent on sweets at all shops
system ("pause");
return 0;
}

I hope the comments will help you to understand what I'm attempting to do. I't is just not working and I don't know how to fix it. Any assistance will be welcome!

Thank you
Mollera

virxen 20Apr2010 14:02

Re: Nested Loops
 
with what i read in your comment maybe you want this.
check it and tell me.

Code:

#include <iostream>
using namespace std;
int main (){
    const int NrOfShops = 3;
    float price;
    char sweets;
// declare and initialize totals

float totChoc[NrOfShops] ={0}, totChips[NrOfShops] ={0}, totIce[NrOfShops] ={0}, totOther[NrOfShops] ={0}, totSweets=0;
// loop over the number of shops
for (int i = 0; i <NrOfShops; i++){
    sweets='1';
    //answer = true;
    cout << "Enter the sweets bought at Shop nr: " << (i+1) << endl;
// sentinel while loop prompting for kind of sweets bought
// enter the kind of sweets bought (c=chocolate,
// ch = chips, i = ice-cream, o = other) as well as price
    while (sweets != '0'){
          cout << "c=chocolate, h = chips, i = ice-cream, o = other and 0 to end:";
          cin >> sweets;
          getchar();
          if (sweets!='0'){
                cout << "money paid: ";
                cin >> price;
                getchar();
                switch (sweets){// Update the total cost per kind of sweet
                    case 'c':
                      totChoc[i]+=price;
                      totSweets+=price;
                      break; 
                    case 'h':
                        totChips[i]+=price;
                        totSweets+=price;
                        break;
                    case 'i':
                      totSweets+=price;// update total spent on all sweets so far for all shops
                      totIce[i]+=price;
                      break;
                    case 'o':
                      totSweets+=price;
                      totOther[i]+=price;
                      break;
                }//end switch
          }//end if
        }//end while
}//end for
for (int i=0;i<NrOfShops;i++){// display total spent for each kind of sweet per shop
    cout<<"shop Nr "<<(i+1)<<":"<<endl;
    cout<<"chocolate spent:"<<totChoc[i]<<endl;
    cout<<"chips spent:"<<totChips[i]<<endl;
    cout<<"ice-cream spent:"<<totIce[i]<<endl;
    cout<<"other spent:"<<totOther[i]<<endl;
    cout<<endl;
}

cout<<"totaly spent on sweets on all shops is "<<totSweets<<endl;// display total spent on sweets at all shops

for (int i=0;i<NrOfShops;i++){// reset totals of sweets
    totChoc[i]=0;
    totChips[i]=0;
    totIce[i]=0;
    totOther[i]=0;
}
totSweets=0;

getchar();
return 0;
}//end main



All times are GMT +5.5. The time now is 09:31.