471,595 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

How to store words?

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.

Apr 1 '06 #1
7 8961
<ab*****@budweiser.com> wrote:
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;
Apr 1 '06 #2
In article <11**********************@g10g2000cwb.googlegroups .com>,
"ab*****@budweiser.com" <ab*****@budweiser.com> wrote:
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?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 1 '06 #3
#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.

Apr 1 '06 #4

<ab*****@budweiser.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
string word;
int w;
cout << "How many words? ";
cin >> w;
vector<string> words;
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
Apr 1 '06 #5
In article <11*********************@i39g2000cwa.googlegroups. com>,
"ab*****@budweiser.com" <ab*****@budweiser.com> wrote:
#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 ) );
}
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 1 '06 #6

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?

Apr 1 '06 #7
In article <11**********************@e56g2000cwe.googlegroups .com>,
"ma740988" <ma******@gmail.com> wrote:
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'.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 1 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mich | last post: by
18 posts views Thread by siddharthkhare | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.