Greetings,

I'm quite new to C++ and i got a problem with my program. It seperates words in a string using the strtok() function. These words are then put into an array, which means the array can contain multiple same words. I'd like to copy different words from this array to a new one, in which there'd be only ONE of each word. I'm out of ideas, so any help would be much appriciated.

Thanks

Do you need the first array or is it just an intermediate for the array of unique words? If it's the latter, you'd be better off using a suitable data structure that doesn't allow duplicates from the get go, like a std::set:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
#include <string>

int main()
{
  std::set<std::string> words;
  std::string word;

  while ( std::cin>> word )
    words.insert ( word );

  copy ( words.begin(), words.end(),
    std::ostream_iterator<std::string> ( std::cout, "\n" ) );
}

Thanks for your effort, but I'd kinda preffer a solution using arrays (if you happen to know one) or something as simple as them, since, like I said, I'm quite new to programming and therefore I don't understand most of your solution.

>I'd kinda preffer a solution using arrays (if you happen to know one)
I do happen to know one. In fact, I know several, but since you say you're new to programming, I'm reasonably sure that all of them would confuse you even more than my set example. Though this is a naive solution you might not have trouble with, but it's basically the worst possible solution:

#include <iostream>
#include <string>

int main()
{
  const int size = 10;

  std::string words[size];
  std::string word;
  int total = 0;

  while ( total < size && std::cin>> word )
    words[total++] = word;

  std::string unique[size];
  int n = 0;

  for ( int i = 0; i < total; i++ ) {
    int j;

    /* Check for a duplicate */
    for ( j = 0; j < n; j++ ) {
      if ( words[i] == unique[j] )
        break;
    }

    if ( j == n )
      unique[n++] = words[i];
  }

  for ( int i = 0; i < n; i++ )
    std::cout<< unique[i] <<'\n';
}

>or something as simple as them
I never understood this request. The solution using arrays is more complicated because you'll end up manually writing all of the logic to remove duplicates. If you do it efficiently and robustly, the code is far from simple, and probably well beyond a beginner. On the other hand, you don't have to understand the internals of std::set to use it as a collection of unique items, and the interface isn't difficult.

>I'm quite new to programming and therefore I don't understand most of your solution.
Last I remember, that wasn't an excuse to discard a solution. If you don't understand something, make an effort to figure out how it works. That's how you learn.

Since you're using strtok() to separate the words, you're probably using a null terminated char array (C style string) as the original string and not using a STL string class object to hold the original string. If that's correct then instead of using the == operator to compare two C style strings you would usually use something like strcmp(). It's in the cstring header file (or the string.h header file if you must) along with other standard functions to manipulate C style strings instead of STL string class objects. It's also possible to compare C style strings char by char for equivalency if you're not allowed to use standard, prewritten functions or classes, but that's taking things a little too far, unless it is done as part of a training exercise.

Thanks alot, both !

in line 12 " while ( total < size && std::cin>> word )", why do you include "&& std::cin>>word."

Why would this be a condition of the while statement?

Shouldn't it be a statement that is executed if the while statement is true?

>Why would this be a condition of the while statement?
Because if cin fails to read a word, I don't want to assign the nonexistent word to the array. The condition reads "while there's room in the array and we got another word from input".

>Why would this be a condition of the while statement?
Because if cin fails to read a word, I don't want to assign the nonexistent word to the array. The condition reads "while there's room in the array and we got another word from input".

ok. I didn't realize that you were able to execute code inside the conditional section of a while statement. I suppose it is just like a for statement then that looks like for(i = 0, i < 10, i++). I am going to take a guess that this works with if statements as well?

why might cin fail to read the word anyhow?>

>I am going to take a guess that this works with if statements as well?
Good guess.

>why might cin fail to read the word anyhow?
A common example is if you want to break the loop before the array is full. Signal end-of-file before typing any characters and it'll put the stream in a failure state.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.