I cant figure out what should i do so that in case one, if i choose to continue buying, not to start from the begining, but just to do the remaining calculations. also if it is possible to help me with a function outside main so that i calculate the purchase and money left and call them in each case.. thanks a lot

#include <iostream.h>
#include <conio.h>


using namespace std;



int main()
{
    int prices[8];
    prices[0]=13.55;
    prices[1]=0.35;
    prices[2]=0.30;
    prices[3]=0.25;
    prices[4]=1.25;
    prices[5]=12.45;
    prices[6]=1.05;
    prices[7]=2.00;
    prices[8]=13.15;
    int money;
    int choice;
    int x;
    int calc;
    int y;
    int spent;
    cout<< "**Welcome to the buying machine**\n\n"<<endl;
    cout<<"Enter amount of money you have"<<endl;
    cin>>money;
           
    cout<<"select what you want to buy"<<endl;
    cout<<"1.Book\t\t 6.USB"<<endl;
    cout<<"2.Pencil\t 7.CD"<<endl;
    cout<<"3.Pen\t\t 8.DVD"<<endl;
    cout<<"4.Ruber\t\t 9.Flash Card"<<endl;
    cout<<"5.Ruler\t\t 10.Finish"<<endl;
    cout<<"Enter your choice here"<<endl;
    cin>>choice;
            




        switch(choice){

 case (1):{
        cout<<"how many items do you want to buy?"<<endl;
        cin>>x;
        cout<<"you have bought"<<x<<" books"<<endl;
        spent=prices[0]*x;
        calc=money-spent;
        cout<<"You spent\b"<< spent <<"\bmoney"<<endl;
        cout<<"credit left :"<< calc <<""<<endl;
        cout<<"Do you want to continue purchasing\n1.Yes\n2.No"<<endl;
        cin>>y;
        if (y = 1) {
            return main();
        }

        else
        {
        cout<<"Thanks for using our Buying machine"<<endl;
        return 0;
        }
        return 1;
        }

 case (2):{
        cout<<"how many items do you want to buy?"<<endl;
        cin>>x;
        cout<<"you have bought books"<<endl;
        return main();
        }
 case (3):{
        cout<<"how many items do you want to buy?"<<endl;
        cin>>x;
        cout<<"you have bought books"<<endl;
        return 1;
        }


 case (4):{
        cout<<"how many items do you want to buy?"<<endl;
        cin>>x;
        cout<<"you have bought books"<<endl;
        return 1;
        }

 case (5):{
        cout<<"how many items do you want to buy?"<<endl;
        cin>>x;
        cout<<"you have bought books"<<endl;
        return 1;
        }

 case (6):{
        cout<<"how many items do you want to buy?"<<endl;
        cin>>x;
        cout<<"you have bought books"<<endl;
        return 1;
        }

 case (7):{
        cout<<"how many items do you want to buy?"<<endl;
        cin>>x;
        cout<<"you have bought books"<<endl;
        return 1;
        }

 case (8):{
        cout<<"how many items do you want to buy?"<<endl;
        cin>>x;
        cout<<"you have bought books"<<endl;
        return 1;
        }

 case (9):{
        cout<<"how many items do you want to buy?"<<endl;
        cin>>x;
        cout<<"you have bought books"<<endl;
        return 1;
        }

 case (10):{
        cout<<"thank you for using buying machine"<<endl;
        return 0;
        }

        }







}

I would consider replacing your "return" statements with "break" within your switch. Then you could do something along the lines of:

case(1):
   ...
   ...
   keep buying?
   if (answer != y && answer != Y)
       break;
case(2):
   ...
   ...
   //etc. etc.

Edited 5 Years Ago by Duki: n/a

Please mention problem also.

The problem is mentioned in the attached pdf file. I would need some help creating a function to call the prices of the items, do the calculations within function and then call the result when printing. thanks for answering.

thanks

This article has been dead for over six months. Start a new discussion instead.