strings in classes

Discussion in 'C++' started by baje, Mar 8, 2007.

  1. baje

    baje Guest

    hi
    new to c++; have an array to hold five names; however having problems
    reading in the names into the array.here the code i have
    #include<iostream>
    using std::cout;
    using namespace std;
    #include<conio.h>
    #include<iomanip>
    #include<string>

    class Names
    {
    private:

    char lastname[5];

    public:
    Names::lastname();
    void setnames();

    //default Constructor function initialises array
    Names::names()
    {
    for (int i = 0; i < 5; i++)
    {
    strcpy(lastname, "");
    }
    }

    void setnames()
    {
    for (int i = 0; i < 5; i++)
    {

    cin.getline(lastname, 30))//not working to read in five names
    }
    }
     
    baje, Mar 8, 2007
    #1
    1. Advertising

  2. baje wrote:
    > hi
    > new to c++; have an array to hold five names; however having problems
    > reading in the names into the array.here the code i have
    > #include<iostream>
    > using std::cout;
    > using namespace std;
    > #include<conio.h>
    > #include<iomanip>
    > #include<string>
    >
    > class Names
    > {
    > private:
    >
    > char lastname[5];


    Thiat's one string consisting of a maximum of four characters (plus one
    terminating null character). It's not an array of five names.

    You want this

    string lastname[5];


    >
    > public:
    > Names::lastname();


    That's not legal syntax.

    > void setnames();
    >
    > //default Constructor function initialises array
    > Names::names()


    Nor is that.

    > {
    > for (int i = 0; i < 5; i++)
    > {
    > strcpy(lastname, "");
    > }
    > }
    >
    > void setnames()
    > {
    > for (int i = 0; i < 5; i++)
    > {
    >
    > cin.getline(lastname, 30))//not working to read in five names


    Where did 30 come from?

    > }
    > }
    >


    It's obvious that you are confused between C strings, and C++ strings.
    Moreover you don't understand C strings.

    In C

    char name;

    this is a single char, it is not a string. So

    char name[5];

    is five chars, it is not five strings.

    In C++ we can forget about C strings, and just use C++ strings

    class Names
    {
    private:
    string lastname[5];
    public:
    void setnames()
    {
    for (int i = 0; i < 5; ++i)
    getline(cin, lastname);
    }
    }

    getline(cin, s) instead of cin.getline(s, 30) is that way you read a C++
    string in C++. cin.getline(s, 30) is for C strings.

    john
     
    John Harrison, Mar 8, 2007
    #2
    1. Advertising

  3. baje

    osmium Guest

    "baje" writes:

    > new to c++; have an array to hold five names; however having problems
    > reading in the names into the array.here the code i have
    > #include<iostream>
    > using std::cout;
    > using namespace std;
    > #include<conio.h>
    > #include<iomanip>
    > #include<string>
    >
    > class Names
    > {
    > private:
    >
    > char lastname[5];
    >
    > public:
    > Names::lastname();
    > void setnames();
    >
    > //default Constructor function initialises array
    > Names::names()
    > {
    > for (int i = 0; i < 5; i++)
    > {
    > strcpy(lastname, "");
    > }
    > }
    >
    > void setnames()
    > {
    > for (int i = 0; i < 5; i++)
    > {
    >
    > cin.getline(lastname, 30))//not working to read in five names
    > }
    > }


    That didn't compile did it? Use cut and paste. There are missing
    semi-colons and misspelled identifiers in what you posted. Remove stuff
    never used such as <conio.h>.

    You are confused, understandably, by the terminology String has no
    definitive meaning in C++. It can mean a C++ style string (as in <string>)
    or an old C style string, as in <cstring>. Your thoughts seem to be a
    mixture of these two vastly different things. A C string is an array of
    arrays. You only have a single dimensional array ion your code. You
    include <sting> but then use strcpy() which is part of a different animal.

    I suggest this:

    o decide what kind of string you want to use.
    (If you continue your interest in C++, you will eventually want to be
    able to use the C++ string. )
    o write a program implementing that choice. ONLY.
     
    osmium, Mar 8, 2007
    #3
  4. baje

    osmium Guest

    "osmium" wrote:

    > A C string is an array of arrays. You only have a single dimensional
    > array [in] your code.



    That's wrong. What I should have said is:

    A C style string is an array of char. So if you want an array of five
    different 29 char names, which is what you seem to want, you need an array
    of arrays. Reading tea leaves, I think you intended to accommodate a 29 char
    name terminated by a 0.

    Note that you will eventually have to be fluent in both C and C++ strings,
    which was why I was ambivalent in my earlier post about which method you
    focused on.
     
    osmium, Mar 8, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    718
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    388
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    502
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    577
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    772
    Malcolm
    Jun 24, 2006
Loading...

Share This Page