Thread: String array and Space problems

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

    Question String array and Space problems

    Hi,

    I wrote the following code and found there are 3 logical errors:

    1) When I input the URL like www.abc.com www.xyz.com (there is a space between these 2 domains), it will skip the input title entry and treat the second string as the title.

    2) I entered the www.abc.com for URL. I input 2 strings like www.xyz.com www.yahoo.com for the title page (A space % 2 strings also). It will list out the first record like:

    www.abc.com www.xyz.com

    without displaying ww.yahoo.com and unable to input next record.

    3) It shows 10 arrays every time even some of them did not have records. e.g. I just input 3 records, it will show 7 blank records.

    How I can fix them?

    Regards

    gogo

    Here is the code for implementation:

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

    void bookmarks::setbookmarks()
    {
    char a = 0;
    int k = 0;
    int j = 0;

    do
    {
    cout << "Input URL : ";
    cin >> url[k++];
    cout << endl;
    cout << "Input Title : ";
    cin >> title[j++];
    cout << endl;
    cout << "Do you want to add more (Y/N)? ";
    cin >> a;
    cout << endl;
    }while (j<SIZE && tolower(a)=='y');
    }

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


    Here is the main program:

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

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    One fatal error:

    you have reserved no memory for url[] or title[] strings

    url[i] = "" is not enough, it is just empty string

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    url[i] = ""

    Yes, the string array was assigned null value for initialization.

    The program can be run and what do you mean the fatal error?

    Thanks

    gogo

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    Likely, you have url defined:

    struct bookmarks {
    char *url[SIZE];
    };

    In constructor you must assign a memory to these pointers.

    Do you do it?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    I have not used pointer for the data members of the following header file.

    #define SIZE 10

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

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

    Sorry, I think the setbookmarks and getbookmarks methods have problem.

    Could you tell me how the memory affect the display of getbookmarks?

    Thanks for help.

    gogo

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    There's no mystery. Like I told, char *url[] is only array of pointers
    to character. SIZE is 10, pointer is 4 bytes long. So you have 40 bytes in this array. Every pointer must point to the valid (reserved, assigned, call it like you want) area of memory before it can be used.

    Reserve memory by "new" command and release it by "delete" command.

    // constructor
    bookmarks::bookmarks() {
    for (int i = 0; i < SIZE; i++)
    url[i] = new char[100];
    }

    Every string will have 100 characters, including the 0 termination byte.


    // destructor
    bookmarks::~bookmarks() {
    for (int i = 0; i < SIZE; i++)
    delete url[i];
    }

    Capito?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    I have 2 questions:

    1) I have not used pointer for the string type. So, any input for the url[i] data member will be assigned to it one by one, not new an object and done by address reference.

    2) The data type of url[SIZE] is string, not char.

    How can I ?

    string url[SIZE] = new char[100] ??

    Thanks

    gogo

  8. #8
    Unregistered
    Guest
    Sorry for not understanding you in the previous posts. I had no
    class description from you. Your class definition with string class arrays seems to be OK. Solution with (char *) arrays is equivalent.
    You may choose one. String is the class which encapsulates character array and assign it memory automatically for you.


    >> 3) It shows 10 arrays every time even some of them did not have records. e.g. I just input 3 records, it will show 7 blank records.

    You must define an int variable and assign it value instead of using SIZE. which is mere preprocessor constant.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Could you elaborate it more specifically?

    e.g. where should I add the int variable, how should I declare it, etc.

    Also, how can I solve the space % 2 strings issue?

    Regards

    gogo

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    #define SIZE 10


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

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



    bookmarks::bookmarks () {
    size = 0;
    }



    void bookmarks::setbookmarks () {
    char a;

    size = 0;

    do {
    cout << "Input URL : "; cin >> url[size];
    cout << "Input Title : "; cin >> title[size++];
    cout << "Do you want to add more (Y/N)? ";
    cin >> a;
    }
    while (size < SIZE && tolower (a) == 'y');

    }




    void bookmarks::getbookmarks () {

    cout << endl;
    cout << "**********************************" << endl;

    for (int i = 0; i < size; i++) {
    cout << "URL : " << url[i] << " " << "Title : " << title[i] << endl;
    }

    cout << endl;

    }


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

    Thanks for your great help and it was fixed now.
    But, I don't understand why the first array uses url[size] but not url[size++]? Does it avoid to double add the size for twice time?

    Also, I have one remaining problem with the code. i.e. the whitespace for the input.

    If I have 2 strings for the URL and there is a whitespace between them, it will go directly to the "Do you want to add more?" question and skip the Title input.

    I tried to change cin >> url[k++] to getline(cin, url[k++]);

    It can be compiled and run. But, the input for url is strange. You must enter 2 strings with whitespace. If you enter www.abc.com without whitespace, it will go directly to ask you whether to add more and skip the title field.

    Any idea or it cannot be done through this way?

    Regards

    gogo

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    >> But, I don't understand why the first array uses url[size] but not url[size++]? Does it avoid to double add the size for twice time?

    That is true. You must not increase counter variable two times within one single cycle.

    title[size++] and url[size] strings must have the same index when you put characters into them. One postscript ++ operator ensures it, because it returns size before performing addition. It is good to look at ++ or -- operators as they were functions.

    For problems with blank space, I'm afraid, I cannot help. I'm not familiar with iostreams nor with standard template library (string). Maybe someone else can.


  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Thanks for your great help.

    Regards

    gogo


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  2. Replies: 10
    Last Post: 11-06-2005, 09:29 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. string arrays
    By Raison in forum C Programming
    Replies: 27
    Last Post: 10-02-2003, 06:27 PM
  5. copying a string to an array
    By sameintheend01 in forum C++ Programming
    Replies: 7
    Last Post: 03-04-2003, 07:50 PM