Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
#include <math.h>

using namespace std;

class arith
{
	public:
			void tp();
			void ex();
			void in();
			void th();
			void m();
			void v();
			void x();
};

void arith::tp()
{
	int z;
	float x, y;
do
{
cout << endl
<< " 1 -> Addition + \n"
<< " 2 -> Subtraction - \n"
<< " 3 -> Multiplication * \n"
<< " 4 -> Division  / \n"
<< " 5 -> Squared x^2 \n"
<< " 6 -> Calculation 1/x \n"
<< " 7 -> Square Root \n"
<< " 8 -> Per Cent \n"
<< " 0 -> Exit \n"
<< " Give a choice: ";
cin >> z;
if(z=1)
{
	cout << "Give 1st num: ";
  	cin >> x;
  	
  	cout << "Give 2nd num: ";
  	cin >> y;
  	
  	cout << "Sum= " << x+y <<"\n\n";
}
if(z=2)
{
	cout << "Give 1st num: ";
  	cin >> x;
  	
  	cout << "Give 2nd num: ";
  	cin >> y;
  	
  	cout << "Difference= " << x-y <<"\n\n";
}
if(z=3)
{
	cout << "Give 1st num: ";
  	cin >> x;
  	
  	cout << "Give 2nd num: ";
  	cin >> y;
  	
  	cout << "Product= " << x*y <<"\n\n";
}
if(z=4)
{
	cout << "Give 1st num: ";
  	cin >> x;
  	
  	cout << "Give 2nd num: ";
  	cin >> y;
  	
  	cout << "Quotient= " << x/y <<"\n\n";
}
if(z=5)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout << "Squared= " << x*x <<"\n\n";
}
if(z=6)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	if(x>0)
  	{
  		cout << "Quotient= " << 1/x <<"\n\n";
	}
	
}
if(z=7)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout << "Square root= " << sqrt(x) <<"\n\n";
}
if(z=8)
{
	cout << "Give a num: ";
	cin >> x;
	
	cout << "Per cent= " << x*100 << "%" <<"\n\n";
}



}
while (z!=0);
}

void arith::ex()
{
	int r;
	float x, y, z;
do
{
cout << endl
<< " 1 -> Powers of y x^y \n"
<< " 2 -> Sine \n"
<< " 3 -> Cosine \n"
<< " 4 -> Tangent \n"
<< " 5 -> Ôhe powers of 10 \n"
<< " 6 -> Factorial Í \n"
<< " 0 -> Exit \n"
<< " Give a choice: ";
cin >> r;
if(r=1)
{
	cout << "Give 1st num: ";
  	cin >> x;
  	
  	cout << "Give 2nd num: ";
  	cin >> y;
  	
  	cout << "Result= " << pow(x, y) << "\n\n";
}
if(r=2)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout<<"sin("<<x<<")= "<<sin(x)<< "\n\n";
}
if(r=3)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout<<"cos("<<x<<")= "<<cos(x)<<"\n\n";
}
if(r=4)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout<<"tan("<<x<<")= "<<tan(x)<<"\n\n";
}
if(r=5)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout << "Result= " << pow(10, x) << "\n\n";
}
if(r=6)
{
	cout << "Give a num: ";
  	cin >> x;
  	z=x;
	while(x>1)
	{
		x--;
		z=z*x;
		
	}
	cout << "Result= " << z << "\n\n";
}


}
while (r!=0);
}

void arith::in()
{
	int s;
	float eu, dr, us;
do
{
cout << endl
<< " 1 -> Euro se Draxmes \n"
<< " 2 -> Draxmes se Euro \n"
<< " 3 -> Euro se USD \n"
<< " 4 -> USD se Euro \n"
<< " 0 -> Exit \n"
<< " Give a choice: ";
cin >> s;
if(s=1)
{
	cout << "Give Euro: ";
  	cin >> eu;
  	
  	cout << "Draxmes= " << eu*340.75 << "\n\n";
}
if(s=2)
{
	cout << "Give Draxmes: ";
  	cin >> dr;
  	
  	cout<<"Euro= "<<dr/340.75<< "\n\n";
}
if(s=3)
{
	cout << "Give Euro: ";
  	cin >> eu;
  	
  	cout<<"USD: "<<eu*1.20690<<"\n\n";
}
if(s=4)
{
	cout << "Give USD: ";
  	cin >> us;
  	
  	cout<<"Euro: "<<us*0.82840<<"\n\n";
}

}
while (s!=0);
}

void arith::th()
{
	int k;
	float c, f;
	
do
{
cout << endl
<< " 1 -> Celsius \n"
<< " 2 -> Fahrenheit \n"
<< " 0 -> Exit \n";
cin >> k;
if(k=1)
{
	cout << "Give Celsius: ";
  	cin >> c;
  	
  	cout << "Fahrenheit= " << (1.8*c)+32 << "\n\n";
}
if(k=2)
{
	cout << "Give Fahrenheit: ";
  	cin >> f;
  	
  	cout << "Celsius= " << (f-32)*5/9 << "\n\n";
}
}
while(k=!0);
}

void arith::m()
{
	
	float km;
	
	cout << "Give km: ";
	cin >> km;
	
	cout << "Meters: " << km*1000.0000 << "\n\n";
	cout << "Kilometers: " << km << "\n\n";
	cout << "Centimeters: " << km*100000.0000 << "\n\n";
	cout << "Millimeters: " << km*1000000.0000 << "\n\n";
	cout << "Levres: " << km*0.2071 << "\n\n";
	cout << "Foot: " << km*3280.00 << "\n\n";
	cout << "Mile: " << km*0.6214 << "\n\n";
	cout << "Nautical mile: " << km*0.5400 << "\n\n";
	cout << "Inch: " << km*39370.0000 << "\n\n";
	cout << "Yard: " << km*1093.6133 << "\n\n";
}

void arith::v()
{
	
	
	float kg;
	
	cout << "Give kg: ";
	cin >> kg;
	
	cout << "Grams: " << kg*1000.00 << "gr \n\n";
	cout << "Kilogram : " << kg << "kg \n\n";
	cout << "Milligram : " << kg*1000000.00 << "mg \n\n";
	cout << "Pounds: " << kg*2.20 << "lb \n\n";
	cout << "Ounces: " << kg*32.15 << "oz \n\n";



}

void arith::x()
{
	int x, h, m, s;
 
    cout << "Give seconds: ";
    cin >> x;
 
    h=x/3600;
    x=x%3600;
    m=x/60;
    x=x%60;
    s=x;
    
    cout<<"\n\nThe time in HH:MM:SS is: " <<h <<" hours " <<m <<" minutes  " <<s<<" seconds!\n";
}




int main()
{
int ep;

do
{
cout << endl
<< " 1 -> Tupikh Xrhsh \n"
<< " 2 -> Epistimonikh Xrhsh \n"
<< " 3 -> Isotimia Nomismatos \n"
<< " 4 -> Thermokrasia \n"
<< " 5 -> Mhkos \n"
<< " 6 -> Varos \n"
<< " 7 -> Xronos \n"
<< " 0 -> Exit \n"
<< " Dwse mia epilogh: ";
cin >> ep;

arith ar;

ar.tp();
ar.ex();
ar.in();
ar.th();
ar.m();
ar.v();
ar.x();

switch (ep)
{
case 1:
	
ar.tp();

break;


case 2:
ar.ex();
break;


case 3:
ar.in();
break;


case 4:
ar.th();
break;


case 5:
ar.m();
break;

case 6:
ar.v();
break;

case 7:
ar.x();
break;

case 0:
	
cout << "End of Program.\n";

break;


default:
cout << "Not a Valid Choice. \n"
<< "Choose again.\n";
break;
}

}
while (ep!=0);
return 0;
}



When selecting from the MAIN e.g. 2 or 3 etc. it only selects 1
that 1 is the first function and when I select something else there do only the first choice
what mistake have I made ???

What I have tried:

i dont know what i have do wrong
Posted
Updated 5-May-21 12:33pm

C++
if(z=1)
"=" is an assignment operator; "==" is a comparison operator.
And because C (on which C++ is based) did not have a boolean datatype, any non-zero value is taken as "true".
So your code:
C++
if(z=1)
Sets z to 1, decides that 1 is true and always executes the code block.

I'd also strongly recommend that you either use if ... else if ... else instead of if ... if ... if ... - or better look at a switch statement instead.

But ... thirty seconds with a debugger would have shown you what was happening for yourself, and saved you some time. It's well worth investing some effort in learning how to use the debugger while you are working on simple little programs like this: trying to debug a 100K line monster for the first time would be a horrible way to get started!
 
Share this answer
 
Comments
[no name] 5-May-21 17:12pm    
i do it but i have the same problem again
jeron1 5-May-21 17:38pm    
Did you change all the occurrences?
OriginalGriff 5-May-21 17:40pm    
Did you check every where you do something similar?
[no name] 5-May-21 17:42pm    
Yes
jeron1 5-May-21 17:50pm    
Update your original post, and add the newly changed code.
#include <iostream>
#include <math.h>


using namespace std;

class arith
{
	public:
			void tp();
			void ex();
			void ni();
			void th();
			void m();
			void v();
			void x();
};

void arith::tp()
{
	int z=1;
	float x, y;
do
{
cout << endl
<< " 1 -> Addition + \n"
<< " 2 -> Subtraction - \n"
<< " 3 -> Multiplication * \n"
<< " 4 -> Division  / \n"
<< " 5 -> Squared x^2 \n"
<< " 6 -> Calculation 1/x \n"
<< " 7 -> Square Root \n"
<< " 8 -> Per Cent \n"
<< " 0 -> Exit \n"
<< " Give a choice: ";
cin >> z;
if(z==1)
{
	cout << "Give 1st num: ";
  	cin >> x;
  	
  	cout << "Give 2nd num: ";
  	cin >> y;
  	
  	cout << "Sum= " << x+y <<"\n\n";
}
else if(z==2)
{
	cout << "Give 1st num: ";
  	cin >> x;
  	
  	cout << "Give 2nd num: ";
  	cin >> y;
  	
  	cout << "Difference= " << x-y <<"\n\n";
}
else if(z==3)
{
	cout << "Give 1st num: ";
  	cin >> x;
  	
  	cout << "Give 2nd num: ";
  	cin >> y;
  	
  	cout << "Product= " << x*y <<"\n\n";
}
else if(z==4)
{
	cout << "Give 1st num: ";
  	cin >> x;
  	
  	cout << "Give 2nd num: ";
  	cin >> y;
  	
  	cout << "Quotient= " << x/y <<"\n\n";
}
else if(z==5)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout << "Squared= " << x*x <<"\n\n";
}
else if(z==6)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	if(x>0)
  	{
  		cout << "Quotient= " << 1/x <<"\n\n";
	}
	
}
else if(z==7)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout << "Square root= " << sqrt(x) <<"\n\n";
}
else if(z==8)
{
	cout << "Give a num: ";
	cin >> x;
	
	cout << "Per cent= " << x*100 << "%" <<"\n\n";
}
}
while (z!=0);
}

void arith::ex()
{
	int r=1;
	float x, y, z;
do
{
cout << endl
<< " 1 -> Powers of y x^y \n"
<< " 2 -> Sine \n"
<< " 3 -> Cosine \n"
<< " 4 -> Tangent \n"
<< " 5 -> Ôhe powers of 10 \n"
<< " 6 -> Factorial Í \n"
<< " 0 -> Exit \n"
<< " Give a choice: ";
cin >> r;
if(r==1)
{
	cout << "Give 1st num: ";
  	cin >> x;
  	
  	cout << "Give 2nd num: ";
  	cin >> y;
  	
  	cout << "Result= " << pow(x, y) << "\n\n";
}
else if(r==2)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout<<"sin("<<x<<")= "<<sin(x)<< "\n\n";
}
else if(r==3)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout<<"cos("<<x<<")= "<<cos(x)<<"\n\n";
}
else if(r==4)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout<<"tan("<<x<<")= "<<tan(x)<<"\n\n";
}
else if(r==5)
{
	cout << "Give a num: ";
  	cin >> x;
  	
  	cout << "Result= " << pow(10, x) << "\n\n";
}
else if(r==6)
{
	cout << "Give a num: ";
  	cin >> x;
  	z=x;
	while(x>1)
	{
		x--;
		z=z*x;
		
	}
	cout << "Result= " << z << "\n\n";
}


}
while (r!=0);
}

void arith::ni()
{
	int s=1;
	float eu, dr, us;
do
{
cout << endl
<< " 1 -> Euro se Draxmes \n"
<< " 2 -> Draxmes se Euro \n"
<< " 3 -> Euro se USD \n"
<< " 4 -> USD se Euro \n"
<< " 0 -> Exit \n"
<< " Give a choice: ";
cin >> s;
if(s==1)
{
	cout << "Give Euro: ";
  	cin >> eu;
  	
  	cout << "Draxmes= " << eu*340.75 << "\n\n";
}
else if(s==2)
{
	cout << "Give Draxmes: ";
  	cin >> dr;
  	
  	cout<<"Euro= "<<dr/340.75<< "\n\n";
}
else if(s==3)
{
	cout << "Give Euro: ";
  	cin >> eu;
  	
  	cout<<"USD: "<<eu*1.20690<<"\n\n";
}
else if(s==4)
{
	cout << "Give USD: ";
  	cin >> us;
  	
  	cout<<"Euro: "<<us*0.82840<<"\n\n";
}

}
while (s!=0);
}

void arith::th()
{
	int k=1;
	float c, f;
	
do
{
cout << endl
<< " 1 -> Celsius \n"
<< " 2 -> Fahrenheit \n"
<< " 0 -> Exit \n";
cin >> k;
if(k==1)
{
	cout << "Give Celsius: ";
  	cin >> c;
  	
  	cout << "Fahrenheit= " << (1.8*c)+32 << "\n\n";
}
else if(k==2)
{
	cout << "Give Fahrenheit: ";
  	cin >> f;
  	
  	cout << "Celsius= " << (f-32)*5/9 << "\n\n";
}
}
while(k=!0);
}

void arith::m()
{
	
	float km;
	
	cout << "Give km: ";
	cin >> km;
	
	cout << "Meters: " << km*1000.0000 << "\n\n";
	cout << "Kilometers: " << km << "\n\n";
	cout << "Centimeters: " << km*100000.0000 << "\n\n";
	cout << "Millimeters: " << km*1000000.0000 << "\n\n";
	cout << "Levres: " << km*0.2071 << "\n\n";
	cout << "Foot: " << km*3280.00 << "\n\n";
	cout << "Mile: " << km*0.6214 << "\n\n";
	cout << "Nautical mile: " << km*0.5400 << "\n\n";
	cout << "Inch: " << km*39370.0000 << "\n\n";
	cout << "Yard: " << km*1093.6133 << "\n\n";
}

void arith::v()
{
	
	
	float kg;
	
	cout << "Give kg: ";
	cin >> kg;
	
	cout << "Grams: " << kg*1000.00 << "gr \n\n";
	cout << "Kilogram : " << kg << "kg \n\n";
	cout << "Milligram : " << kg*1000000.00 << "mg \n\n";
	cout << "Pounds: " << kg*2.20 << "lb \n\n";
	cout << "Ounces: " << kg*32.15 << "oz \n\n";



}

void arith::x()
{
	int x, h, m, s;
 
    cout << "Give seconds: ";
    cin >> x;
 
    h=x/3600;
    x=x%3600;
    m=x/60;
    x=x%60;
    s=x;
    
    cout<<"\n\nThe time in HH:MM:SS is: " <<h <<" hours " <<m <<" minutes  " <<s<<" seconds!\n";
}




int main()
{
int ep;

do
{
cout << endl
<< " 1 -> Tupikh Xrhsh \n"
<< " 2 -> Epistimonikh Xrhsh \n"
<< " 3 -> Isotimia Nomismatos \n"
<< " 4 -> Thermokrasia \n"
<< " 5 -> Mhkos \n"
<< " 6 -> Varos \n"
<< " 7 -> Xronos \n"
<< " 0 -> Exit \n"
<< " Dwse mia epilogh: ";
cin >> ep;

arith ar;


switch (ep)
{
case 1:
	
ar.tp();

break;


case 2:
ar.ex();
break;


case 3:
ar.ni();
break;


case 4:
ar.th();
break;


case 5:
ar.m();
break;

case 6:
ar.v();
break;

case 7:
ar.x();
break;

case 0:
	
cout << "End of Program.\n";

break;


default:
cout << "Not a Valid Choice. \n"
<< "Choose again.\n";
break;
}

}
while (ep!=0);
return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900