New to C++

hello!

I'm new and i tough myself c++ and i need someone to give me his opinion about this code its simple code for calculator and what i should do to make it better:

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

void operation(){

char ope;
int num1=0;
int num2=0;
float result=0;
char controller;
do{
cout << "\nWelcome\nEnter Eqution Or first number: ";
cin >> num1;
cout << "\nEnter the operator: ";
cin >> ope;
cout << "Enter second number: ";
cin >> num2;
if(ope == '+')
result = num1 + num2;
else if(ope == '-')
result = num1 - num2;
else if(ope == '*')
result = num1 * num2;
else if(ope == '/')
result = num1 / num2;
else if (ope == '%')
result = num1 % num2;
else
{cout << "You fail";}
cout << '\n' << result << "\nThank you, Please continue\n";
cout << "Type any key and ENTER to continue or Q ENTER to quit ...:";
cin >> controller;

}while(controller != 'Q');
cout << "Thanks for using my basic calculator !! \n Goodbye :D\n";
system("pause");
//return 0;
}
int main()
{
int num;
int num2;
double num3;
float result=0;
char opt;
char choice;
for (;;){
do {
cout<<"Welcome to Heba's great Calculater\n";
cout<<"Please choose an option by entering the number, press q to quit\n";
cout<<"1 - Addition\n";
cout<<"2 - Subtraction\n";
cout<<"3 - Division\n";
cout<<"4 - Multiplication\n";
cout<<"5 - DISCOUNT%\n";
cout<<"6 - Modulus\n";
cout<<"7 - Multi numbers\n";
cin>>choice;
} while ( choice < '1' || choice > '7' && choice != 'q');
if (choice == 'q'|| choice == 'Q') break;
switch (choice) {
case '1':
cout<<"Please enter a number\n";
cin>>num;
cout<<"Another number to be added\n";
cin>>num2;
cout<<num + num2;
cout<<"\n";
cout<<"\n";
break;
case '2':
cout<<"Please enter a number\n";
cin>>num;
cout<<"Another number to be subtracted\n";
cin>>num2;
cout<<num - num2;
cout<<"\n";
cout<<"\n";
break;
case '3':
cout<<"Please enter a number\n";
cin>>num;
cout<<"Another one to be divided\n";
cin>>num2;
cout<<num / num2;
cout<<"\n";
cout<<"\n";
break;
case '4':
cout<<"Please enter a number\n";
cin>>num;
cout<<"Another one to be multiplied\n";
cin>>num2;
cout<<num * num2;
cout<<"\n";
cout<<"\n";
break;
case '5':
cout<<"Please enter a number\n";
cin>>num;
cout<<"How much discount\n";
cin>>num2;
cout << "The discount on " <<num << " : " << num - (num * num2/100) << endl;
cout<<"\n";
cout<<"\n";
break;
case '6'://i had to change the num from double to int
cout<<"Please enter a number\n";
cin >> num;
cout<<"Another number\n";
cin>>num2;
cout<<num % num2;
cout<<"\n";
cout<<"\n";
break;
case '7':
operation();

break;
default:
cout<<"That is not an option";

}

}
return 0;
}


please tell me, thank you
You seem to ask for the numbers in main(), then ask for them again in operation(). Why twice?
use switchinstead of if-else
i looked at it and i made it twice and i dont know why and i will use only switch case
thank you
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int num;
int num2;
double num3;
float result=0;
char opt;
char choice;
for (;;){
do {
cout<<"Welcome to Heba's great Calculater\n";
cout<<"Please choose an option by entering the number, press q to quit\n";
cout<<"1 - Addition\n";
cout<<"2 - Subtraction\n";
cout<<"3 - Division\n";
cout<<"4 - Multiplication\n";
cout<<"5 - DISCOUNT%\n";
cout<<"6 - Modulus\n";
cin>>choice;
} while ( choice < '1' || choice > '7' && choice != 'q');
if (choice == 'q'|| choice == 'Q') break;
switch (choice) {
case '1':
cout<<"Please enter an equation\n";
cin>>num;
//cout<<"Another number to be added\n";
cin>>num2;
cout<<num + num2;
cout<<"\n";
cout<<"\n";
break;
case '2':
cout<<"Please enter an equation\n";
cin>>num;
//cout<<"Another number to be subtracted\n";
cin>>num2;
cout<<num - num2;
cout<<"\n";
cout<<"\n";
break;
case '3':
cout<<"Please enter an equation\n";
cin>>num;
// cout<<"Another one to be divided\n";
cin>>num2;
cout<<num / num2;
cout<<"\n";
cout<<"\n";
break;
case '4':
cout<<"Please enter an equation\n";
cin>>num;
//cout<<"Another one to be multiplied\n";
cin>>num2;
cout<<num * num2;
cout<<"\n";
cout<<"\n";
break;
case '5':
cout<<"Please enter a number\n";
cin>>num;
cout<<"How much discount\n";
cin>>num2;
cout << "The discount on " <<num << " : " << num - (num * num2/100) << endl;
cout<<"\n";
cout<<"\n";
break;
case '6'://i had to change the num from double to int
cout<<"Please enter an equation\n";
cin >> num;
cout<<"Another number\n";
cin>>num2;
cout<<num % num2;
cout<<"\n";
cout<<"\n";
break;

default:
cout<<"That is not an option";
system ("pause");
return 0;
}

}
system ("pause");
return 0;
}


