DID I DO THIS PROGRAM RIGHT
#include <iostream>
using namespace std;
class distance
{
private:
int feet;
float inches;
public:
distance() : feet(0);inches(0,0)
{}
distance(int ft, float in) : feet(ft), inches(in)
void get_dist()
{
cout<<"\n Enter feet : ";cinfeet;
cout<<"Enterinches";cin>>inches;
}
void showdist()const
{cout<<feet<<"<<inches<<";}
void operator += (Distance);
};
void operator :: operator +=(distance d2)
{
feet += d2.feet;
inches += d2.inches;
if(inches)=12.0;
{inches-=12.0;
feet ++;
}
}
int main ( )
{
distance dist1;
dist1.getdist();
cout<<"\n dist2="; dist1.showdist();
distance dist2 (11,6.25);
cout<<"\n dist2=";dist2.showdist();
dist1 += dist2;();
cout<<"\n after addion",;
cout<<"\n dist1=";dist1.showdist;
cout<<endl;
return 0;
}



can i please have some help on this program..:-(.
Correct program
#include <iostream>
using namespace std;
class distance
{
private:
int feet;
float inches;
public:
distance()
{
feet = 0;
inches = 0;
}
distance(int ft, float in) {
feet = ft;
inches = in;
}
void get_dist()
{
cout<<"\n Enter feet : ";
cin>>feet;
cout<<"Enterinches";
cin>>inches;
}
void showdist()
{
cout<<feet<<"<<inches<<";
}
/*void operator += (Distance);
};*/
void operator +=(distance d2)
{
feet += d2.feet;
inches += d2.inches;
if(inches==12.0)
{
inches-=12.0;
feet++;
}
}
} dist1, dist2(11,6.25);
int main()
{
dist1.get_dist();
cout<<"\n dist1=";
dist1.showdist();
cout<<"\n dist2=";
dist2.showdist();
dist1 += dist2;
cout<<"\n after addion";
cout<<"\n dist1=";
dist1.showdist();
cout<<endl;
return 0;
}
Before expecting an answer from us you should have atleast stated what you felt could have been wrong with this code. That way even you would have got a better insight into your code and scope for improvements.
I have these errors:template declaration of 'T Power'
'Ta' was not declared in this scope
expected primary-expression before 'int'
'Power' was not declared in this scope
I wrote it how it was given to me. I understand I need atleast stated what I felt could have been wrong with this code. I did attempt to see whats wrong and i don't understand but I grately appreciate any help given to me
#include <iostream>
using namespace std;
template<class T>
T Power (Ta, int exp)
{
T ans = a;
whiile (--exp>0)
ans*=a;
return (ans);
}
int main()
{
int i = 5, j=2;
float r=0.5;
double b=12.345;
cout<<Power(j,i)<<endl;
cout<<Power(r,j)<<endl;
cout<<Power(b,i)<<endl;
Correct code -
#include <iostream>
using namespace std;
template<class T>
T Power (T a, int exp)
{
T ans = a;
while ((--exp)>0)
ans*=a;
return (ans);
}
int main()
{
int i = 5, j=2;
float r=0.5;
double b=12.345;
cout<<Power(j,i)<<endl;
cout<<Power(r,j)<<endl;
cout<<Power(b,i)<<endl;
}
why did i get error "int"
#include <iostream>
using namespace std;
double divide(int, int)
int main()
{
int num1, num2;
cout<<"enter two numbers";
cin>>num1>>num2;
try
{
quotient=divide(num1,num2);
cout<<"the quotient is"<<quotient<<endl;
}
catch (char*exception string)
{
cout<<exception string;
}
cout<<"End of the Program \n";
return 0
}
double divide (int numerator, int denominator)
{
if(denominator==0)
throw "Error: can not be divided by zero \n"
return static_cast <double>(numerator)/denominator;
}
Note:- This post has been edited by jpratt at Thu, 07/15/2010 - 18:37.