Nested Loops

Discussion in 'C' started by Mollera, Apr 19, 2010.

  1. Mollera

    Mollera New Member

    Joined:
    Apr 19, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     
    Last edited by a moderator: Apr 20, 2010
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    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
    
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice