You are here:

C++/Nested Loops

Advertisement


Question
Hi,  I have to write a program.  The Question is this:  
Jody is a teenager who is very fond of sweets. She buys sweets at three different shops. Write a C++ program that
reads the kind of sweets bought and the price of each sweet bought at the three shops. The kind of sweets should be
input as a char where c = ‘chocolate’, h = ‘chips’, i =’ ice-cream’ and o =’other’. The program should calculate and
display the total that Jody spent on the different kinds of sweets at each shop. Use a switch statement to update the
total spent on each kind of sweet. Use a sentinel driven while loop to prompt the user if more information should
be entered for a certain shop. The program should also calculate and display the total that Jody spends on sweets at
all the shops.
The program has the following structure:
• The five totals (total spent on chocolates, total spent on chips, total spent on ice-cream, total spent on other
sweets and total spent on all sweets) are initialized to 0.
• Now a for loop follows, going from 1 to the number of shops.
• Inside this loop a while loop displays a prompting message asking the user to enter the sweets and price
of the sweets bought at the certain shop. The kind of sweets and associated price are then input. A switch
statement follows to update the total spent so far on this kind of sweets at this shop.
• The while loop should prompt the user at the end of the loop to ask whether more information on this
shop needs to be entered. The while loop should terminate when the user enters 0.
• When the while loop is exited, the total spent for each kind of sweet at this shop should be displayed.
Thereafter the total spent on all sweets so far should be updated, and the totals for the 4 kinds of sweets
should be reset to 0 for the next shop.
• When the for loop is exited the total spent on sweets at all the shops should be displayed.

I wrote the following code only it is not working right.  What am I doing wrong?

#include <iostream>
using namespace std;
int main ()
{

   const int NrOfShops = 3;
   float price;
   char sweets, userAnswer;
   bool answer;
   
// 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 < 3; i++){
   sweets='1';

   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 sweets and 0 (Zero) to end:";
         cin >> sweets;
         getchar();
         
         if (sweets!='0')
               {
               cout << "How much money did you pay: ";
               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;
                     
                }
         }
       }
       
}

for (int i=0;i<NrOfShops;i++){// display total spent for each kind of sweet per shop
   cout<<"Shop Nr: "<<(i+1)<<":"<<endl;
   cout<<"Amount spent on chocolate:"<<totChoc[i]<<endl;
   cout<<"Amount spent on chips:"<<totChips[i]<<endl;
   cout<<"Amount spent on ice-cream:"<<totIce[i]<<endl;
   cout<<"Amount spent on other sweets:"<<totOther[i]<<endl;
   cout<<endl;
   }

cout<<"Total spent on all sweets on all shops is: "<<totSweets<<endl;

for (int i=0;i<NrOfShops;i++)
   {
   totChoc[i]=0;
   totChips[i]=0;
   totIce[i]=0;
   totOther[i]=0;
   }

totSweets=0;

getchar();
system ("pause");
return 0;
}

Answer
Hello Anne.
Good work on your program. The logic is correct but problems happen if you input an improper character when the program is doing cin >> price. For example, entering abc for price will cause the program to stop working. Is that the problem you are seeing ? If you are seeing some other problem, please let me know.

Generally for input, I recommend that you use the getline function and read into a string. With the getline function, you don't need to do a getchar to read the newline character that is stuck in the keyboard buffer. The getline does it automatically, and throws it away.

Below is your program with the getline. I also added a bit of error checking. My comments start with //ZM.
Let me know if you need more explanation.

Best regards
Zlatko

#include <iostream>
#include <string>
using namespace std;
int main ()
{

   const int NrOfShops = 3;
   float price;
   char sweets;

   //ZM added this string for user input
   std::string input;

   // 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';

       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 sweets and 0 (Zero) to end:";
           //ZM Instead of this:
           //ZM cin >> sweets;
           //ZM getchar();
           //ZM Use this:
           std::getline(cin, input);
           sweets = input[0];

           //ZM you can add some error checking too
           //This checks if the sweets character is in the allowed set before asking for price
           //You can look up what strchr does.
           if (strchr("chio0", sweets) == NULL)
           {
               cout << "That's not a proper choice\n";
           }
           else if (sweets!='0')
           {
               cout << "How much money did you pay: ";
               //ZM Instead of this:
               //ZM cin >> price;
               //ZM getchar();
               //ZM Use this:
               std::getline(cin, input);
               price = (float)atof(input.c_str());

               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;

               }
           }
       }

   }

   for (int i=0;i<NrOfShops;i++){// display total spent for each kind of sweet per shop
       cout<<"Shop Nr: "<<(i+1)<<":"<<endl;
       cout<<"Amount spent on chocolate:"<<totChoc[i]<<endl;
       cout<<"Amount spent on chips:"<<totChips[i]<<endl;
       cout<<"Amount spent on ice-cream:"<<totIce[i]<<endl;
       cout<<"Amount spent on other sweets:"<<totOther[i]<<endl;
       cout<<endl;
   }

   cout<<"Total spent on all sweets on all shops is: "<<totSweets<<endl;

   // ZM you don't really need to set everything back to 0 if your program is done.
   for (int i=0;i<NrOfShops;i++)
   {
       totChoc[i]=0;
       totChips[i]=0;
       totIce[i]=0;
       totOther[i]=0;
   }

   totSweets=0;

   getchar();
   system ("pause");
   return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.