calculator loop

can you guys help me add on to this program by using loops to let the user enter however many numbers he or she wants to use. please help me with this and make it as simple as posible please.im not asking you to give me the code.im asking for help

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
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    double a, b, answer;
    int choice;
    char again;

    do{
    system("CLS");
    cout << "here are your options" << endl << endl;
    cout << "1.Addition" << endl;
    cout << "2.subtraction" << endl;
    cout << "3.multiplication" << endl;
    cout << "4.divison" << endl;
    cout << "5.average calculator" << endl;
    cout << "6.to quit" << endl;
    cin >> choice;


    switch(choice){
        case 1:
        cout << "enter two numbers you want to use(press enter after you enter each number): ";
        cout << endl;
        cin >> a >> b;
        answer = a + b;
        cout << "the answer is: " << a << " + " << b << " = " << answer << endl;
        break;
        case 2:
        cout << "enter two numbers you want to use(press enter after you enter each number): ";
        cout << endl;
        cin >> a >> b;
        answer = a - b;
        cout << "the answer is: " << a << " - " << b << " = " << answer << endl;
        break;
        case 3:
        cout << "enter two numbers you want to use(press enter after you enter each number): ";
        cout << endl;
        cin >> a >> b;
        answer = a * b;
        cout << "the answer is: " << a << " * " << b << " = " << answer << endl;
        break;
        case 4:
        cout << "enter two numbers you want to use(press enter after you enter each number): ";
        cout << endl;
        cin >> a >> b;
        answer = a / b;
        cout << "the answer is: " << a << " / " << b << " = " << answer << endl;
        break;
        case 5:
        cout << "please enter two numbers you want to find the average of" << endl;
        cout << "be sure to press enter after each number" << endl;
        cin >> a >> b;
        answer = (a + b) /2;
        cout << "the average of " << a << " and " << b << " is " << answer << endl;
        break;
        case 6:
        cout << "thanks for using this calculator!" << endl << endl;
        cin.get();
        return 0;
        break;

    }
    cout << "would you like to restart(y or n)";
    cin >> again;
    }while(again == 'y' ||again == 'Y');

    cin.get();
    return 0;
}
Ohk, here's what you can do,

1) Ask what the user wants to do, (add, subtract, etc etc)
2) Ask how many numbers she wants to do it with..
3) Store the number in variable (say) 'n'
4) Now declare a temp variable (say) 'temp' and 'temp1'
5) Initialize temp to 0, and temp1 to 1.
6) Suppose the users choice is add n numbers. You run a loop like this:

1
2
3
4
5
6
7
for (int i=1; i<=n; i++)
{
   cin>>num; 
   temp=temp+num;
}

cout<<"The sum of the "<<n<<" numbers is: "<<temp;


7)Use similar logic for all operations. Use temp1 for multiply and divide, and temp for add and subtract.
:)
Last edited on
than how do i declare all the variables.say the user wants to add 100 numbers . i dont have to declare a 100 variables right??how do i do that
and i tryed it and say i entered 5 it would just ask me how many i want to use again
Please post your new code so we can see what the problem with your loop is.
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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int operation;
    int howmany;
    int n;
    int answer;


    cout << "Here are your options: " << endl << endl;
    cout << "1. Addition" << endl;
    cout << "2. Subtraction" << endl;
    cout << "3. Multiplication" << endl;
    cout << "4. Division" << endl;
    cin >> operation;

    switch(operation){

        case 1:

        for (int i=1; i<=n; i++)

{
    cout << "Plese enter the amount of numbers you want to use";
    cin>>howmany;
    answer=answer+howmany;
}

cout<<"The sum of the "<<n<<" numbers is: "<<answer;


    }
    cin.get();
    return 0;

}
Last edited on
what did i do wrong
well i actually see a couple of things i didnt even let the user add what he wants
Yeah! The how many question is outside the loop... you store that in n, and then u start loop, and ask fr inputs... follow d algorithm proprly, n u wnt hav ne issues...
Topic archived. No new replies allowed.