vector strings and ouputing user input

Feb 3, 2012 at 5:46pm
I am trying to write a program where i let the user input a string, and compare it to see which words where used the most least and the mode, i having trouble with the vector words, when i push_back the input it evaluates it as One, how do i get the string to be evaluated by the words, and also just to test trying to output the words in the string in order from lowest to highest






#include<iostream>
#include<vector>
#include <algorithm>
#include<numeric>
#include<string>

using namespace std;

int main()
{

string resp;

vector<string>words;


cout<<"Please type in some words\n";
getline(cin, resp);
cout<<resp<<'\n';


words.push_back(resp);
sort(words.begin(),words.end(),);
cout<<"the length of your string is: "<<resp.length()<<'\n';

for(int i=0; i<words.size();++i)
cout<<words[i]<<'\n';//trying to output words in the string
Last edited on Feb 3, 2012 at 5:47pm
Feb 3, 2012 at 10:36pm
Are they to enter the words in one line like..

 
The rabbit rabbit had more than one two rabbit The


Or type a word, hit enter, type another word hit enter etc.

What you have so far looks like the input is supposed to have an array of words? After further reading your question I think what you are asking is why are the multiple words I'm typing in evaluated as one word, instead of multiple.

Once you get your resp from the user, you can then split that string based on some delimeter, like space or commas pushing back each individual word.

I hope this helps.
Feb 4, 2012 at 1:36am
Have the user input the line with words seperated with commas, like this:
you, me, you, them, i , you, they, few

You would then iterate through the string using a loop, like this.
1
2
3
4
5
6
7
8
9
10
unsigned int count(0);
string in;
while (count < resp.length()){
    in = in + resp[count];
    if (resp[count] == ',') {
        words.push_back(in);
        in = "";
    }
    count++;
}

That will load the vector, ready for you to do all of the arcane and bizarre things you intend to do to it! :)
Last edited on Feb 4, 2012 at 1:38am
Feb 4, 2012 at 2:24am
well here's what the programming principles and practices using c++ book is asking "write a program that finds the min, max, mode of a sequence of strings" is there a way that you can store the words from the string in the vector, i know when i run the program it evaluates just the string that's why i get one, im trying to store the words from the string in the vector, but i appreciate the help and will try your suggestions.
Last edited on Feb 4, 2012 at 2:27am
Topic archived. No new replies allowed.