473,390 Members | 1,273 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,390 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 9318
<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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.