Hi everyone,

I've been having a little trouble with an assignment I've been given and I'm looking for a little help. The assignment requires me to read in a line of text from the user, and then use Parallel Arrays (both iteratively & recursively), An Array of Object (iteratively, recursively, and pointer recursive), and an STL Vector (iteratively, and a for loop) to count the number of occurrences in the words that the user has entered.

I have a good grasp on parallel arrays, stl vectors, and the array of object, but my main problem is getting a method to input the text, parse it, and then print out how many times each word appeared. I honestly have been stumped forever and could use some direction. I've posted my (as of now completely useless) code below:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, char **argv)
{
        string input;

        cout << "Hello! Please input your text: ";
        getline(cin, input);
        
        system("pause");
        
        return 0;
}

If anybody has any suggestions, I'd be very thankful. I'm just trying to find any direction I can right now, I'm just totally lost on what seems like the most simple part of the assignment.

Finally, I searched for a half hour and couldn't find anything that applies to this particular problem, and apologize beforehand if I've ignorantly asked a question that's been answered many times before. If I have, if someone could be so kind as to point me in the right direction, I'd be greatly appreciative.

Thanks again.

I see that you already include <sstream>. So do you know how stringstreams work? If yes: That's one way of doing it.
- take input in a string
- create a stringstream and load the inputstring in it
- use the >> operator to extract one word from the stringstream. The >> automaticly delimits on a space
- store the word somewhere (in a vector or a map or...)

Thank you very much for your input niek_e, your suggestion certainly got some gears moving, and that is the direction I believe I'm supposed to be taking my assignment in. You've been a big help and I appreciate it.

However, when it comes to stringstreams perhaps I don't know them as well as I should. I was under the impression that if, for example, the input line was "I have no skills" and I fed the line into the stream and used the stream to store it in an array(for example), it would feed in "I" to the array. Then if I were to feed in the line again, rather than store "have" to the array, which is the desired result, it would store a second "I". A thought that just caught me as I'm typing this is that I could use push_back()(or something similar) to move the "I" to the back of the line (making it appear as "have no skills I"), and using some method of counting the words to make sure I cycle through the entire line completely.

Does anyone have any thoughts on this? Or am I taking this in completely the wrong direction? Thanks in advance.

Also, I would want to use istringstream, not just stringstream since I'm going through user input, right? I hadn't even thought about that before.

As much as I hate to triple post, I can't find a way to edit my previous posts. But I have gotten a good input method to writing to an array. My problem now is creating a parallel array with the occurrences. All I'm getting for the occurrences are a load of garbage.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main()
{ 
  string line;
  getline(cin, line);
  istringstream is(line);
  string list[10];
  const int capacity=10;
  int occurances[capacity];
  
  for (int i=0; i<10; i++)
      {
      is >> list[i];
      ++occurances[i];
      }
  for (int c=0; c<10; c++)
      {
      cout << list[c] << " " << occurances[c] << endl;
      }
system("pause");
}

Again, any help provided would be greatly appreciated.

you need to initialize each element of occuarnces to zero before you try to increment them.

I suspect you want to loop through list to see if the word was found before and if not then add it to the array.

for each word in list
  if current word equals this word of list
    increment element of occurances with same index 
    change a bool flag's value to true
    break out of loop
if bool flag is false
   add current word to end of list
   increment element of occurances with same index as list
else
   reset boolean flag to false

That helped a ton, thanks a bundle Lerner. Unfortunately, I can't seem to get it to compile now, and can't see what exactly is wrong. I feel like I'm so close, but I just can't get through that final wall. Based on the top-notch help I've gotten from everyone so far I figured I'd try here again. Here's what I have so far:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

void parallelIterative(string input);

int main()
{
        string input;
        int choice;

        cout << "Hello! Please input your text: ";
        fflush(stdout);
        getline(cin, input);
        cout << "Thank you." << endl;
        cout << "Please choose the number of how to process your text" << endl;

        cout << "1 - Parallel Array Iterative" << endl;
        cout << "2 - Parallel Array Recursive" << endl;
        cout << "3 - Object Array Iterative" << endl;
        cout << "4 - Object Array Recursive" << endl;
        cout << "5 - Object Array Pointer Recursive" << endl;
        cout << "6 - STL Vector 'For' Loop" << endl;
        cout << "7 - STL Vector Iterator" << endl;
        cout << "8 - Exit" << endl;
        fflush(stdout);
        cin >> choice;
        while (choice>8)
                {
                cout << "I'm sorry, that is not a valid choice."<<endl;
                cout << "Please try again: ";
                cin >> choice;           
                }
        switch(choice)
        {
        case 1:
             cout << "You chose Parallel Array Iterative" << endl;
             parallelIterative(input);
             break;
        case 2:
             cout << "You chose Parallel Array Recursive" << endl;
             //put parallel recursive function here
             break;
        case 3:
             cout << "You chose Object Array Iterative" << endl;
             //put object iterative function here
             break;
        case 4:
             cout << "You chose Object Array Recursive" << endl;
             //put object recursive function here
             break;
        case 5:
             cout << "You chose Object Array Pointer Recursive" << endl;
             //put object pointer recursive function here
             break;
        case 6:
             cout << "You chose STL Vector 'For' Loop" << endl;
             //put STL vector for loop function here
             break;
        case 7:
             cout << "You chose STL Vector Iterator" << endl;
             //put STL vector iterator function here
             break;
        default:
                cout << "You chose to exit.  Have a nice day!" << endl;
                break;
        }
        
        system("pause");
        
        return 0;
}

void parallelIterative (string input);
  string input;
  istringstream is(input);
  string list[10];
  int occurances[10]={0};
  bool repeated;
  
      is >> list[0];

  for (int i=1; i<10; i++)
      {
           if (list[i]==list[i-1])
              {
               ++occurances[i-1];
               repeated = true;
               }
               if (repeated == false)
               {
                is >> list[i];
                ++occurances[i];
                }
           else
               repeated = false;
  for (int c=0; c<10; c++)
      {
      cout << list[c] << " " << occurances[c] << endl;
      }

Does anybody have any suggestions? The program itself seems to compile, it seems like it's my function that isn't quite all there, or what is there is incorrect.

Again, any help would be greatly appreciated, thanks for everything so far, I've learned a ton.

I don't see brackets surronding the parralelIterative function. If this explains the error, it will be good. Also, a semi-colon after the parralelIterative line is not correct.

Thanks for the response. I've actually solved the problem (after a long sleepless night), and I did come across what you mentioned.

However, thank you for taking the time to look into my problem and help me out, I do appreciate it. Also, thanks for the coding tip, anything that makes my coding better helps.

Again, thanks for your time and effort.

This question has already been answered. Start a new discussion instead.