help with calculator

can u guys please post an example of a calculator that allows you to enter how many numbers you want to add...i just wanna see cause im learning about loops in my book now..heres what i can do so far...plus i can make a calculator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int numberofloops;
    cout << "how many loops do you want to use?" << endl;
    cin >> numberofloops;

    for(; numberofloops > 0 ;)
    {
        numberofloops = numberofloops - 1;
        cout << "only " << numberofloops << " loops to go! " << endl;
}
system("pause");
return 0;
}
i started here is what i have so far but i dont know what to do next?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int howmanynum;
    int anum;
    int sum;
    
    cout << "enter howmany numbers you want to use: ";
    cin >> howmanynum;
    for(; howmanynum > 0 ;)
    {
        howmanynum = howmanynum - 1;
        cout << "enter a number you want to add"
        cin >> anum;
    }
    
system("pause");
return 0;
}
Last edited on
Something like this?

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
#include <iostream>

using namespace std;

int main()
{
	int Total = 0; 
	int Amount;
	int Number;

	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount 
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering 
		cin >> Number;

		Total += Number;  // Adds number to total 
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished 

	system("PAUSE"); 
	return 0;
}


When using a for loop you usually set a beginning value (in this case int i = 0) then compare it to another value (i < Amount), then increment or decrement the initial value if the middle condition isn't met.

Another way you could do it is just keep adding numbers until the user entered 0 (or some other number) using a do-while loop.

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
#include <iostream>

using namespace std;

int main()
{
	int Total = 0; 
	int i = 0;
	int Number;

	cout << "Enter in 0 to finish" << endl; 

	do
	{
		cout << "Number " << i << ": " ;
		cin >> Number;

		i++; 
		Total += Number;
	}	
	while(Number != 0);

	cout << "The total is " << Total << endl; 

	system("PAUSE"); 
	return 0;
}
Last edited on
How bout instead of using a loop that determines how many inputs, just use a couple cleverly used variables that hold current number, a placeholder, and a current answer variable. Use a case statement for the operator selection. And you can loop through this until the user decides to quit :) Hope this helps. I'm an my iPhone so I can't really post an example
im starting to get it but on the first one what is int i? you didnt even declare it as a variable
int i is declared inside the for loop, it can be called whatever you want...it is just to check when the loop will be completed.
ok and can you please explain to me why this doesnt work?? -,* and / dont work...only + works....i switched Total = Number + Total to Total = Number * Total

here is my code

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int Total = 0;
	int Amount;
	int Number;
	int whatoperation;

	cout << "Here are your choices(enter the number before the operation)" << endl << endl;
	cout << "1.Addition" << endl;
	cout << "2.Subtraction" << endl;
	cout << "3.Multiplication" << endl;
	cout << "4.Division" << endl;

	cin >> whatoperation;

	switch(whatoperation){
	    case 1:
    cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number + Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 2:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number * Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 3:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number / Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 4:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number + Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;
	}
	system("pause");
	return 0;
}
wait i messed up the operations...but it still doesnt work??whats wrong

heres the updated version

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int Total = 0;
	int Amount;
	int Number;
	int whatoperation;

	cout << "Here are your choices(enter the number before the operation)" << endl << endl;
	cout << "1.Addition" << endl;
	cout << "2.Subtraction" << endl;
	cout << "3.Multiplication" << endl;
	cout << "4.Division" << endl;

	cin >> whatoperation;

	switch(whatoperation){
    case 1:
    cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number + Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 2:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number - Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 3:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number * Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 4:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number / Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;
	}
	system("pause");
	return 0;
}
Please answer
Your subtraction case isn't working correctly because you are subtracting the total from the number each time, when it is meant to be the other way around. At line 46 change it to
Total = Total - Number;

(You should also fix this for the other cases)

When looking at the multiplication and division cases for a while you should be able to see why they aren't working correctly. The total is starting at 0 and then is multiplied by whatever number the user enters, which is always going to result in an answer of 0. There are probably better ways to solve this (involving arrays or vectors) but just to stay with simple loops for now. Change your multiplication case to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ; 
		cin >> Number;

		if(i == 0)
		{
			Total = Total + Number; 
		}

		else
		{
			Total = Total * Number;
		}
	}


This makes it so the first number the user enters in the loop is simply added to the total, then everything after that is multiplied together (solves the 0 total problem). Pretty much the same thing should be done for division. The final thing you will need to do is change your Total variable to a float so the division will work correctly (it will only print out whole numbers with int).

Hopefully all of that makes sense after you try it for a while.
ok i did it ...thanks for the help....heres the code tell me what u think and any thing that will make it better

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int Total = 0;
	int Amount;
	int Number;
	int whatoperation;
	int restart;

	loop:
    system("CLS");

	cout << "Here are your choices(enter the number before the operation)" << endl << endl;
	cout << "1.Addition" << endl;
	cout << "2.Subtraction" << endl;
	cout << "3.Multiplication" << endl;
	cout << "4.Division" << endl;

	cin >> whatoperation;

	switch(whatoperation){
    case 1:
    cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number + Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 2:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;
		cin >> Number;

		if(i == 0)
		{
			Total = Total + Number;
		}

		else
		{
			Total = Total - Number;
		}
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 3:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;
		cin >> Number;

		if(i == 0)
		{
			Total = Total + Number;
		}

		else
		{
			Total = Total * Number;
		}
	}
	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 4:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;
		cin >> Number;

		if(i == 0)
		{
			Total = Total + Number;
		}

		else
		{
			Total = Total / Number;
		}
		cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	}
	break;
	}
	cout << "enter 1 to restart or 2 to quit: ";
	cin >> restart;
	if(restart == 1)
	    goto loop;

    system("pause");
	return 0;
}
can i please have feedback
i just added a average calculator

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
128
129
130
131
132
133
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int Total = 0;
	int Amount;
	int Number;
	int whatoperation;
	int answertoaverage;
	int restart;

	loop:
    system("CLS");

	cout << "Here are your choices(enter the number before the operation)" << endl << endl;
	cout << "1.Addition" << endl;
	cout << "2.Subtraction" << endl;
	cout << "3.Multiplication" << endl;
	cout << "4.Division" << endl;
	cout << "5.Average Calculator" << endl;

	cin >> whatoperation;

	system("CLS");

	switch(whatoperation){
    case 1:
    cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number + Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 2:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;
		cin >> Number;

		if(i == 0)
		{
			Total = Total + Number;
		}

		else
		{
			Total = Total - Number;
		}
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 3:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;
		cin >> Number;

		if(i == 0)
		{
			Total = Total + Number;
		}

		else
		{
			Total = Total * Number;
		}
	}
	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	break;

	case 4:
	cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;
		cin >> Number;

		if(i == 0)
		{
			Total = Total + Number;
		}

		else
		{
			Total = Total / Number;
		}
		cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	}
	break;
	case 5:
	cout << "Enter how many numbers you want to find the average of: ";
	cin >> Amount;

	for(int i = 0; i < Amount; i++)
	{
	    cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number + Total;  // Adds number to total
	}
	answertoaverage = Total / Amount;
	cout << "The average of those " << Amount << " numbers is " << answertoaverage << endl;
}
	cout << "enter 1 to restart or 2 to quit: ";
	cin >> restart;
	if(restart == 1){
	    goto loop;
	    }

    system("pause");
	return 0;
}
I just skimmed through it, I'm on my phone so I can't look to closely at it. But, first of all don't use system() for anything. It's not portable, it's slow, and it's a security risk. Second, you have a lot of repeating code. See if you can find it and then see what you can make functions of it. Functions are used so you don't have to repeat any code.

Another thing, you want a default case for your switch statement. And also, see of you can think of/find a way to handle input validation. What if instead of 1,2,3,4, or 5, I type in Q? Your program crashes. I know these might be kind of advanced topics for you currently, but they're not hard and it's something you should at least be thinking about.
for(; numberofloops > 0 ;)
nice! Thanks! that's a awesome way
Topic archived. No new replies allowed.