How to store words?

A

abraxas

Ok maybe someone here can help me. I have been learning c++ and have
been toying with some basic programming and have run into something
that I would have assumed would have been fairly easy. I am wanting to
input a list of words into the program and I thought you would use and
array to do that, but I am not having any luck finding info on storing
an actual list of words. I have set it up so that it prompts you for
the number of words. Then it enters a while loop and prompts you for
each of the words, It is all working so far but my problem arrises here
I am storing each word under the same string called word. Shouldn't I
be able to simply assign that string to a position in an array and
automatically increment it to the next spot when I enter the next word
so I don't keep overwriting the same string variable with each word.
Sorry if this was confusing, was struggling with how to word this.
 
O

osmium

Ok maybe someone here can help me. I have been learning c++ and have
been toying with some basic programming and have run into something
that I would have assumed would have been fairly easy. I am wanting to
input a list of words into the program and I thought you would use and
array to do that, but I am not having any luck finding info on storing
an actual list of words. I have set it up so that it prompts you for
the number of words. Then it enters a while loop and prompts you for
each of the words, It is all working so far but my problem arrises here
I am storing each word under the same string called word. Shouldn't I
be able to simply assign that string to a position in an array and
automatically increment it to the next spot when I enter the next word
so I don't keep overwriting the same string variable with each word.
Sorry if this was confusing, was struggling with how to word this.

Vectors are more the C++ way to do things than vectors. There is a member
function "push_back" that does what you seem to want. The declaration
might be something like this:

vector<string> v;
 
D

Daniel T.

Ok maybe someone here can help me. I have been learning c++ and have
been toying with some basic programming and have run into something
that I would have assumed would have been fairly easy. I am wanting to
input a list of words into the program and I thought you would use and
array to do that, but I am not having any luck finding info on storing
an actual list of words. I have set it up so that it prompts you for
the number of words. Then it enters a while loop and prompts you for
each of the words, It is all working so far but my problem arrises here
I am storing each word under the same string called word. Shouldn't I
be able to simply assign that string to a position in an array and
automatically increment it to the next spot when I enter the next word
so I don't keep overwriting the same string variable with each word.
Sorry if this was confusing, was struggling with how to word this.

I can see your struggle. It seems as though English is not your first
language. How about you show us with code?

What do you have now? What is it's output? What do you want the output
to be?
 
A

abraxas

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

int main()
{
string word;
int w;
cout << "How many words? ";
cin >> w;
while (w > 0)
{
cout << "Word # " << w <<":";
cin >> word;
w--;
}

return 0;
}
There is the code.
 
M

Mike Wahler

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

int main()
{
string word;
int w;
cout << "How many words? ";
cin >> w;

vector said:
while (w > 0)
{
cout << "Word # " << w <<":";
cin >> word;
words.push_back(word);

w--;
}

cout << "Words:\n";
for(vector<string>::const_iterator it = words.begin();
it != words.end(); ++it)
{
cout << *it << '\n';
}
return 0;
}
There is the code.

-Mike
 
D

Daniel T.

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

int main()
{
string word;
int w;
cout << "How many words? ";
cin >> w;
while (w > 0)
{
cout << "Word # " << w <<":";
cin >> word;
w--;
}

return 0;
}
There is the code.

There's a couple of ways to go with this. First things first, you have a
word variable that is declared at the top of main, and used once in
(about) the middle. Let's move that together.

int main() {
cout << "How many words ? ";
int w;
cin >> w;
while ( w > 0 ) {
cout << "Word #" << w << ": ";
string word;
cin >> word;
--w;
}
}

After extracting the word, we have to put it somewhere before the next
word is red. This is exactly what a vector is for
<http://www.sgi.com/tech/stl/Vector.html>

int main() {
cout << "How many words ? ";
int w;
cin >> w;
vector<string> words;
while ( w > 0 ) {
cout << "Word #" << w << ": ";
words.push_back( string() );
cin >> words.back();
--w;
}
}

Now for a little more advanced class... What you are doing is generating
a number of words by querying the user. So lets move the code that
generates the words into a separate block.

struct word_getter {
int count;
word_getter( int c ): count( c ) { }
string operator()() {
cout << "Word# " << count << ": ";
string word;
cin >> word;
--count;
return word;
}
};

int main() {
vector<string> words;
cout << "How many words ? ";
int w;
cin >> w;
generate_n( back_inserter( words ), w, word_getter( w ) );
}
 
M

ma740988

Daniel T. wrote:
[ ... ]

|| cin >> words.back();
If I had to explain this. Would the following make sense.

std::vector doesn't define an operator >>, but cin does. The fact
that it's a vector of strings is I suspect irrelevant, in that
words.back gives you a string object?
 
D

Daniel T.

"ma740988 said:
Daniel T. wrote:
[ ... ]

|| cin >> words.back();
If I had to explain this. Would the following make sense.

std::vector doesn't define an operator >>, but cin does. The fact
that it's a vector of strings is I suspect irrelevant, in that
words.back gives you a string object?

It makes more sense if looked at in conjunction with the line above it:

words.push_back( string() );
// puts a blank string at the back of the array
cin >> words.back();
// fills the last string in the array with the user's input

So the fact that it is a vector of strings is very important. It tells
cin what kind of value to read from the user. If for example we had:

vector<int> vec;
vec.push_back( 0 );
cin >> vec.back();

'cin' would read any input as an integer because that is what the
reference returned from 'back()' is.


Another way to do it would be:

string s;
cin >> s;
words.push_back( s );

Doing it the way I suggested gets rid of the temporary 's'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top