|
 |
|
 |
05-09-2004, 06:27 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Feb 2004
Posts: 4
|
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
|
|
|
05-09-2004, 09:43 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,712
|
hmm... don't know if cin strips '\n' or <cr> from the input, if it dosn't then strcmp() will return the index where the comparison fails ie a positive number which can't be 0.
Else use strncmp() and use the length of A[i].n as the size.
|
|
|
05-09-2004, 09:47 PM
|
#3 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
you're searching the wrong planets. A is not initialized in this function.
Code:
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;
}
}
}
|
|
|
05-10-2004, 03:22 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Feb 2004
Posts: 4
|
Thanks. How would I go about doing that?
|
|
|
05-11-2004, 12:15 AM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
|
you're searching the wrong planets.
|
Nice Catch!

__________________
|
|
|
05-11-2004, 01:29 AM
|
#6 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,712
|
Quote:
Originally posted by kokkines
Thanks. How would I go about doing that?
|
Remove the Planet A[5]; from the void Planet::find() Since you're declaring A as Planet class within the main you shouldn't need to declare it within it's own find function.
On a closer look, it seems your Planet class definition won't work with this sort of find() function.
Code:
class Planet
{
private:
string name;
float radius;
float median;
public:
/* create functions to deal with any type of "decleration" */
bool Planet();
bool Planet(string name);
bool Planet(string name, float radius);
bool Planet(string name, float radius, float median);
/* delete function */
bool ~Planet();
float sa();
float den();
void output();
void outputaf();
};
bool Planet::Planet()
{
return (this.name = "" &&
this.radius = 0.0 &&
this.median = 0.0);
}
bool Planet::Planet(string name)
{
return (this.name = name &&
this.radius = 0.0 &&
this.median = 0.0);
}
bool Planet::Planet(string name, float radius)
{
return (this.name = name &&
this.radius = radius &&
this.median = 0.0);
}
bool Planet::Planet(string name, float radius, float median)
{
return (this.name = name &&
this.radius = radius &&
this.median = median);
}
bool Planet::~Planet()
{
return (this.name = "" &&
this.radius = 0.0 &&
this.median = 0.0);
}
float Planet::sa()
{
return pow(this.radius,2)*4*M_PI;
}
float Planet::den()
{
return (this.median/(pow(this.radius,3)*M_PI*4/3));
}
class Planets
{
private:
vector<Planet> P
public:
bool sort();
bool add();
bool del();
void find();
}
might be a better aproach.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 12:15 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|