[HELP] Online Shop

/*take note that this is not finish yet, so the answer are always 1 and yes as of now
My problem is how to debug it (sorry I am a newbie) I try to call the main function (as shown in the first code) but it is not possible. So I try to put it in another function but it is not working.(2nd code)
*/

FIRST CODE:

#include <iostream>
using namespace std;


void process(int item)
{
char ans;
if(item==1)
{
do
{
cout<<"Do you want to purchase Head Motion Tennis Shoes?[Y/N]";
cout<<"\nIt cost Php 3500";
cin>>ans;

if(ans=='y' || ans=='Y')
{
cout<<"Thank You for Purchasing!";
}
else
//it should go back to 'home' but i cant call main function as int main();



}while(ans!='Y' || ans!='y' || ans!='n' || ans!='N');


}

}








int main ()
{




int choice,item;

cout<<"Welcome To Eralyn's Online Shop";
cout<<"\nPlease select the category you want to browse through.";
cout<<"\n1.)Bags"<<"\n2.)Shoes"<<"\n3.)Top";

cin>>choice;
system("pause");
system("cls");





if(choice==1)
{cout<<"You are in the shoes category. These are the available items.";
cout<<"\n1.)Head Motion Tennis Shoes. Php 3500";
cout<<"\n2.)Head Lazer Running Shoes. Php 2400";
cout<<"\n3.)Nike Air Jordan Basketball Shoes. Php 5400";
cout<<"\n4.)Raffy Lopez Black Leather Shoes. Php 6200";
cout<<"\n5.)Enter the number of your choice: ";
cin>>item;
process(item);
}




system ("pause");
return 0;
}

SECOND CODE:
#include <iostream>
using namespace std;


void eralynshop()
{
int choice,item;

cout<<"Welcome To Eralyn's Online Shop";
cout<<"\nPlease select the category you want to browse through.";
cout<<"\n1.)Bags"<<"\n2.)Shoes"<<"\n3.)Top";

cin>>choice;
system("pause");
system("cls");





if(choice==1)
{cout<<"You are in the shoes category. These are the available items.";
cout<<"\n1.)Head Motion Tennis Shoes. Php 3500";
cout<<"\n2.)Head Lazer Running Shoes. Php 2400";
cout<<"\n3.)Nike Air Jordan Basketball Shoes. Php 5400";
cout<<"\n4.)Raffy Lopez Black Leather Shoes. Php 6200";
cout<<"\n5.)Enter the number of your choice: ";
cin>>item;
process(item);
}
}


void process(int item)
{
char ans;
if(item==1)
{
do
{
cout<<"Do you want to purchase Head Motion Tennis Shoes?[Y/N]";
cout<<"\nIt cost Php 3500";
cin>>ans;

if(ans=='y' || ans=='Y')
{
cout<<"Thank You for Purchasing!";
}
else
eralynshop();



}while(ans!='Y' || ans!='y' || ans!='n' || ans!='N');


}

}








int main ()
{

eralynshop();

system ("pause");
return 0;
}

You need two loops. One is inside process() and it keeps going until the user answers y or n. The second is inside main and it keeps going until the user wants to exit the program.

I've changed process() to return an bool indicating whether they wanted to make the purchase. I'm not sure if you'll need this for the assignment. The main program uses a bool called keepGoing to repeat the loop. You'll want to set that to false if the user wants to exit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;


bool
process(int item)
{
    char ans;
    bool result = false;

    if (item == 1) {
        do {
            cout << "Do you want to purchase Head Motion Tennis Shoes?[Y/N]";
            cout << "\nIt cost Php 3500";
            cin >> ans;

        } while (ans != 'Y' && ans != 'y' && ans != 'n' && ans != 'N') ;

        if (ans == 'y' || ans == 'Y') {
            cout << "Thank You for Purchasing!";
            result = true;
        }
    }
    return result;
}


int main()
{
    int choice,
        item;
    bool keepGoing = true;

    cout << "Welcome To Eralyn's Online Shop";

    do {
        cout << "\nPlease select the category you want to browse through.";
        cout << "\n1.)Bags" << "\n2.)Shoes" << "\n3.)Top";

        cin >> choice;
        system("pause");
        system("cls");





        if (choice == 1) {
            cout <<
                "You are in the shoes category. These are the available items."\
;
            cout << "\n1.)Head Motion Tennis Shoes. Php 3500";
            cout << "\n2.)Head Lazer Running Shoes. Php 2400";
            cout << "\n3.)Nike Air Jordan Basketball Shoes. Php 5400";
            cout << "\n4.)Raffy Lopez Black Leather Shoes. Php 6200";
            cout << "\n5.)Enter the number of your choice: ";
            cin >> item;
            process(item);
        }
    } while (keepGoing);



    system("pause");
    return 0;
}

Why is in the process function, declared bool= false? What does it mean?
Last edited on
Sorry, I don't understand the question. the process() function is declared on lines 5 & 6. It returns true if the user wants to make the purchase and false if they don't.
Topic archived. No new replies allowed.