String array question

This is a discussion on String array question within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a question regarding the string array. Here is the header file: #include <iostream> #include <string> #define SIZE ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Question String array question

    Hi,

    I have a question regarding the string array.

    Here is the header file:

    #include <iostream>
    #include <string>
    #define SIZE 100
    using namespace std;

    class bookmarks
    {
    private:
    string url[SIZE];
    string title[SIZE];

    public:
    bookmarks();
    void setbookmarks(string& x[], string& y[]); //how should I set?
    string getbookmarks();
    };


    Here is the implementation file:

    #include <iostream>
    #include "bookmarks.h"
    #include <string>
    #define SIZE 100
    using namespace std;

    bookmarks::bookmarks()
    {
    url[] = ""; //how should I set initialize all elements to be null?
    title[] = "";
    }

    void bookmarks::setbookmarks(string& x[], string& y[])
    {
    url = x; //how should I set?
    title = y;
    }

    string bookmarks::getbookmarks()
    {
    int i;
    cout << " URL Page Title";
    for (i = 0; i < SIZE; i++)
    cout << url[i];
    cout << " ";
    cout << title[i];
    cout << endl;
    }

    Thanks

    gogo




  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    > void setbookmarks(string& x[], string& y[]); //how should I set?
    Try
    setbookmarks(string& x, string& y);
    or
    setbookmarks(string *x, string *y);

    > url[] = ""; //how should I set initialize all elements to be null?
    memset(url, NULL, SIZE);

    > url = x; //how should I set?
    strncpy(url, x, SIZE);
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    I changed the code: (I also tried *x, *y)

    bookmarks::bookmarks()
    {
    memset(url, NULL, SIZE);
    memset(title, NULL, SIZE);
    }

    void bookmarks::setbookmarks(string& x, string& y)
    {
    strncpy(url, x, SIZE);
    strncpy(title, y, SIZE);
    }

    There are 2 identical errors:

    error C2664: 'strncpy' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [100]' to 'char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    Thanks for help.

    gogo

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    instances of the string class cannot be null, but they can be empty. in your constructor try this:

    bookmarks::bookmarks()
    {
    int i;
    for(i = 0; i < size; i++)
    {
    url[i] = "";
    title[i] = "";
    }

    strcpy and it's relatives like strncpy only work with c_style strings, not instances of string classes such as you use in the arrays url and title.

    void bookmarks::setbookmarks(string& x, string& y, int i)
    {
    url[i] = x;
    title[i] = y;
    }

    I think you are getting confused over the various types of strings, a word that is used in many contexts in C++.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Try this as an alternative, I was never partial to the C++ standard string class.
    Code:
    #include <iostream>
    #include <string>
    #define SIZE 100
    using namespace std;
    
    class bookmarks
    {
    private:
    char url[SIZE];
    char title[SIZE];
    
    public:
    bookmarks();
    void setbookmarks(char *x, char *y); //how should I set?
    void getbookmarks();
    };
    
    bookmarks::bookmarks()
    {
    memset(url, 0, SIZE); //how should I set initialize all elements to be null?
    memset(title, 0, SIZE);
    }
    
    void bookmarks::setbookmarks(char *x, char *y)
    {
    strcpy(url, x); //how should I set?
    strcpy(title, y);
    }
    
    void bookmarks::getbookmarks()
    {
    int i;
    cout << " URL Page Title";
    for (i = 0; i < SIZE; i++)
    cout << url[i];
    cout << " ";
    cout << title[i];
    cout << endl;
    }
    But, if you must use the string class, this will work
    Code:
    bookmarks::bookmarks()
    {
    	url = "";
    	title = "";
    }
    
    void bookmarks::setbookmarks(string& x, string& y)
    {
    	url = x;
    	title = y;
    }
    -Prelude
    Last edited by Prelude; 12-08-2001 at 11:55 AM.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Hi,

    I knew that char * can be used to read words and it must be used with strcpy. But, I am doubted that whether it can be done with the array? e.g. char x[20], char *y, strcpy(x, y). Then, the address of the passed word will be passed to y. If I have 10 inputs for the word, could I also use char x[20]?

    or I need to use 2D arrays - char[][20] instead?

    Anyway, I don't want to use char. I have to use string.

    Also, Guest's suggestion is close but still has one error. I need to specify x and y are constants like void bookmarks::setbookmarks(const string& x, const string& y, int i). Otherwise, it will be an error.

    Now, I have 2 directions to accomplish it, but both failed:

    Case 1:

    main program - created an object and called the functions:

    void main()
    {
    bookmarks x;
    x.setbookmarks();
    x.getbookmarks();
    }

    implementation -

    bookmarks::bookmarks() //all arrays are initialized.
    {
    int i;
    for(i = 0; i < SIZE; i++)
    {
    url[i] = "";
    title[i] = "";
    }
    }

    void bookmarks::setbookmarks() //here is the problem - it shows nothing on the console screen for inputation.
    {
    string x;
    string y;
    int j=0;
    url[j] = x;
    title[j] = y;
    for(j = 0; j < SIZE; j++)
    {
    cin >> "Input URL : " >> x;
    cout << endl;
    cin >> "Input Title : " >> y;
    cout << endl;
    }
    }

    void bookmarks::getbookmarks()
    {
    int i;
    cout << " URL Page Title" << endl;
    for (i = 0; i < SIZE; i++)
    {
    cout << url[i] << " " << title[i] << endl;
    }
    cout << endl;
    }


    Case 2:

    A simplied model without string array:

    here is the header file:

    #include <iostream>
    #include <string>
    using namespace std;

    class bookmarks
    {
    private:
    string url; // no array this time
    string title;

    public:
    bookmarks();
    void setbookmarks();
    void getbookmarks();
    };


    Here is the implementation:

    bookmarks::bookmarks()
    {
    url = ""; //initialized null value
    title = "";
    }

    void bookmarks::setbookmarks() // Here is the same problem with the string array code, it shows nothing.
    {
    string x;
    string y;
    url = x;
    title = y;
    cin >> "Input URL : " >> x;
    cout << endl;
    cin >> "Input Title : " >> y;
    cout << endl;
    }

    void bookmarks::getbookmarks()
    {
    cout << "URL : " << url << " " << "Title : " << title << endl;
    }

    Main program:

    void main()
    {
    bookmarks x;
    x.setbookmarks();
    x.getbookmarks();
    }

    My teacher gave a hint to me that to create a bookmarks type of array in order to get continuous input from user.

    e.g. bookmarks x[100];

    And, you can print out each array like object.

    But, I think I have to fix the inputation problem first. If any hint for the bookmarks array, pls let me know.

    Regards

    gogo

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Sorry, I made a big mistake for the inputation.

    cout << "Input URL : ";
    cin >> x;
    cout << endl;
    cout << "Input Title : ";
    cin >> y;
    cout << endl;

    Thanks for help.

    gogo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  2. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 08:15 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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