Calculator v. 1.6

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
#include <iostream.h>
#include <cstdlib>
int main ()
{
 system("color 3f");
 int choice1;
 int choice2;
 int choice3;
 int result;
 for (;;) {
 do {
 
 cout << "\n";
 
 cout << "-----------------------------\n";
 cout << "|     Calculator v. 1.6     |\n";
 cout << "|   1 for plus              |\n";
 cout << "|   2 for minus             |\n";
 cout << "|   3 for divide            |\n";
 cout << "|   4 for multiplication    |\n";
 cout << "|   5 to quit               |\n";
 cout << "-----------------------------\n";
 cin >> choice1;
 } while (choice1 == '1' &&choice1 == '2' &&choice1 == '3' &&choice1 == '4' &&choice1 != '5');
  if (choice1 == '5') break;
  
  switch (choice1) {
  
   case 1:
   
    cout << "Plus. Enter two numbers: \t";
	cin >> choice2;
	cin >> choice3;
	
	result = choice2 + choice3;
	
	cout << "result: \t" << result << "\n";
	
	break;
	
   case 2:
   
    cout << "Minus. Enter two numbers: \t";
	cin >> choice2;
	cin >> choice3;
	
	result = choice2 - choice3;
	
	cout << "result: \t" << result << "\n";
	
	break;
	
   case 3:
   
    cout << "Divide. Enter two numbers: \t";
	cin >> choice2;
	cin >> choice3;
	
	result = choice2 / choice3;
	
	cout << "result: \t" << result << "\n";
	
	break;
	
   case 4:
   
    cout << "Multiplication. Enter two numbers: \t";
	cin >> choice2;
	cin >> choice3;
	
	result = choice2 * choice3;
	
	cout << "result: \t" << result << "\n";
	
	break;
	
   case 5:
   
    cout << "Quiting Calculator v. 1.6 . . .";
	
	return 0;
	
   default:
   
   cout << "That is not a valid option.\n";
   
  
   
}

}

return 0;

}


It took me 1 hour to program, what do you guys think? Any bug?
Consider the while statement. Do some calculation, then you will realise you should change AND && symbols to OR...
I did consider the while statment, but I dont know where exactly
to put it. [second sentence] I dont understand what do you mean by that.
The do-while loop is not functioning.
Try commenting-out (using //) lines 11 and 24. The behaviour of the program is unchanged.

The if statement at line 25 is non-functioning. Again, comment it out and the behaviour of the program is unchanged.

The above problems should be corrected, or that code deleted.

A general comment, there is a lot of repeated code within the switch-case. You might consider ways of having the identical code appear just once. In this case it isn't too important, but it is an interesting exercise.
Sorry, my fault. I skimmed too fast. :)
But, please remove the construct do...while. In my opinion I see it's redundant...

And what's wrong?

EDIT : I agree with Chervil. Your program needs some improvements.

Last edited on
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
#include <iostream> // not <iostream.h>

int main()
{
    const char PLUS = '+', MINUS = '-', MULTIPLY = '*', DIVIDE = '/', QUIT = 'q' ;

    while( true )
    {
        char choice ;
        do
        {
            std::cout << "\n"
                          "-----------------------------\n"
                          "|     Calculator v. 1.6     |\n"
                          "|   + for plus              |\n"
                          "|   - for minus             |\n"
                          "|   / for divide            |\n"
                          "|   * for multiply          |\n"
                          "|   q to quit               |\n"
                          "-----------------------------\n" ;
        } while( std::cout << "choice? " && std::cin >> choice && choice != PLUS
                  && choice != MINUS && choice != DIVIDE && choice != MULTIPLY && choice != QUIT  ) ;

        if( choice == QUIT ) break ;

        std::cout << '\'' << choice << "' enter two numbers: " ;
        int first, second ;
        std::cin >> first >> second ;

        std::cout << "result: " << first << ' ' << choice << ' ' << second << " == " ;

        if( choice == DIVIDE )
        {
                if( second == 0 ) std::cout << "can't divide by zero\n" ;
                else std::cout << double(first) / second << '\n' ;
        }
        else
        {
            int result = 0 ;
            switch( choice )
            {
                case PLUS : result = first + second ; break ;
                case MINUS : result = first - second ; break ;
                case MULTIPLY : result = first * second ; break ;
            }
            std::cout << result << '\n' ;
        }
    }
}
ok im not that good at C++
Don't take it to heart, it takes time. And I think we all find it useful to see how another programmer tackles the same task.
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
    

while( true ) {

    char choice ;
    std::cout << "\n"
                  "-----------------------------\n"
                  "|     Calculator v. 1.6     |\n"
                  "|   + for plus              |\n"
                  "|   - for minus             |\n"
                  "|   / for divide            |\n"
                  "|   * for multiply          |\n"
                  "|   q to quit               |\n"
                  "-----------------------------\n" ;       

    std::cin >> choice;
   
    if (choice == QUIT) 
        break;
    
    else if (choice != PLUS && choice != MINUS && choice != DIVIDE && choice != MULTIPLY) {
        std::cout << "invalid option" << endl;
        continue;  //takes you back to the start of the while loop
    } 
  


Here's another way you could go about it.
Topic archived. No new replies allowed.