calculator

Jun 8, 2016 at 5:55pm
i am trying to make a simple calculator allow to the user to enter how many number her/she wants. it goes well from the out side but when it start the calculation it gives a wrong result to the numbers
i used these cods if you can help me please to know where is the mistake

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
#include "stdafx.h"
#include <iomanip> 
#include<iostream>
using namespace std;

int main()
{
	char operator_symbol, decision; 
	int loopcount = 0;
        int howmanynumbers;
        int number;
        int  total = 0; 
	
	decision = 'y';                      

	while (decision == 'y')
	
	{
		cout << "how many numbers you want to enter :"; 
																 
		cin >> howmanynumbers;     
		while (loopcount < howmanynumbers) {
			cout << "Enter a number" << endl;
			cin >> number;
			cout << " Enter operator";
			cin >> operator_symbol;

			loopcount = loopcount + 1;

		}


		switch (operator_symbol)                               
		{

		case '+':total = total+ number;
			break;                                                            

		case '-':total = total- number;
			break;                                                          

		case '*':total = total* number;
			break;                                                              

		case '/':total = total/ number;
			break;                                                             
		default:cout << "Wrong operator! Please retry" << endl; 
		                                                                

		}
		cout << "total= " << total << endl;       
	abc:cout << "Do another calculation(y/n):";
		cin >> decision;                                          

		if (decision == 'n')                                      
			break;

		else                                                       
continue;
    
	}
	cout << "Thank you. Good buy!" << endl;             
	return 0;                                                                 

}
Last edited on Jun 9, 2016 at 12:44pm
Jun 8, 2016 at 6:14pm
Please, use code tags (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your
post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <>
formatting button.
Jun 8, 2016 at 6:37pm
First off, you have a compile error.

Line 49: you have no statement associated with the else.

Then you have some logical errors:

Line 14-20: Your while loop continuously overlays number and operator_symbol.

Line 8: Don't use floats for index variables (loopcount). floats are approximations.

Poor style:
Line 41: Avoid the use of gotos.

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

int main()
{   char operator_symbol, decision; 

    float loopcount = 0, howmanynumbers, number, total = 0; 
    decision = 'y'; 

    while (decision == 'y')
    {   cout << "how many numbers you want to enter :"; 
        cin >> howmanynumbers; 
        while (loopcount < howmanynumbers) 
        {   cout << "Enter a number" << endl;
            cin >> number;
            cout << " Enter operator";
            cin >> operator_symbol;
            loopcount = loopcount + 1;
        }
        switch (operator_symbol) 
        {
        case '+':
            total = total+ number;
            break; 

        case '-':
            total = total- number;
            break; 

        case '*':
            total = total* number;
            break; 

        case '/':
            total = total/ number;
            break; 
        
        default:
            cout << "Wrong operator! Please retry" << endl; 
            goto abc; 
        }
        cout << "total= " << total << endl; 
abc:
        cout << "Do another calculation(y/n):";
        cin >> decision; 
        if (decision == 'n') 
            break;
        else 
    }
    cout << "Thank you. Good buy!" << endl; 
    return 0; 
}
Last edited on Jun 8, 2016 at 6:39pm
Jun 9, 2016 at 9:49am
AbstractionAnon
i did the changes in line 49 and 41.
in line 8 i replaced the floats with int
about the while loop i want to give the user the ability to enter numbers how many times he want but i don't know if i am doing it right.
that is the assignment
The calculator should be able to
add how many numbers at any time before choosing to get the total
Subtract the number of times before you decide to get the total
Multiply the number of times any time before operating one chooses to get the total
Add, subtract, multiply and / or divide the number of times before you decide to get the total
Clear the calculator so that you can start over
Topic archived. No new replies allowed.