Thread: Passing string through a function...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Question Passing string through a function...

    Howdy. I have been pondering how I would go about taking, in this case, the name of a subject (for a GPA tracking program), and pass it through to an overloaded constructor. I figured pointers were the solution, but nonetheless I kept getting "int to char[11]" errors and the like. Here is a stubbed out version of what I've attempted so far:

    #include <iostream.h>

    class Course
    {

    public:

    Course();
    Course(int pname, float credits, unsigned short sem, unsigned short per);
    ~Course();

    private:

    char courseName[11];
    float creditsAtt;
    unsigned short Semesters;
    unsigned short Period;

    };

    Course::Course()
    {

    }

    Course::Course(int pname, float credits, unsigned short sem, unsigned short per)
    {

    courseName = * pname; // so this should give the string,
    // and courseName is a string
    // identical to the local string
    // in int main(). Where have I
    // fouled things up?
    creditsAtt = credits;
    Semesters = sem;
    Period = per;

    }

    //////////////////////////////////////////////////
    //////////////////////////////////////////////////

    int main()
    {

    char name[11];
    float credits = 0;
    unsigned short sem = 0;
    unsigned short per = 0;

    // ( I deleted a lot of explanation, irrelevant stuff here )

    //////////////////////////////////////////////////
    //////////////////////////////////////////////////

    Course * All = new Course[9];
    Course * pCourse;

    int * pname = 0; // OK, setting a pointer for each string

    for(short p = 0; p < 9; p++)
    {

    cout << "\nPlease enter the name of period number " << p << ", 10 characters max please:\t";

    cin.get(name, 10);

    pname = &name; // OK. So this sets the pointer to name's
    // address, right?

    // ( deleted more irrelevant stuff, questions for info )

    pCourse = new Course(pname, credits, sem, per); // OK, so
    // this should set name's address at pname into the parameter?
    All[p] = * pCourse;
    delete pCourse;
    delete pname; // do I have to do this in this case?

    }

    delete [] All;

    return 0;

    }

    ////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////

    END OF CODE

    Hey, thanks a billion. Just wondered if anyone could give this a quick scan and detect my obvious error. :-P
    "None are more hopelessly enslaved than those who falsely believe they are free."
    -Goethe

    [paraphrased] "Those who would sacrifice essential liberties for a little temporary safety deserve neither."
    -Benjamin Franklin

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    pname should be char *pname in your main and constructor. Also, use <iostream>, not <iostream.h>
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Course(int pname, float credits, unsigned short sem, unsigned short per)

    In your parameter list you are saying pname is of type int. If you declare a parameter to be

    int Mr_Compiler_this_is_a_pointer

    the compiler won't change the type to int*. Similarly, just because you use the name "pname" as your parameter name doesn't mean it's a pointer.
    Last edited by 7stud; 04-15-2003 at 04:06 AM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Unhappy

    Oh okay thanks guys, but I've noticed two snags: when I change it to <iostream>, I get like 33 errors and 3 warnings, saying that basically everything is done incorrectly. also, for some reason when i changed the pointers to char and added pointer designations in main and the constructor, nothing changed.
    Last edited by MisterWonderful; 04-15-2003 at 12:01 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    17

    Post

    I should change that, I figured out how to use <iostream> w/ using namespace std...

    okay, so I get an error at this particular line of the aforementioned code...

    Course::Course(char *pname, float credits, unsigned short sem, unsigned short per)
    {

    courseName = *pname; // right here. the error code is:

    /*C:\Programs\GPAcalc\beta.cpp(50) : error C2440: '=' : cannot convert from 'char' to 'char [11]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays*/

    creditsAtt = credits;
    Semesters = sem;
    Period = per;

    }

    ///// END CODE

    and I changed the pointer in int main() to char * pname;
    I changed all of the constructor parameters to char * pname.
    When I called the function, I changed it to *pname instead of pname. Sorry to bother you guys with all this stuff, hopefully as soon as I figure out how to pass references correctly I'll be able to write this GPA prog much more smoothly. Thanks!

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Course::Course(char *pname, float credits, unsigned short sem, unsigned short per)
    {

    courseName = *pname; // right here. the error code is:


    char courseName[11];

    Arrays names act like pointers but they aren't exactly pointers. I cannot explain the internals of why you can't do this:

    char text[10];
    text= "dog";

    but basically it's like a type mismatch "dog" is a string literal that's length is 4(when it's stored in memory a \0 is appended), and text's length is 10. They have to be equal.

    The following code demonstrates that a true pointer can point to cstyle strings of different lengths:

    char* pname;
    pname = "dog";
    pname = "A bigger dog";

    )
    Last edited by 7stud; 04-15-2003 at 01:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing string to function
    By R.Stiltskin in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 12:56 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM