• Advertisement

Archived

This topic is now archived and is closed to further replies.

Output Problem

This topic is 5298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to do an exercise from a book I got, which is to read a line of input, store it in a list rotate and display it but after I get the input it doesn't do anything. Here's the code
#include<iostream>
#include<string>
#include<list>
#include<cctype>
using namespace std;

list<string> Split(const string &str)
{
	list<string> l;
	string::size_type i, j;
	i = 0;
	
	while(i != str.size())
    {
    	for(; isspace(str[i]); i++)
    		;
   		
   		for(j = i; !isspace(str[j]); j++)
   			;
   			
	    l.push_back(str.substr(i, j));
    }
    
    return l;
}

int main()
{
	list<string> perm;
	string line;
	
	cout << "Enter a line of words:";
	getline(cin, line);
	
	perm = Split(line);
	
	cout << endl;
	
	for(int x = 0; x < perm.size(); x++)
	{
	
		for(list<string>::iterator i = perm.begin();
 			 i != perm.end(); i++)
 			 cout << *i << endl;
 			 
	    line = perm.front();
	    perm.erase(perm.begin());
	    perm.push_back(line);
    }
    
    system("pause");
    return 0;
}
[\source]

The same thing happens in about a lot of my programs that gets input and has to do something with the input.  [/i]

<SPAN CLASS=editedby>[edited by - Typhoon on August 17, 2003 1:27:37 PM]</SPAN>

Share this post


Link to post
Share on other sites
Advertisement
After you've grabbed your first word, where is i sitting? It's pointing to the first character of that word in the original string. So the next time through the loop, i's for loop will immediately fail because it's already on a non-space character. So you're endlessly pushing back the first word of the string.

Just take your program as is except change

l.push_back(str.substr(i, j));

to be

string sub = str.substr(i, j);
cout << sub << endl;
l.push_back(sub);

and you'll see what I mean. Doing little things like this can make it a lot clearer what's going on.

After the push back you just need to say i = j, so i can start working on the next word.

But there's another problem. j's kind of on his own, nobody's there to make sure he stays within the boundaries of the string. Changing his for loop to be

for(j = i; j < str.size() && !isspace{str[j]); ++j)
;

prevents that.

[edited by - tortoise on August 17, 2003 4:00:05 PM]

Share this post


Link to post
Share on other sites
Er, I made a mistake. Was thinking of something else.

substr is substr(start position, length of string),

so that needs to be str.substr(i, j-i).

What book is this? I''d seriously consider getting another one.

Share this post


Link to post
Share on other sites
I already realized that mistake after I ran the program. The book i''m reading is Accelerated C++.

Share this post


Link to post
Share on other sites
Ahh, ok. I assumed you presented code from the book. Definitely don''t get a new book, Accelerated C++ is excellent

Share this post


Link to post
Share on other sites
As you will see as you progress through Accelerated C++, the STL provides many entities that do the "hard coding" for many common algorithms and data structures, and thus make your code cleaner, and often much more efficient. For example, the exercise you described could be done very simply like this:

int main()
{
std::cout << "Enter line: ";
std::string line;
std::getline( std::cin, line ); // get user input


std::stringstream ss(s);
std::istream_iterator<std::string> beg(ss), end;
std::list<std::string> words( beg, end ); // construct list, initialising each new std::string element to a word of the line

std::reverse( words.begin(), words.end() ); // reverse the list of strings


std::cout << "Reversed: ";
std::copy( words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, " ") ); // print them to the standard output (`std::cout'')

std::cout << std::endl;

system("PAUSE");
return 0;
}

Naturally, I don''t expect you to understand this, considering the stage of learning that you are at. I''m merely pointing out what a benefit the STL is, as I am sure you have started to see. And yes, keep with Accelerated C++; it is an excellent book.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites

  • Advertisement