Thread: Class Array
View Single Post
Old 05-09-2004, 07:27 PM   #1 (permalink)
kokkines
Registered User
 
Join Date: Feb 2004
Posts: 4
kokkines is on a distinguished road
Unhappy Class Array

I need help with my find function. It is coming up "Not Found" even when I enter a valid name. Please help me figure out what I am doing wrong. Here is my code...
Code:
//need to figure out how to add,delete, find, sort alphabetically
#include<iostream>
#include<cmath>
#include<string>
using namespace std;

class Planet
{
private:
char n[100];
//char n[100];
float r;
float m;
public:
bool input();
float sa();
float den();
void output();
void outputaf();
Planet();
bool sort();
bool add();
bool del();
void find();
//int Planet[], int qty
//bool find(int Planet[], int qty);
//int getsize();
//int find();
};

Planet::Planet()
{
char n[100];
float r = 0;
float m = 0;
}

void Planet::output()
{
cout<<"Name"<<n<<endl;
cout<<"Radius"<<r<<endl;
cout<<"Mass"<<m<<endl;
}
/////////////////////////////////
void Planet::outputaf()
{
cout<<" Name "<<n;
cout<<" Radius "<<r;
cout<<" Mass "<<m;
cout<<" sa "<<Planet::sa();
cout<<" den "<<Planet::den()<<endl;
}
/////////////////////////////////////
/////////////////////////////////////////
bool Planet::input()
{
bool rv=false;
cout<<"Name"<<endl;
cin>>n;
if(cin.fail()==0)
{
cout<<"Radius"<<endl;
cin>>r;
if(cin.fail()==0)
{
cout<<"Mass"<<endl;
cin>>m;
if(cin.fail()==0)
{
rv=true;
}
}
}
return rv;
}

float Planet::sa()
{
return pow(r,2)*4*M_PI;
}

float Planet::den()
{
float v=(pow(r,3)*M_PI*4)/3;
return m/v;
}

//bool input(Planet x[], int num);
//void output(Planet x[], int num);
//void outputaf(Planet x[], int num);
//void sort(Planet x[], int num);
//void swap(Planet x[], int num);
//bool pass(Planet x[], int num);
//void display(Planet x[], int num);
//bool find(Planet x[], int num);



bool input(Planet x[], int num)
{
bool rv=true;
for(int i=0; i<num && rv==true; i++)
{
rv=x[i].input();
}
return rv;
}

void output(Planet x[], int num)
{
for(int i=0; i<num; i++)
{
x[i].output();
}
}
////////////////////////////////////////
void outputaf(Planet x[], int num)
{
for(int i=0; i<num; i++)
{
x[i].outputaf();
}
}
//////////////////////////////////////////
void swap(Planet& x, Planet& y)
{
Planet tmp;
tmp=x;
x=y;
y=tmp;
}


//bool pass
//int Planet::getsize()
//{
//Planet A;
//return A.getsize();
//}
/////////////////////////////////////////////////////
void Planet::find()
{
Planet A[5];
char find[100];
cout<<"Find which planet?"<<endl;
cin>>find;
//cout<<"Find is "<<find<<endl;
//bool found=false;
for(int i=0; i<=5 ; i++)
{
if (strcmp(find, A[i].n)==0)
{
//found=true;
cout<<"radius"<<A[i].r;
}
else
{
cout<<"Planet not found"<<endl;
}
}
}
///////////////////////////////////////////////////////
bool Planet::sort()
{
}

bool Planet::add()
{
}

bool Planet::del()
{
}

//////////////////////////////////
int main()
{
Planet A[5];
if(input (A,5) == true)
{
int choice;
cout<<"What would you like to do?"<<endl;
cout<<"1. Add a Planet"<<endl;
cout<<"2. Delete a Planet"<<endl;
cout<<"3. Find a Planet"<<endl;
cout<<"4. List all planets"<<endl;
cin>>choice;
if(cin.fail()==1)
{
cout<<"Invalid Choice"<<endl;
}
else if(choice==1)
{
A[5].add();
}
else if(choice==2)
{
A[5].del();
}
else if(choice==3)
{
A[5].find();//not working
}
else if(choice==4)
{
outputaf(A,5);//works
}
//A[5].find();//no working
//A[5].sort();//where should I put this???
//A[5].del();
//A[5].add();
//A[5].outputaf();
}
//Planet b[5];
//if(input(A,20)==true)
//if(input(b,5)==true)
// {
// int choice;
// cout<<"What would you like to do?"<<endl;
// cout<<"1. Search for a planet "<<endl;
// cout<<"2. Add a new planet "<<endl;
//cout<<"3. Delete a planet "<<endl;
// cout<<"4. List all planets "<<endl;
// cin>>choice;//put cin.fail statement here
// if(choice==2)
// {
// outputaf(A,5);
//}
//else
//{
//cout<<"What is your choice?"<<endl;
// cin>>choice;
// }
//}
return 0;
}
Also if any one knows how to do the add function and delete function it would be appreciated. I think I have to increase and decrease the number of elements in my array, but I am kind of clueless.

Thanks
-Sharon
kokkines is offline   Reply With Quote