Archived

This topic is now archived and is closed to further replies.

Klear

Need Some Guidence :)

Recommended Posts

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

Share this post


Link to post
Share on other sites
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! ]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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! ]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Siebharinn I don''t see any difference in putting
char tmpname[] in

Address:utname(char tmpname[])

as a parameter or putting

Address:utname(char *tmpname)

these both r same then y u emphisizing on using the later one. Actually char tmpname[] is same as char* const tmpname.



Share this post


Link to post
Share on other sites
Here is the beginning of my CreateContact class. I still have do alot to it such as it saving the contacts info to a file. How would I implement this? Thanks


  
class CreateContact{

public:
void SetName(const char* tmpname) {
name = tmpname;
}

void SetEmail(const char* tmpemail) {
email = tmpemail;
}

void SetPhone(const char* tmpphone) {
phone = tmpphone;
}

void SetPhoneb(const char* tmpphoneb) {
phoneb = tmpphoneb;
}

void SetAddress(const char* tmpaddress) {
address = tmpaddress;
}

void SetComment(const char* tmpcomment) {
comment = tmpcomment;
}

private:
string name;
string email;
string phone;
string phoneb;
string address;
string comment;
};

Share this post


Link to post
Share on other sites
added string copy like Promit said to:


  
class CreateContact{

public:
void SetName(const char* tmpname) {
strcpy(name ,tmpname);
}

void SetEmail(const char* tmpemail) {
strcpy(email ,tmpemail);
}

void SetPhone(const char* tmpphone) {
strcpy(phone ,tmpphone);
}

void SetPhoneb(const char* tmpphoneb) {
strcpy(phoneb ,phoneb);
}

void SetAddress(const char* tmpaddress) {
strcpy(address ,tmpaddress);
}

void SetComment(const char* tmpcomment) {
strcpy(comment ,tmpcomment);
}

private:
string name;
string email;
string phone;
string phoneb;
string address;
string comment;
};

Share this post


Link to post
Share on other sites
How would I get the class to save contact information to a file to later be brought up to be viewed. This file needs to be set up so it can hold other contacts as well. Thanks

Share this post


Link to post
Share on other sites