do while loop with case

how can i loop this case, and prompt the user for for everything again, i should do more case but i can do that later i first need to know how to loop it

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  #include <iostream>

using namespace std;

int main()
{



    double answer,values[2];
    int count=1,method;
    string respond;

    cout <<"   +-+-+-+-+-+-+-+-+-+-+-+-+-+  "<<endl;
    cout <<"   +  SIMPLE CALCUTOR MENU   +  "<<endl;
    cout <<"   +++++++++++++++++++++++++++  "<<endl;
    cout <<"   +  SELECT 1  TO ADD       +  "<<endl;
    cout <<"   +  SELECT 2  TO SUBTRACT  +  "<<endl;
    cout <<"   +  SELECT 3  TO MULTIPLY  +  "<<endl;
    cout <<"   +  SELECT 4  TO DIVDLE    +  "<<endl;
    cout <<"   +  SELECT 5  TO MODULUS   +  "<<endl;
    cout <<"   +++++++++++++++++++++++++++\n"<<endl;

    cout <<"NB. THIS CALCUTOR CAN ONLY ACCEPT TWO VALUES\n"<<endl;





do
{



    cout<<"PLEASE SELECT AN OPERATION FROM MENU"<<endl;
    cin>>method;

    if (method>5 or method<1)
        {
        cout<<"ERROR: NOT VALID"<<endl;
        cout<<"ENTER VALID NUMBER FROM MENU"<<endl;

        }


        switch (method)
    {
    case 1:
        cout<<"YOU HAVE CHOSEN TO ADD"<<endl;
        while(count<=2)
    {

        cout<<"ENTER VALUE "<<count<<endl;
        cin>>values[count];
        count++;
    }
    answer=values[1]+values[2];
    cout <<"RESULT : "<<answer<<endl;
    cout <<"DO YOU WISH TO DO ANOTHER OPERATION ? "<<endl;
    cout <<"Y for Yes"<<endl;
    cout <<"N for No"<<endl;
    cin>>respond;

    break;

    case 2:
        cout<<"YOU HAVE CHOSEN TO SUBTRACT"<<endl;
        while(count<=2)
    {

        cout<<"FORMAT: VALUE 1 - VALUE 2 = DIFFERANCE"<<endl;
        cout<<"ENTER VALUE "<<count<<endl;
        cin>>values[count];
        count++;
    }
    answer=values[1]-values[2];
    cout <<"RESULT : "<<answer<<endl;
    cout <<"DO YOU WISH TO DO ANOTHER OPERATION ? "<<endl;
    cout <<"Y for Yes"<<endl;
    cout <<"N for No"<<endl;
    cin>>respond;

    break;

    case 3:
        cout<<"YOU CHOSEN TO MULTIPLY"<<endl;
        while(count<=2)
    {

        cout<<"ENTER VALUE "<<count<<endl;
        cin>>values[count];
        count++;
    }
    answer=values[1]*values[2];
    cout <<"RESULT : "<<answer<<endl;
    cout <<"DO YOU WISH TO DO ANOTHER OPERATION ? "<<endl;
    cout <<"Y for Yes"<<endl;
    cout <<"N for No"<<endl;
    cin>>respond;

    break;

    case 4:
         while(count<=2)
    {
        cout<<"FORMAT: VALUE 1 (DIVIDEND)/ VALUE 2 (DIVISOR)"<<endl;
        cout<<"ENTER VALUE "<<count<<endl;
        cin>>values[count];
        count++;
    }
    answer=values[1]/values[2];
    cout <<"RESULT : "<<answer<<endl;
    cout <<"DO YOU WISH TO DO ANOTHER OPERATION ? "<<endl;
    cout <<"Y for Yes"<<endl;
    cout <<"N for No"<<endl;
    cin>>respond;

    break;
    }



    }while(respond=="y");


return 0;
}


This is where the loop comes in, the program should ask the user to decide whether he/she wants to continue the operation. If he/she input "y", the program will prompt the user to choose the operation again. otherwise the program will terminate. i'm beginner so i dont understand anything advance
Last edited on
Hi have a look at this:

http://www.cplusplus.com/forum/beginner/99203/#msg534078


Notice there isn't code duplication.

Also, when doing division, always check for division by zero.

Good Luck !!
I have put your program into my compiler. There was one error:

if (method>5 or method<1)
needed to be changed to
if (method>5 || method<1)

Ignore this. My compiler puked with this, but it is legal.
The code seems to work just fine. Is it that you are having trouble understanding the code? You have working code.

You should probably have a default in your switch, just in case the user enters something unexpected (I entered 'N' and it went into an infinite loop).

If you want to loop inside any of the cases, just add a loop inside any of the case statements.
Last edited on
When i type in yes to continue , it loops but instead of prompting for new values it uses the result and the value 1 automatically, and thanks TheIdeasMan @TheIdeasMan
Last edited on
@JayZom

That's not an error. or is a keyword and has exactly the same meaning as ||

There are others and xor etc.

http://en.cppreference.com/w/cpp/language/operator_alternative


The OP has working code, but it is full of repetition.
i tried to fix it by looking at the code,but i dont really understand it completely. I i could do was to remove the duplication. What is really want to know is how to make the user enter new values instead of using result.
Last edited on
@TheIdeasMan My apologies. My compiler does not recognize the keyword, so I myself had to change it to make it work (VS 2015), but I have used the alternative operators before.
I know of alternative operators.
@JayZom

No need for apologies, it's all good :+)

I would recommend getting g++ and clang ,and a different IDE, so you can compile standard compliant code. I thought VS had finally come up to date with the standard, but apparently not.

@Tom

What didn't you understand?

Do you know how to use functions yet? If so, it's a good idea to have each case call a function to do it's work.

With a function, you can get the user to enter 2 values, once only - that's all you need for all 4 functions (+-*/)

Last edited on
Topic archived. No new replies allowed.