Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-09-2004, 06: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
Old 05-09-2004, 09:43 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,712
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-09-2004, 09:47 PM   #3 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
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;
}
}
}
joe_bruin is offline   Reply With Quote
Old 05-10-2004, 03:22 AM   #4 (permalink)
kokkines
Registered User
 
Join Date: Feb 2004
Posts: 4
kokkines is on a distinguished road
Thanks. How would I go about doing that?
kokkines is offline   Reply With Quote
Old 05-11-2004, 12:15 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
you're searching the wrong planets.
Nice Catch!
__________________
Valmont is offline   Reply With Quote
Old 05-11-2004, 01:29 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,712
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
to put data methods inside class or not? sde Java 2 05-25-2004 04:09 PM
Array initialization in a class dhaas Standard C, C++ 2 04-18-2003 12:30 PM
pointer to function with class? Kportertx Standard C, C++ 5 04-11-2003 05:12 AM


All times are GMT -8. The time now is 12:15 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting