Hi! I've taken my first steps into the world of c++ by trying to write a
text adventure game. Things are proceeding fine, but there's some code
in there that isn't very well coded. More specifically, I use the
following code:
...
string word [4];
size_t pos = action.find(" ");
word[0] = action.substr (0,pos);
if (pos < action.size() - 2)
{
word[1] = action.substr(pos + 1);
}
string word1 [2];
for (int i=1;i<3;i++)
{
pos = word[i].find(" ");
if (pos == string::npos) goto skippy;
word1[i-1] = word[i].substr (0,pos);
if (pos < word[i].size() - 2)
{
word[i+1] = word[i].substr(pos + 1);
}
word[i] = word1[i-1];
}
skippy:
...
To convert a string called 'action' into the array word[4]. Is there a
way to do this more efficiently?
Thanks 5 9087
Frederik Van Bogaert wrote:
[..]
To convert a string called 'action' into the array word[4]. Is there a
way to do this more efficiently?
Not sure about efficiency. But elegance is relatively easy to come by.
All you need to do is take your string, construct a stringstream out of
it and then read all the words from it until end-of-file.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
"Frederik Van Bogaert" <fr*****************@student.kuleuven.bewrote in
message news:f0**********@ikaria.belnet.be...
Hi! I've taken my first steps into the world of c++ by trying to write a
text adventure game. Things are proceeding fine, but there's some code in
there that isn't very well coded. More specifically, I use the following
code:
...
string word [4];
size_t pos = action.find(" ");
word[0] = action.substr (0,pos);
if (pos < action.size() - 2)
{
word[1] = action.substr(pos + 1);
}
string word1 [2];
for (int i=1;i<3;i++)
{
pos = word[i].find(" ");
if (pos == string::npos) goto skippy;
word1[i-1] = word[i].substr (0,pos);
if (pos < word[i].size() - 2)
{
word[i+1] = word[i].substr(pos + 1);
}
word[i] = word1[i-1];
}
skippy:
...
To convert a string called 'action' into the array word[4]. Is there a way
to do this more efficiently?
Thanks
Use. std::stringstream. stringstream will read using >like std::cin,
stopping on spaces.
Output of following program is:
look
at
the
clock
on
the
wall
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
// typedef is not required, just makes it a little eaiser.
typedef std::vector<std::stringWordVec;
int main()
{
std::string action = "look at the clock on the wall";
WordVec Words;
// Following declares a stringstream Parse and initializes it to the
contents of action
std::stringstream Parse( action );
// Now we read each word out one by one til there's no more and stuff
them into our vector
std::string word;
while ( Parse >word )
Words.push_back( word );
// At this point each word is in the vector.
for ( WordVec::iterator it = Words.begin(); it != Words.end(); ++it )
std::cout << (*it) << "\n";
std::string wait;
std::getline( std::cin, wait );
}
Jim Langston wrote:
"Frederik Van Bogaert" <fr*****************@student.kuleuven.bewrote in
message news:f0**********@ikaria.belnet.be...
>Hi! I've taken my first steps into the world of c++ by trying to write a text adventure game. Things are proceeding fine, but there's some code in there that isn't very well coded. More specifically, I use the following code:
... string word [4]; size_t pos = action.find(" "); word[0] = action.substr (0,pos); if (pos < action.size() - 2) { word[1] = action.substr(pos + 1); } string word1 [2];
for (int i=1;i<3;i++) { pos = word[i].find(" "); if (pos == string::npos) goto skippy; word1[i-1] = word[i].substr (0,pos); if (pos < word[i].size() - 2) { word[i+1] = word[i].substr(pos + 1); } word[i] = word1[i-1]; }
skippy: ...
To convert a string called 'action' into the array word[4]. Is there a way to do this more efficiently? Thanks
Use. std::stringstream. stringstream will read using >like std::cin,
stopping on spaces.
Output of following program is:
look
at
the
clock
on
the
wall
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
// typedef is not required, just makes it a little eaiser.
typedef std::vector<std::stringWordVec;
int main()
{
std::string action = "look at the clock on the wall";
WordVec Words;
// Following declares a stringstream Parse and initializes it to the
contents of action
std::stringstream Parse( action );
// Now we read each word out one by one til there's no more and stuff
them into our vector
std::string word;
while ( Parse >word )
Words.push_back( word );
// At this point each word is in the vector.
for ( WordVec::iterator it = Words.begin(); it != Words.end(); ++it )
std::cout << (*it) << "\n";
std::string wait;
std::getline( std::cin, wait );
}
Thanks! That's exactly what I was looking for :-)
PS: Is there some reason you're using "std::" all the time instead of
specifying "using namespace std;" in the beginning of the file as I've
been told to do?
Jim Langston wrote:
[..]
// Now we read each word out one by one til there's no more and
stuff them into our vector
std::string word;
while ( Parse >word )
Words.push_back( word );
Replace the three lines above with this:
std::copy(std::istream_iterator<std::string>(Parse ),
std::istream_iterator<std::string>(),
std::back_inserter(Words));
>
// At this point each word is in the vector.
for ( WordVec::iterator it = Words.begin(); it != Words.end();
++it ) std::cout << (*it) << "\n";
Replace the loop with this:
std::copy(Words.begin(), Words.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
>
std::string wait;
std::getline( std::cin, wait );
}
And you get an even more radical standard-library based program!
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
"Frederik Van Bogaert" <fr*****************@student.kuleuven.bewrote in
message news:f0**********@ikaria.belnet.be...
Jim Langston wrote:
>"Frederik Van Bogaert" <fr*****************@student.kuleuven.bewrote in message news:f0**********@ikaria.belnet.be...
>>Hi! I've taken my first steps into the world of c++ by trying to write a text adventure game. Things are proceeding fine, but there's some code in there that isn't very well coded. More specifically, I use the following code:
... string word [4]; size_t pos = action.find(" "); word[0] = action.substr (0,pos); if (pos < action.size() - 2) { word[1] = action.substr(pos + 1); } string word1 [2];
for (int i=1;i<3;i++) { pos = word[i].find(" "); if (pos == string::npos) goto skippy; word1[i-1] = word[i].substr (0,pos); if (pos < word[i].size() - 2) { word[i+1] = word[i].substr(pos + 1); } word[i] = word1[i-1]; }
skippy: ...
To convert a string called 'action' into the array word[4]. Is there a way to do this more efficiently? Thanks
Use. std::stringstream. stringstream will read using >like std::cin, stopping on spaces.
Output of following program is:
look at the clock on the wall
#include <iostream> #include <string> #include <vector> #include <sstream>
// typedef is not required, just makes it a little eaiser. typedef std::vector<std::stringWordVec;
int main() { std::string action = "look at the clock on the wall";
WordVec Words; // Following declares a stringstream Parse and initializes it to the contents of action std::stringstream Parse( action );
// Now we read each word out one by one til there's no more and stuff them into our vector std::string word; while ( Parse >word ) Words.push_back( word );
// At this point each word is in the vector.
for ( WordVec::iterator it = Words.begin(); it != Words.end(); ++it ) std::cout << (*it) << "\n";
std::string wait; std::getline( std::cin, wait ); }
Thanks! That's exactly what I was looking for :-)
PS: Is there some reason you're using "std::" all the time instead of
specifying "using namespace std;" in the beginning of the file as I've
been told to do?
Because I feel that "using namespace std;" defeats the purpose of namespaces
to begin win. Check out the FAQ: http://www.parashift.com/c++-faq-lit....html#faq-27.5 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bushido Hacks |
last post by:
Hey c.l.c++ and/or c.g.a.opengl posters,
How do I convert a hexidecimal string, traditionally used for defining
colors with HTML, into a floating point array?
In other words, how do I convert...
|
by: Asbjørn Ulsberg |
last post by:
Hi.
I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0
module to C#. I've managed to convert the VB code to VB.NET,
which gave me the following code:
Option Strict On
Option...
|
by: Marcio Kleemann |
last post by:
I need to force the first letter of each word in a line of text to
uppercase. The text comes from a TextBox control in a Web Form. I'm new to
..NET and am having a problem.
Since I can't modify...
|
by: Gidi |
last post by:
Hi,
I'm writing a C# win application program, and i need to transfer my hebrew
letters from unicode to ascii, now if i use the ascii encoding it writes me
??? instead of the hebrew letter i've...
|
by: Sling |
last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) &
words(s).
word(s,i) returns the ith word in the s(tring), and words(s) returns the
number of words within the s(tring)....
|
by: gg9h0st |
last post by:
i'm a newbie studying php.
i was into array part on tutorial and it says
i'll get an array having keys that from member variable's name
by converting an object to array.
i guessed "i can...
|
by: jerry.upstatenyguy |
last post by:
I am really stuck on this. I am trying to write a string array
containing a "word" and a "definition" to a class called Entry.
Ultimately this will end up in another class called dictionary. No,...
|
by: =?Utf-8?B?cmtwYXQ=?= |
last post by:
i'm calling a C++ DLL from C#.
here's C++ interface:
char* __cdecl SendCmd(void* handle, char* cmd, char* data, char* buffer);
here's my call in C#:
unsafe public static extern byte* SendCmd...
|
by: JackYee123 |
last post by:
Hey,
I need a structure to store a string array in c, for example
Index Content
-------- -----------
0 word1
1 word2
2
3
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |