Advertisement

Need Some Guidence :)

Started by May 02, 2002 06:48 AM
14 comments, last by Klear 22 years, 7 months ago
I writing my first program. I am encountering some things I need help with. I am just really setting up the class that will make everything work. I have a few questions though. Should I use pointers to pass info in and out of the class to keep from movin big arrays? Should I be using strings to do this work(dont know how to use it though)? If I do, where is there some info on string that could help me. Thanks //BOC #include <iostream.h> class Address{ Public: Address (); ~Address (); // putting in info int putName(char tmpname[]); int putEmail(char tmpemail[]); int putAddress(char tmpaddress[]); int putphone(char tmpphone[]); int putcomment(char tmpcomment[]); // getting info char getName(); char getEmail(); char getAddress(); char getPhone(); char getComment(); Private: char name[25]; char email[30]; char address[120]; char phone[20]; char comment[100]; } Address::Address(){ } Address:utName(char tmpname[]){ name[] = tmpname[]; } Address:utEmail(char tmpemail[]){ name[] = tmpEmail[]; } Address:utAddress(char tmpaddress[]){ name[] = tmpAddress[]; } Address:utPhone(char tmpphone[]){ name[] = tmpphone[]; } Address:utComment(char tmpcomment[]){ name[] = tmpComment[]; } Address::getName(){ return pname[]; } Address::getEmail(){ return pemail[]; } Address::getAddress(){ return paddress[]; } Address::getPhone(){ return pphone[]; } Address::getComment(){ return pcomment[]; } void main(){ Address currententry = new Address; //EOC
quote: Original post by Klear
Should I use pointers to pass info in and out of the class to keep from movin big arrays?

Since you can''t pass arrays, this is a moot point. A more useful suggestion would be to use std::vector instead of arrays, and to pass vectors by const reference wherever possible.
quote:
Should I be using strings to do this work(dont know how to use it though)? If I do, where is there some info on string that could help me. Thanks

Here. You will also find info on std::vector.
quote:
//BOC
#include <iostream.h>

That''s
#include <iostream> 


To summarise: don''t use char* and arrays, they are confusing you. Prefer std::string to char* and prefer std::vector to arrays.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
Advertisement
Assignment to arrays doesn't work the way you're trying. You can't assign to an array, you have to copy data into the array.

So instead of:

    Address::PutName(char tmpname[]){name[] = tmpname[];}You would need to do something like:void Address::PutName(char* tmpname) {   strcpy(name, tmpname);}// Your functions also need return types.  If you aren't// returning anythin, declare it as void.    


Another (better) option is to store your strings as std::string instead of character arrays. Then assignment works like you'd expect.


  #include <string>class Foo {protected:   std::string name;public:   void SetName(const char* tmpname) {      name = tmpname;   }   const char* GetName() {      return name.c_str();   }};  



Take care,
Bill



[edited by - Siebharinn on May 2, 2002 9:53:44 AM]
The folks above pretty much covered it. But you might wanna root around and get the book "Thinking in C++". It is available on-line in PDF format. It is possibly the best help I have found for C++. It is actually written on the premise that you aren''t a dumb ass, unlike many books out there.

Landsknecht
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
Thanks. I am sure I''ll have more questions in a day or two. I just keeping using this topic reserved for my address book proggie. Thanks for the help!
The above posters kind of suggested you change the overall implementation of the thing. While that isn''t a bad idea, This is how you''d go about things your way:

Pass pointers, not arrays. Or even better, pass array references. This is likely the best way to handle all those arrays of stuff.

Second, your method of using char''s and things won''t work at all. You haven''t actually tried to compile this, have you? To move data from one char array to another, use this line:
strcpy( DestArray, SourceArray );
The first param is a char*, the second a const char*. Set the functions to take array references. Just remember the fact that modifying references modifies the original as well!

P.S. your class syntex is wrong. You didn''t list the return types with the function definitions, but you need to. Also, your class declaration doesn''t have a semicolon at the end

____________________________________________________________
Direct3D vs. OpenGL
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Advertisement
Just a suggestion, but if this is your first program you may want to try something a little simpler.
Also - in the future, use the source tags to put you stuff in a code box. That way you will get this:

    CTmpClass::putStuff()    

Instead of:
CTmpClass:utStuff()

See that little smiley face??

Landsknecht

P.S. Why are you assingning EVERY peice of data to Name, anyway?

[edited by - landsknecht on May 2, 2002 8:04:43 PM]
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
Ok, I changed all the arrays to strings. it will help me out in the long run when I want to display multiple stings and such. I also made two of them vectors so more than one line could be stored. Did I do this right?

I also need some help on how to get my class working. Could anyone maybe give me some examples in code...?


  #include <iostream>#include <string>#include <fstream>#include <vector>void viewContact();void searchContact();void viewContact();void editContact();class Address{	Public:		Address ();   	~Address ();   // putting in info   void setName(tmpname); 	//sending in tmp strings   void setEmail(tmpemail);   void setAddress(tmpaddress);   void setphone(tmpphone);   void setcomment(tmpcomment);   // getting info   char getName() const {return name};   char getEmail() const {return email};   char getAddress() const {return address};   char getPhone() const {return phone};   char getComment() const {return comment};   Private:   	//info kept in strings   	string name;      string email;      vector<string> address; //vector for mutliple lines      string phone;      vector<string> comment; //vector for mutliple lines}void Address::setName(char tmpname[]){		name = tmpname;   }void Address::setEmail(char tmpemail[]){		email = tmpEmail;   }void Address::setAddress(char tmpaddress[]){		address = tmpAddress;   }void Address::setPhone(char tmpphone[]){		phone = tmpphone;   }void Address::setComment(char tmpcomment[]){		comment = tmpComment;   }void main(){    bool fQuit = false;    int choice;    while (!fQuit){    	choice = doMenu();      if (choice > 5 || choice==0){      	cout >> "\nInvalid entry. Please select again.\n"      }      switch (choice)      {      case 1:	viewContact();             	break;      case 2:	searchContact();      			break;      case 3:	createContact();      			break;      case 4:	editContact();      			break;      case 5:	fQuit = true;      			cout << "\nExiting...\n\n";               break;      default: cout << "\nError in choice!\n";               fQuit = true;      			break;      }    }	return 0;  }void viewContact(){}void searchContact(){}void createContact(){}void editContact(){}int doMenu(){	int choice;   cout << "\n\n        [ Address Book vAlpha - Main Menu ]\n\n;   cout << "[ 1 ] View All Contacts\n";   cout << "[ 2 ] Search Contacts\n";   cout << "[ 3 ] Create New Contact\n"   cout << "[ 4 ] Edit Contact\n"   cout << "[ 5 ] Exit\n"   cin >> choice;   return choice;}  


This is my first real program other than the beginner hello proggie. Thanks for all the help!


[ They said I needed windows 95 or better so I chose linux! ]
I''ll implement pointers in next.

No, I have not compiled this yet. I am just trying to get a basic structure of how I want my program to be. Then I''ll go through and hard code things. The reason why I chose to make an address book is because my mom''s work is getting an ass load of wurms through outlook that are taking advantage of the m$ addressbook. So if she uses mine, there will less of a chance of her spreading it even more within the company.

This topic is closed to new replies.

Advertisement