Problems with classes... just starting out w/ them.

This is a discussion on Problems with classes... just starting out w/ them. within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using std::cout; class sample { private: string name; public: sample(); //use to set the value--sometime ...

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    
    class sample
    {
       private:
          string name;
       public:
           sample();
    
          //use to set the value--sometime called a mutator, because it changes the value, but can't retrieve it 
          void set_name(string);
    
         //use to get the value--sometimes called an accessor, because it allows you to access private stuff, but not change it
         string get_name() const;
    };
    
    sample::sample() : name("")
    {}
    
    sample::set_name(string s_name)
    {
       name = s_name;
    }
    
    sample::get_name() const
    {
       return name;
    }
    
    int main()
    {
       sample her;
       her.set_name("Gloria");
       cout << her.get_name();
    
      // her.name = "Gloria";   //won't work, name is private
      // cout << her.name;      //won't work, name is private
    }

  2. #17
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    I am having problems with the validatessn function, it will always return false, or at least it seems like that is what it is doing. it also takes way too long (or at least it seems like it to me) to validate (around 10 seconds or so... running a p4 2.8 ghz) any suggestions, I know that I am going about it wrong, but my book is of no help to me on this...

    Code:
    // person.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include "person.h"
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    void Get_Input (Person &tmp){
    	char name[100];
    	char lname[100];
    	char soc[100];
    	int byr,
    		bmo,
    		bdy;
    	bool ValidSSN;
    	cout <<"Input a name ";
    	cin >> name;
                    tmp.SetfName(name);
    	cout <<"Input a last name ";
    	cin >> lname;
                    tmp.SetlName(lname);
    				cout <<tmp.GetlName() << endl;
    	cout <<"Input a SSN (9 digits) ";
    	cin >> soc;
    	ValidSSN = tmp.validateSSN(soc);
    		if (ValidSSN == true)
                    tmp.SetSSN(soc);
    		else 
    		{
    			cout <<"Input a SSN (9 digits) ";
    			cin >> soc;
    			ValidSSN = tmp.validateSSN(soc);
    		}
    	cout <<"Input birth year: ";
    	cin >> byr;
    				tmp.SetBirth_Year(byr);
    	cout <<"Input birth month (1-12): ";
    	cin >> bmo;
    			tmp.SetBirth_Month(bmo);
    	cout <<"Input birth day (1-31): ";
    	cin >> bdy;
    			tmp.SetBirth_Day(bdy);
    	
    }
    
    
    int main() {
        Person firstPerson, secondPerson;
        Person workers[2];
        for (int i=0; i<2; i++)
            Get_Input(workers[i]);
    	cout << workers[i].GetfName() << endl;
        return 0;
    }
    Code:
     
    #ifndef Person_H
    #define Person_H
    class Person{
    private:
    	char *fName;
    	char *lName;
    	char ........N;
    	int Birth_Year;
    	int Birth_Month;
    	int Birth_Day;
    	int Length;
    	char* value;
    	
    	int findLength ();
    	
    	
    public:
    Person ();
    	
    bool validateSSN (char *soc);
    void SetfName (char* first);
    void SetlName (char* last);
    void SetSSN (char* soc);
    void SetBirth_Year (int byr);
    void SetBirth_Month (int bmo);
    void SetBirth_Day (int bdy);
    
    char* GetfName ();
    char* GetlName ();
    char* GetSSN ();
    int GetBirth_Year ();
    int GetBirth_Month ();
    int GetBirth_Day ();
    };
    #endif;
    Code:
     
    #include "stdafx.h"
    #include "person.h"
    using std::cout;
    using std::cin;
    using std::endl;
    Person::Person (){
    	fName = "";
    	lName = "";
    	SSN = "";
    	Birth_Year = 0000;
    	Birth_Month = 00;
    	Birth_Day = 00;
    	}
    int Person::findLength (){
    	int len = 0;
    	char *s=value;
    	for (;s!='\0'; s++, len++);
    	return len;
    	}
    
    bool Person::validateSSN (char* soc){
    		value = soc;
    		Length = findLength();
    		if (Length == 9)
    			return true;
    		else
    			return false;
    	}
    
    
    void Person::SetfName (char* first){
    	fName = first;
    }
    
    void Person::SetlName (char* last){
    	lName = last;
    }
    void Person::SetSSN (char* soc){
    	SSN = soc;
    	}
    void Person::SetBirth_Year (int byr){
    	Birth_Year = byr;
    }
    void Person::SetBirth_Month (int bmo){
    	Birth_Month = bmo;
    }
    void Person::SetBirth_Day (int bdy){
    	Birth_Day = bdy;
    }
    char* Person::GetfName (){
    	return fName;
    }
    char* Person::GetlName (){
    	return lName;
    }
    char* Person::GetSSN (){
    	return SSN;
    }
    int  Person::GetBirth_Year (){
    	return Birth_Year;
    }
    int Person::GetBirth_Month (){
    	return Birth_Month;
    }
    int Person::GetBirth_Day (){
    	return Birth_Day;
    }
    Last edited by criticalerror; 03-03-2004 at 10:09 PM.

  3. #18
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ok, next time, just post the function you had the error in. First, you don't need all those extra values. You also have a problem with findLength. You use value, but it's not initialized.
    Code:
    bool Person::validateSSN (){
                                    int len = 0;
    		char *s = SSN;
                                    while (s != '\n')
                                        len++;
                                    //or instead
                                    len = strlen(SSN);
    		if (len == 9)
    			return true;
    		else
    			return false;
    	}
    Try that. It's using your basic idea of your findLength function, not sure if it works. Try strlen().
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #19
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    char * value; doesn't assign any memory to value, it just means it is a pointer to type char. It could be a pointer to a single char or multiple chars, but you can't say.

    You can't assign one array to another, irrespective of the type of the array. For char arrays terminated by the null character, that is a C style string, you can use the strcpy(), or similar function, to assign the value of one string to another, provided that both strings have memory assigned to them, and there is adequate memory to hold the string. Therefore the following line won't work.

    value = soc;

    I'd drop findlength() and just use another string function called strlen() to find length of soc, like this:

    Length = strlen(soc);

    There are a bunch of other standard functions to manipulate C style strings. They are found in the header file called cstring (or string.h if you have an older compiler). The header file called string, as you've used it, is for the STL string class, which has a C style string (aka null terminated char array) embedded in it, but is a related/but different beast from a char [] or a char * used to denote a C style string. For one thing, you can assign one STL string to another, using the assignment operator directly, that is, without using strcpy().

    Looks like you need to look up the difference between STL strings and C style strings, and stick to one or the other for now. You're getting them all mixed up at the moment. It's a very common problem.

  5. #20
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    elad, same thing i said. but, your function is a part of the class, and you shouldn't pass it a value that is a part of the class. SSN can be seen from within the function and should be used how I have it. I'd recommend declaring SSN like this though:

    Code:
    char SSN[10];
    //and in constructor
    memset(SSN, '\0', 10); // I'm pretty sure this is how memset is used
    //I would double check, I'm used to using ZeroMemory()
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #21
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    i am using the #include <string> and it says that string is an undeclared variable when i try to do something like

    string SSN;

  7. #22
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    neandrake:

    agreed, our last two posts have essentially the same information, but you flatter me if you think I could type that post in 2 minutes.

    As to the second statement in your last post, I'm not sure what it is in reference to. In reading my posts I can't see where I pass a member varaible to a member function, but I'm getting tired and I may be overlooking it.
    _________________________________________________
    criticalerror:

    fName = "";
    lName = "";
    SSN = "";

    the above three assignments should all fail because fName, lName, and SSN are all meant to be C style strings since they are declared using type char * and not type string. Therefore trying to assign even an empty string using the asignment operator should cause an error statement, I would think. In addition, none of the above variables have been assigned any memory yet, either, so even trying to use strcpy() would fail. If fName, lName, and SSN were declared as STL strings like this:

    string fName;

    instead of

    char * fName; //this could be a C style string

    or even

    char fName[10]; //this will be a C style string, if used correctly

    then the above lines would be valid as you can assign one STL string to another, even if the right hand side string is empty.

  8. #23
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    since you are using individual using statements like:

    using std::cout;

    etc., then you need to tell the compiler that the string class you want to use is from the std namespace, which is the default namespace, and where STL classes, including string, reside. Therefore you will need to do this:

    std::string SSN;

    or erase all the using std::cout; etc lines and use a line like this:

    using namespace std:

    This line allows you to use everything in namespace std without using the scope operator in front of it (at least once). It also opens access to everything in namespace std, which is considered bad form by programmers that are more sophisticated than I am, so I usually use the using namespace std; line and be done with it, even though I know I am slumming it by doing so.

  9. #24
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    alright... i'm getting closer... I have changed to namespace std, but now the strlen is looking for a char * (c-style string?) instead of a string... what do you use for strings?

    btw elad where in wisconsin are you from? i'm in Stevens Point....

  10. #25
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    figured it out...

    len = SSN.length ();

  11. #26
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    thanks everyone, I really appreciate all the help!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  2. Just starting to learn C++ Question about classes
    By uraliss in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2006, 04:31 AM
  3. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 11:19 AM
  4. Starting to get aggrivated with classes....
    By imortal in forum C++ Programming
    Replies: 21
    Last Post: 06-01-2003, 04:21 PM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21