DID I DO THIS PROGRAM RIGHT

6 replies [Last post]
jpratt
User offline. Last seen 7 weeks 2 days ago. Offline
Joined: 06/17/2010
bOt Points: 94

#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;
}

 
 

jpratt
User offline. Last seen 7 weeks 2 days ago. Offline
Joined: 06/17/2010
bOt Points: 94
can i please have some help

can i please have some help on this program..:-(.

shashwat
User offline. Last seen 14 hours 33 min ago. Offline
Joined: 02/18/2009
bOt Points: 1064
Correct 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;
}

nitesh
User offline. Last seen 5 weeks 6 days ago. Offline
Joined: 03/26/2009
bOt Points: 370
What you think was wrong

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.

jpratt
User offline. Last seen 7 weeks 2 days ago. Offline
Joined: 06/17/2010
bOt Points: 94
I Don't Understand Whats Wrong

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;

shashwat
User offline. Last seen 14 hours 33 min ago. Offline
Joined: 02/18/2009
bOt Points: 1064
Correct Code

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;

}

 

jpratt
User offline. Last seen 7 weeks 2 days ago. Offline
Joined: 06/17/2010
bOt Points: 94
what went wrong

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.