String class confusions

This is a discussion on String class confusions within the C++ Programming forums, part of the General Programming Boards category; My string array 'wordbank' is empty after my getword function has run. Please could somebody tell me what I need ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    String class confusions

    My string array 'wordbank' is empty after my getword function has run. Please could somebody tell me what I need to know about strings to understand why what I'm trying won't work. Thanks

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string wordbank[100];
    	int getword(string);
    
    	int i = 0;
    	while(!getword(wordbank[i++]));
    
    	cout << "wordbank[0] is empty? " << wordbank[0].empty() << endl;
    	
    	for (i=0; (!wordbank[i].empty()); i++)
    		cout << wordbank[i];
    
    }
    int getword(string str)
    {
    	cin >> str;
    
    	if (str == "Quit")
    		return 1;
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    671
    Code:
    int getword(string& str)
    {
        ...
    }
    You have to pass str by reference, so that the modifications are visible to the caller.
    I never put signature, but I decided to make an exception.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    kmdv's observation is on the mark, but a few other things to watch out for:
    • wordbank is an array of 100 words, but your loop that reads into wordbank does not check for this, thus your program is susceptible to buffer overflow.
    • getword probably should return a bool instead of an int.
    • While reading into wordbank, you increment i. Thus, when the loop terminates, you know the number of words that have been stored. Use this knowledge instead of testing with wordbank[i].empty().
    • As a matter of good style, include the parameter name in your forward declaration of getword. Personally, I would rather forward declare getword at file scope rather than in the scope of main.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Thanks for your help both!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusions regarding the GCC compilation steps
    By AnishaKaul in forum C Programming
    Replies: 3
    Last Post: 09-09-2010, 05:48 AM
  2. Confusions related to Dll.
    By nnhamane in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2008, 02:11 AM
  3. array confusions
    By yukapuka in forum C++ Programming
    Replies: 9
    Last Post: 06-11-2008, 05:15 AM
  4. string class
    By jjg in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2002, 07:58 PM
  5. String Class
    By CanadianOutlaw in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2001, 01:33 PM

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