Need advise on my looping!

Need advise on my looping end part needs some work and I need anyones advise.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>

using namespace std;

void drinks()
{
cout<<"1.coke 10.00"<<endl;
cout<<"2.sprite 10.00"<<endl;
cout<<"3.mountain dew 15.00";
}

void food()
{
cout<<"1.cake 15.00"<<endl;
cout<<"2.ice cream 12.00"<<endl;
cout<<"3.hamburger 20.00";
}


int main()
{
int mychoice;
int choice2;
int choice3;
int qty;
int bitm1=10,bitm3=15,fitm1=15,fitm2=12,fitm3=20;
int total=0,cost;
int more='y',cash,change;
itry:
drinks();
cout<<endl;
food();
cout<<endl;
cout<<"Select:";
cin>>mychoice;
if (mychoice==1){
system ("cls");
drinks();
cout<<endl;
cout<<"select:";
cin>>choice2;

if (choice2==1||choice2==2){
cout<<"amount:";
cin>>qty;
cost=bitm1*qty;
total=total+cost;
cout<<cost<<endl;
cout<<total<<endl;
}else if (choice2==1)
{
cout<<"amount:";
cin>>qty;
cost=bitm3*qty;
total=total+cost;
cout<<cost<<endl;
cout<<total<<endl;
}}else if (mychoice==2)

{
system ("cls");
food();
cout<<endl;
cout<<"select:";
cin>>choice3;

if (choice3==1)
{
cout<<"amount:";
cin>>qty;
cost=fitm1*qty;
total=total+cost;
cout<<cost<<endl;
cout<<total<<endl;
}else if (choice3==2)
{
cout<<"amount:";
cin>>qty;
cost=fitm2*qty;
total=total+cost;
cout<<cost<<endl;
cout<<total<<endl;
}else if (choice3==3)
{
cout<<"amount:";
cin>>qty;
cost=fitm3*qty;
total=total+cost;
cout<<cost<<endl;
cout<<total<<endl;
}}

cout<<"try again?(y/n)";
cin>>more;
if (more=='y'||more=='Y')
{
system ("cls");
goto itry;
}else if(more=='n'||more=='N');
{
cout<<"Total in cart is "<<total<<endl;
cout<<"Cash:";
cin>>cash;
change=cash-total;
cout<<change;
}
system ("pause");
return 0;
}


This part is the problem. It goes into an infinite loop after this part.
1
2
3
4
5
6
7
8
9
10
11
12
13
cout<<"try again?(y/n)";
cin>>more;
if (more=='y'||more=='Y')
{
system ("cls");
goto itry;
}else if(more=='n'||more=='N');
{
cout<<"Total in cart is "<<total<<endl;
cout<<"Cash:";
cin>>cash;
change=cash-total;
cout<<change;


Last edited on
Well before anyone yells at you...

http://cplusplus.com/forum/beginner/1988/

,And try not to use "goto" to often.


you said that this part id problem, but dint mention whats the problem in this part. Dont't expect random strangers to go an extra mile for you when you can not even post your question properly.
@king214

Correction... don't EVER use goto... there is absolutely NO reason you should EVER need it. If you DO need it, you're doing it wrong.
I think the use of the goto statement is still one of C/C++ ongoing debates.

However, when used by newcomers to C/C++ it is usually becsue they can't be arsed/lazy or don't know how to use loops properly.
I'm kinda new so I still can't use the loops properly so please tell me what I'm doing wrong.
closed account (zb0S216C)
Correction... don't EVER use goto... there is absolutely NO reason you should EVER need it. If you DO need it, you're doing it wrong.
loop: goto loop; has it's ups and downs but doesn't necessarily mean you're "doing it wrong". The loop: goto loop; has been deprecated in favour of the while( ), for( ) and do{ }while. You can use it, however, it's unstable.

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
67
68
69
70
71
// Just add a loop of your chose where 'itry' is.
while( true ) // This will loop forever unless you use the 'break' clause.
{
    drinks();
    cout<<endl;
    food();
    cout<<endl;
    cout<<"Select:";
    cin>>mychoice;
    if (mychoice==1)
    {
        system ("cls");
        drinks();
        cout<<endl;
        cout<<"select:";
        cin>>choice2;

        if (choice2==1||choice2==2)
        {
            cout<<"amount:";
            cin>>qty;
            cost=bitm1*qty;
            total=total+cost;
            cout<<cost<<endl;
            cout<<total<<endl;
            break;
        }
    }

    else if (mychoice==2)
    {
        system ("cls");
        food();
        cout<<endl;
        cout<<"select:";
        cin>>choice3;

        if (choice3==1)
        {
            cout<<"amount:";
            cin>>qty;
            cost=fitm1*qty;
            total=total+cost;
            cout<<cost<<endl;
            cout<<total<<endl;
            break;
        }

        else if (choice3==2)
        {
            cout<<"amount:";
            cin>>qty;
            cost=fitm2*qty;
            total=total+cost;
            cout<<cost<<endl;
            cout<<total<<endl;
            break;
        }

        else if (choice3==3)
        {
            cout<<"amount:";
            cin>>qty;
            cost=fitm3*qty;
            total=total+cost;
            cout<<cost<<endl;
            cout<<total<<endl;
            break;
        }
    }
}


Edited.
Last edited on
closed account (z05DSL3A)
Correction... don't EVER use goto... there is absolutely NO reason you should EVER need it.
Unless you want to do a unconditional jump to another part of the code.

If you DO need it, you're doing it wrong.
Unless you aren't, then you do and you should.

Read the following and make your own mind up, you shouldn't listen to anyone who says NEVER do X without giving good reason.
Code Complete: A Practical Handbook of Software Construction. -> 16.1 Using gotos
http://www.stevemcconnell.com/ccgoto.htm
Topic archived. No new replies allowed.