Change in the code

Hello I've made simple calculator but it has got a small bug.
everything is fine until I enter (s) (c) or (t) sin cos or tan
it shows me to enter the first number,
after that it expect to enter the second number (it doesn't matter what you will
enter ) it shows the right result but I want to get rid of entering the second number without any purpose. By the way I use code::blocks as platform.
Thank you in advance!!!

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
 # include <iostream>
# include <math.h>
# include <windows.h>
using namespace std;
int main(){
double parwocislo, vtorocislo;
double resultat;
char znak;
int type;
while( true ){
        cout << "Do you want to convert measurments or use calc.?\nfor calc pres 1\nfor convert use 2.\n";
        cin >> type;
    if( type == 2 ){
        cout << "You have chosen measurment convertor\nplease enter your number in inches to make it in cm\n";
        cin >> parwocislo;
        resultat = parwocislo / 2.54;
        cout << "Result is: " << resultat << endl << endl;
    }
    if( type == 1 ){
        cout << "Operations, you can use:(/) (*) (-) (+) (^) (~ square root) (%) (# any other root) (s) (c) (t),\n if you have chosen (s) (c) (t) use first number as an angle in degrees";
        cout << endl;
        cout << "Please enter your first number: ";
        cout << endl;
        cin >> parwocislo;
        cout << "Please enter your operation: ";
        cout << endl;
        cin >> znak;
        if( znak == '~' ){
            resultat = sqrt( parwocislo );
            cout << "Result is:" << resultat << endl << endl;
        }
        else{
            if( znak == 's' ){
                resultat = sin( parwocislo * 22 / 7 / 180 );
            }
            if( znak == 'c' ){
                resultat = cos( parwocislo * 22 / 7 / 180 );
            }
            if( znak == 't' ){
                resultat = tan( parwocislo * 22 / 7 / 180 );
            }
            cout << "Please enter your second number\nif you have chosen'#'for operation enter your root power e.g.3,4,5...\n";
            cin >> vtorocislo;
            if( znak == '#' ){
                resultat = pow( parwocislo, 1 / vtorocislo );
            }
            if( znak == '+' ){
                resultat = parwocislo + vtorocislo;
            }
            if( znak == '-' ){
                resultat = parwocislo - vtorocislo;
            }
            if( znak == '/' ){
                resultat = parwocislo / vtorocislo;
            }
            if( znak == '*' ){
                resultat = parwocislo * vtorocislo;
            }
            if( znak == '^' ){
                resultat = pow( parwocislo, vtorocislo );
            }
            if( znak == '%' ){
                resultat = ( parwocislo / vtorocislo ) * 100;
                cout << " Result is: " << resultat << "%\n\n";
            }
            else{
                cout << " Result is: " << resultat << "\n\n";
            }
        }
    }
}
return 0;
}
[code]
[/code]
You could check what the operation is and only display the prompt for the second number in the appropriate cases.

With a lot of options, I might use a switch statement instead of a lot of ifs. And then just have one statement to output the result of the conversion or calculation at the end.

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
if (znak != '~' && znak != 's' && znak != 'c' && znak != 't')
{
	cout << "Please enter your second number\nif you have chosen'#'for operation enter your root power e.g.3,4,5...\n";
	cin >> vtorocislo;
}
switch(znak) 
{
        case '~':	resultat = sqrt( parwocislo );
        		break;
	case 's':	resultat = sin( parwocislo * 22 / 7 / 180 );
       			break;
	case 'c':	resultat = cos( parwocislo * 22 / 7 / 180 );
			break;
 	case 't':	resultat = tan( parwocislo * 22 / 7 / 180 );
 			break;
	case '#': 	resultat = pow( parwocislo, 1 / vtorocislo );
			break;
        case '+':	resultat = parwocislo + vtorocislo;
            		break;
        case '-':	resultat = parwocislo - vtorocislo;
            		break;
        case '/': 	resultat = parwocislo / vtorocislo;
            		break;
        case '*':	resultat = parwocislo * vtorocislo;
            		break;
        case '^':	resultat = pow( parwocislo, vtorocislo );
            		break;
        case '%':	resultat = ( parwocislo / vtorocislo ) * 100;
            		break;
 	default:	cout << "couldn't read operation";
 			break;
}//end switch
        
cout << " Result is: " << resultat << "\n\n";
}
Thank you! That solved my problem
:)
Topic archived. No new replies allowed.