• Advertisement
Sign in to follow this  

C++ program

This topic is 3736 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

Hey I'm having trouble with a simple cpp program and was wondering if anyone would help :).Thanks in advance! Write a program to read a sequence of strings from standard input until either the same word occurs twice in succession or all the words have been read. Use a while loop to read the text one word at a time. Use the break statement to terminate the loop if a word occurs twice in succession. Print the word if it occurs twice in succession, or else print a message saying that no word was repeated. I was thinking something this:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
string str;
vector<string> vecstr;
	while(cin >> str)
	{
		vecstr.push_back(str);

		for(vector<string>::iterator iter = vecstr.begin(); iter != vecstr.end() - 1; ++iter)
		{
			if(*iter == *(iter + 1))
			{
				// do something...
			}
			else
			{
				// do something else 
			}
		}
	}
				
	system("pause");
	return 0;
}
But that's not really it :).

Share this post


Link to post
Share on other sites
Advertisement
We can't help you with homework, but we can give you a nudge in the right direction.

In this case, all I'll say is that the variable 'str' contains the possible duplicate. Your current loop only compares adjacent elements in the vector.

See if you can figure it out from that.

[Edited by - rip-off on October 26, 2007 7:30:01 AM]

Share this post


Link to post
Share on other sites
Quote:
to terminate the loop if a word occurs twice in succession


You don't need to remember all the words that were read, only the current and previous one - hence no need for vector.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Quote:
to terminate the loop if a word occurs twice in succession


You don't need to remember all the words that were read, only the current and previous one - hence no need for vector.


In which case my original post is wrong. [embarrass]

Share this post


Link to post
Share on other sites
Thanks for the replies I'll see what i can do!I think I have to revise the while(cin>>some_string) part -> lets say I input three words, where are these words stored?

Share this post


Link to post
Share on other sites
The while loop construct is fine. You only need to hold on to two strings at a time. Each iteration of the loop reads the next string waiting in the standard input stream in to some_string.

Have you got an algorithm worked out on paper, yet? A simple flow chart or some pseudocode might help.

Share this post


Link to post
Share on other sites
As has been said, you only need to track two strings, since you're comparing only adjacent words. For every word after the first, compare it to the word you read in previously and see if they are the same.

Once you run out of words or the new word matches the previous word, echo the information and you're done.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement