473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 9310
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mich | last post by:
Hi, Looking for a store locator in javascript, with data for 80 stores in the code, with other words, no external database, server will not run php. Will pay if nescessary. Urgent.
11
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am...
5
by: lawrence k | last post by:
I'm a little weak on my basic I/O. Help me out please. Is it right to say that I can just open any file with file(), get it as a string, and then store in a MySql database, in, say, a MediumText...
18
by: siddharthkhare | last post by:
Hi All, what is the diference between these two cache control header. no-cache and no-store. I have read the w3.org explanation. So lets say I am using only no-cache ....my understanding is...
4
by: AViS | last post by:
Hi, I am building a language translator, that must convert input from source languages to a language neutral format in XML. This XML must be read by the target language translator and produce the...
0
by: rxding | last post by:
Can Java Store Procedure increase performance Hello, Performance reason we need to move some of our code into database. Java Store Procedure is given the first choice. However, while...
2
by: Ryan | last post by:
I am new to .net 2.0 and was wondering how best to leverage its membership capabilities to handle a situation similar to the following: We have a 4 web applications belonging to 3 departments. ...
3
by: charlz | last post by:
im been having difficuties in reading and storing from an input file. the problem goes like this: Write a program that reads in a whole bunch of words from all file (each word is no more than 20...
8
by: hanna88 | last post by:
hello.. is there a way of storing data to other class object. here's the problem. class Repository is where it reads through the sentences and tokenize it into words . and what i did is by using...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.