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:
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();
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;
}
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???????????
#include <cstdlib>
#include <iostream>
#include <cmath>
usingnamespace 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;
}
#include <cstdlib>
#include <iostream>
#include <cmath>
usingnamespace 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???????????
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.
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
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.