|
-
Feb 23rd, 2002, 06:56 PM
#1
Thread Starter
Frenzied Member
simple string modification
Say i had a string such as:
"1 -2.5 6"
(without the quotes) and I wanted to set the three numbers to a, b, and c. How would I do that?
Last edited by markman; Feb 23rd, 2002 at 07:00 PM.
retired member. Thanks for everything 
-
Feb 23rd, 2002, 07:17 PM
#2
New Member
I think you have to write a function that input a string and generates array and you can split the string by " " (remember strings are also array). I do not have time now if noone answer your question I will write you an sample code...
yilmaz..
-
Feb 23rd, 2002, 07:56 PM
#3
Thread Starter
Frenzied Member
yeah but how do I split the string by " "?
retired member. Thanks for everything 
-
Feb 24th, 2002, 04:21 AM
#4
transcendental analytic
1. while in the string
2. find " "
3. add the substring between the occurances of " " to a list
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 24th, 2002, 04:51 PM
#5
Monday Morning Lunatic
Also:
Code:
istringstream iss("5 -2.5 6");
float a, b, c;
iss >> a >> b >> c;
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 24th, 2002, 07:23 PM
#6
Thread Starter
Frenzied Member
istringstream comes from what header file?
retired member. Thanks for everything 
-
Feb 24th, 2002, 08:40 PM
#7
Fanatic Member
Parksie: what exactly do istringstream and ostringstream do?
Alcohol & calculus don't mix.
Never drink & derive.
-
Feb 25th, 2002, 12:06 PM
#8
Monday Morning Lunatic
Code:
#include <strstream>
Stringstreams are specialisations of istream and ostream for string objects.
Instead of formatting onto stdout, they format into a string, replacing sprintf and sscanf.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 25th, 2002, 01:28 PM
#9
Thread Starter
Frenzied Member
how come this wont work then?
make a file Test.in with the text:
3
1 2 1
1 1 1
1 -2.5 6
then put this code in a project:
Code:
#include <fstream.h>
#include <iostream.h>
#include <strstream>
struct ABC {
double a;
double b;
double c;
};
void main() {
char* inFilename = "Test.in";
char* outFilename = "Test.out";
ifstream infile(inFilename);
ofstream outfile(outFilename);
char szText[20] = "";
int i = 0;
istringstream splitter = "";
ABC vABC;
while (infile >> szText) {
i++;
if (i > 1) {
/////////////////////////////
splitter = szText;
splitter >> vABC.a >> vABC.b >> vABC.c;
cout << vABC.a << endl;
cout << vABC.b << endl;
cout << vABC.c << endl;
/////////////////////////////
}
}
}
I get 15 errors from the header file.
retired member. Thanks for everything 
-
Feb 25th, 2002, 03:39 PM
#10
a) to get a complete line of the file you should use getline (the global version)
b) C++ headers with .h are deprecated. You should use the ones without the .h instead. Important: NEVER mix the two types.
c) C++ headers without the .h define everything in the std namespace. You can either
- always use std:: when accessing one of the header elements (e.g. std::cout << "Hello" << std::endl
- use using declarations to make specific parts accessible (e.g.
using std::cout;
cout << "Hello" << std::endl; // endl is still not accessible
// you could change that with using std::endl
)
- include the whole namespace by doing
using namespace std;
after including all headers.
This should solve at least 14 of the 15 errors you have.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 26th, 2002, 01:11 PM
#11
Thread Starter
Frenzied Member
then how come this wont work?
using the same file as im my previous post:
Code:
#include <fstream>
#include <iostream>
#include <strstream>
using namespace std;
struct ABC {
double a;
double b;
double c;
};
void main() {
char* inFilename = "Test.in";
char* outFilename = "Test.out";
ifstream infile(inFilename);
ofstream outfile(outFilename);
char szText[20] = "";
int i = 0;
istringstream iss;
ABC vABC;
while (infile.getline(szText,20,' ')) {
i++;
if (i > 1) {
/////////////////////////////
iss = szText;
iss >> vABC.a >> vABC.b >> vABC.c;
cout << vABC.a << endl;
cout << vABC.b << endl;
cout << vABC.c << endl;
/////////////////////////////
}
}
}
retired member. Thanks for everything 
-
Feb 26th, 2002, 02:04 PM
#12
Monday Morning Lunatic
Not sure about that one...try creating the istringstream at that point.
What compiler errors/logic problems do you have?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 26th, 2002, 07:07 PM
#13
Fanatic Member
Originally posted by parksie
Also:
Code:
istringstream iss("5 -2.5 6");
float a, b, c;
iss >> a >> b >> c;
So what exactly does this do?
Alcohol & calculus don't mix.
Never drink & derive.
-
Feb 27th, 2002, 12:59 PM
#14
Thread Starter
Frenzied Member
Code:
#include <strstream>
using namespace std;
void main() {
istringstream iss("5 -2.5 6");
float a, b, c;
iss >> a >> b >> c;
}
Even this doesnt work
retired member. Thanks for everything 
-
Mar 1st, 2002, 01:41 PM
#15
Thread Starter
Frenzied Member
bump
Hurry up! I need to know how to do this by february next year!
retired member. Thanks for everything 
-
Mar 1st, 2002, 02:00 PM
#16
Monday Morning Lunatic
That's a whole 11 months 
I don't have a compiler at the moment so I wouldn't know 
How about a << iss or something?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 1st, 2002, 02:19 PM
#17
when I do
istringstream iss;
it gives me this error:
error C2079: 'iss' uses undefined class 'basic_istringstream<char,struct std::char_traits<char>,class std::allocator<char> >'
In the help, istringstream is defined as
typedef basic_istringstream<char> istringstream;
but there is no header file that contains this line. In <strstream> there is the class istrstream, which is derived from istream, which is the char-specialization of basic_istream.
So you probably must use this class.
Here is the modified code (compiles and runs with correct output on my pc):
Code:
#include <strstream>
#include <iostream>
using namespace std;
void main()
{
istrstream iss("5 -2.5 6");
float a, b, c;
iss >> a >> b >> c;
cout << a << " " << b << " " << c << endl;
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 1st, 2002, 02:34 PM
#18
Monday Morning Lunatic
Try <sstream> I may have been thinking insanely last time...
istrstream is the old type, don't use it (it's easy to get huge memory leaks if you don't know how it works).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 2nd, 2002, 04:09 AM
#19
Works! Here's your solution markman!
btw parksie, istrstream comes from the new template-based standard library too, why is it bad?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 2nd, 2002, 05:46 AM
#20
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 2nd, 2002, 08:45 AM
#21
Code:
typedef basic_istream<char, char_traits<char> > istream;
class istrstream : public istream // ...
vs.
Code:
template<class _E,
class _Tr = char_traits<_E>,
class _A = allocator<_E> >
class basic_istringstream : public basic_istream<_E, _Tr> // ...
typedef basic_istringstream<char, char_traits<char>, allocator<char> > istringstream;
It's not THAT much difference, both come from basic_istream
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 2nd, 2002, 08:56 AM
#22
Monday Morning Lunatic
Hmmm...didn't look like that when I looked at it.
But either way, stick to the longer version...it makes more difference in output stringstreams:
char* ostrstream::str()
as opposed to
string ostringstream::str()
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 2nd, 2002, 02:00 PM
#23
good point
I wish the STL documentation of VC++ was better...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 2nd, 2002, 09:07 PM
#24
Monday Morning Lunatic
I prefer the dinkumware documentation, it's well good (plus they sell a standard library replacement for visual c++).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 3rd, 2002, 11:41 AM
#25
Thread Starter
Frenzied Member
Ok. How would i get the string from a text file and then split it? I cant seem to be able to set the iss = the text from the text file.....
retired member. Thanks for everything 
-
Mar 3rd, 2002, 12:40 PM
#26
Monday Morning Lunatic
Code:
ifstream file("whatsit.txt");
file >> a >> b >> c;
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 3rd, 2002, 04:00 PM
#27
Thread Starter
Frenzied Member
I should have asked that at the beginning
retired member. Thanks for everything 
-
Mar 3rd, 2002, 04:42 PM
#28
Monday Morning Lunatic
Dead nice, these streams - they all work in exactly the same way
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 4th, 2002, 10:48 AM
#29
I don't see the need to buy an additional STL when I already paid for the VC++ one. (I have all this stuff for private use, no company that pays my programs)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 4th, 2002, 11:52 AM
#30
Monday Morning Lunatic
It's called standards compliance, which is a big thing in a corporate environment.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 5th, 2002, 06:29 AM
#31
Documentation of VC++ Standard Library says i/ostringstream <sstream> is for string objects, i/ostrstream <strstream> is for C-style strings...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|