Thread: String class confusions

  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
    733
    Code:
    int getword(string& str)
    {
        ...
    }
    You have to pass str by reference, so that the modifications are visible to the caller.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 03: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