NOW I FIXED IT HOW ABOUT IT??????????????????
That seems ok.

Can you please use the code format tags on the right around your code please. It's near impossible to read unformatted code.
ok i will
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int num;
int num2;
double num3;
float result=0;
char opt;
char choice;
for (;;){
do {
cout<<"Welcome to Heba's great Calculater\n";
cout<<"Please choose an option by entering the number, press q to quit\n";
cout<<"1 - Addition\n";
cout<<"2 - Subtraction\n";
cout<<"3 - Division\n";
cout<<"4 - Multiplication\n";
cout<<"5 - DISCOUNT%\n";
cout<<"6 - Modulus\n";
cin>>choice;//the input given
} while ( choice < '1' || choice > '7' && choice != 'q');
if (choice == 'q'|| choice == 'Q') break;//to stop the .exe
switch (choice) {

case '1'://addition
cout<<"Please enter a NUMBER\n";
cin>>num;
cout<<"Another number to be added\n";
cin>>num2;
cout<<num + num2;
cout<<"\n";
cout<<"\n";
break;

case '2'://-
cout<<"Please enter a NUMBER\n";
cin>>num;
cout<<"Another number to be subtracted\n";
cin>>num2;
cout<<num - num2;
cout<<"\n";
cout<<"\n";
break;

case '3'://divid
cout<<"Please enter a NUMBER\n";
cin>>num;
cout<<"Another one to be divided\n";
cin>>num2;
cout<<num / num2;
cout<<"\n";
cout<<"\n";
break;

case '4'://multiply
cout<<"Please enter a NUMBER\n";
cin>>num;
cout<<"Another one to be multiplied\n";
cin>>num2;
cout<<num * num2;
cout<<"\n";
cout<<"\n";
break;

case '5': //for the bookshop i made it
cout<<"Please enter a number\n";
cin>>num;
cout<<"How much discount\n";
cin>>num2;
cout << "The discount on " <<num << " : " << num - (num * num2/100) << endl;
cout<<"\n";
cout<<"\n";
break;

case '6'://i had to change the num from double to int
cout<<"Please enter a NUMBER\n";
cin >> num;
cout<<"Another number\n";
cin>>num2;
cout<< num % num2 << endl;
cout<<"\n";
cout<<"\n";
break;



default:
cout<<"That is not an option";
system ("pause");//i want to see when it stops
return 0;
}

}
system ("pause");
return 0;
}


now i fixed it
tell me if i can add more. i dont know what to add???????????
use these code tags (<> icon) around your code :D


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

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int num;
int num2;
double num3;
float result=0;
char opt;
char choice;
for (;;){
do {
cout<<"Welcome to Heba's great Calculater\n";
cout<<"Please choose an option by entering the number, press q to quit\n";
cout<<"1 - Addition\n";
cout<<"2 - Subtraction\n";
cout<<"3 - Division\n";
cout<<"4 - Multiplication\n";
cout<<"5 - DISCOUNT%\n";
cout<<"6 - Modulus\n";
cin>>choice;//the input given
} while ( choice < '1' || choice > '7' && choice != 'q');
if (choice == 'q'|| choice == 'Q') break;//to stop the .exe
switch (choice) {

case '1'://addition
cout<<"Please enter a NUMBER\n";
cin>>num;
cout<<"Another number to be added\n";
cin>>num2;
cout<<num + num2;
cout<<"\n";
cout<<"\n";
break;

case '2'://-
cout<<"Please enter a NUMBER\n";
cin>>num;
cout<<"Another number to be subtracted\n";
cin>>num2;
cout<<num - num2;
cout<<"\n";
cout<<"\n";
break;

case '3'://divid
cout<<"Please enter a NUMBER\n";
cin>>num;
cout<<"Another one to be divided\n";
cin>>num2;
cout<<num / num2;
cout<<"\n";
cout<<"\n";
break;

case '4'://multiply
cout<<"Please enter a NUMBER\n";
cin>>num;
cout<<"Another one to be multiplied\n";
cin>>num2;
cout<<num * num2;
cout<<"\n";
cout<<"\n";
break;

case '5': //for the bookshop i made it
cout<<"Please enter a number\n";
cin>>num;
cout<<"How much discount\n";
cin>>num2;
cout << "The discount on " <<num << " : " << num - (num * num2/100) << endl;
cout<<"\n";
cout<<"\n";
break;

case '6'://i had to change the num from double to int
cout<<"Please enter a NUMBER\n";
cin >> num;
cout<<"Another number\n";
cin>>num2;
cout<< num % num2 << endl;
cout<<"\n";
cout<<"\n";
break;



default:
cout<<"That is not an option";
system ("pause");//i want to see when it stops
return 0;
}

}
system ("pause");
return 0;
}




Last edited on
sooooooooooooooooooory
i didnt understand
hehehehehe idiot i am
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
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

#define PI 3.14159265

int main()
{
    double num;
    double num2;
	int nu;
	int nu2;
	char opt;
    char choice;
    for (;;){
     do {
    cout<<"Please choose an option by entering the number, press q to quit\n";
    cout<<"1 - Addition\n";
    cout<<"2 - Subtraction\n";
    cout<<"3 - Division\n";
    cout<<"4 - Multiplication\n";
    cout<<"5 - DISCOUNT%\n";
    cout<<"6 - Modulus\n";
	cout<<"7 - Sine\n";
    
	cin>>choice;

    } while ( choice < '1' || choice > '7' && choice != 'q');
    if (choice == 'q'|| choice == 'Q') break;//to stop the .exe
    switch (choice) {
           
			case '1'://addition
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another number to be added\n";
                cin>>num2;
                cout<<num + num2;
                cout<<"\n";
				cout<<"\n";
                break;
           case '2'://subtract
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another number to be subtracted\n";
                cin>>num2;
                cout<<num - num2;
                cout<<"\n";
				cout<<"\n";
                break;
           case '3'://divid
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another one to be divided\n";
                cin>>num2;
                cout<<num / num2;
                cout<<"\n";
				cout<<"\n";
                break;
           case '4'://multiply
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another one to be multiplied\n";
                cin>>num2;
                cout<<num * num2;
                cout<<"\n";
				cout<<"\n";
                break;
           case '5':          //for the bookshop i made it      
			    cout<<"Please enter a number\n";
                cin>>num;
                cout<<"How much discount\n";
                cin>>num2;
				cout << "The discount on " <<num << " : " << num - (num * num2/100) << endl;
                cout<<"\n";
				cout<<"\n";
                break;
           case '6'://i had to change the num from double to int
			   //now i made new intergers value so that i wont 
			   //harm my discount (case 5)
				cout<<"Please enter a number\n";
                cin >> nu;
				cout<<"Another number\n";
                cin>>nu2;
				cout<< nu % nu2 << endl;
                cout<<"\n";
				cout<<"\n";
                break;
		   case '7':
				cout <<"please enter a number";
				cin>>num;
				cout<<"The sine of " << num << " degrees is " << sin (num*PI/180) <<endl;
                cout<<"\n";
				cout<<"\n";				
				break;

           default:
                    cout<<"That is not an option";
                    system ("pause");//i want to see when it stops
					return 0;
                }
}

system ("pause");
return 0;

}


i'm slowly learning hehehheheh
now what you think???????????
if (choice == 'q'|| choice == 'Q') break;//to stop the .exe

Change to
 
if (choice == 'q'|| choice == 'Q') return 0; //some use return 1; 



Why do you have this twice?
1
2
3
4
5

system ("pause");//i want to see when it stops
					return 0;



Remove the default case and what's inside.

Concerning your for (;;), think of another way of combining this with the if statement that checks when to exit the program using while or do while.
Last edited on
@Alobaidan

I think you would benefit from splitting your code into functions such as these:

ShowMenu
ProcessMenu -call a function for each menu option
GetNumber
GetOperator
IsOperator
DegreesToRadians
CalculateAnswer


Also the basic design is this:

Get a Number
Get an Operator
Get a number
Calculate answer


Some other problems:



You can use M_PI in the maths library, instead of the #define

You should check for divide by zero. Google floating point and DBL_EPSILON to see how to do this, -it's not as easy as it looks.

Your code will be easier to understand if you use meaningful variable names, not single letters or abbreviations.

Consider using a bool variable like Quit in a while loop, to make the test expression easier.

You should not have a endless for loop with a do-while inside, just have a while loop instead.

HTH


be aware of your levels and develop a good coding style. Use tabs in descending expressions it can be difficult to tell what code runs inside of what code if they all begin in the same horizontal level.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
		  case '7':
				cout <<"please enter a number"<<endl;
				cin>>num;
				cout <<"Sin or Cos?"<< endl;
				cin >>input;
				if (input == 'sin')
					cout<<"The sine of " << num << " degrees is " << sin (num*PI/180) <<endl;
				else {
					cout<<"The cosine of " << num << " degrees is " << cos (num*PI/180)<<endl;
					}
				return 0;

				cout<<"\n";
				cout<<"\n";				
				break;




i added this part it didnt work why and when i put case 8 as well it doesn't work why??????is it becos of the for(;;) statment!!!!!!:s
Did you declare & initialise the input variable?

How did you go with my suggestions above?
I will try again, look I'm little bit newbie about things as u remember i'm a beginner i'm old but i try my best its all for me.
give me time and i will get better and i will show you what i have done.

thank you very much for all the replays



:D i'm happy
I think it's fine as is. There are endless ways in which the code can be refactored, but it's nice and consistent as is.
Last edited on
Topic archived. No new replies allowed.