Newbie string question: how to read off the first char from a string?

D

DTO

I am suppose to read off the number one by one from a string like
1,2,3,4,5 (up to 100 numbers)
to get results
1
2
3
4
5
I have searched the cplusplus.com, but I can't seems to find an answer.
I would greatly appreciate any help.
 
V

Victor Bazarov

DTO said:
I am suppose to read off the number one by one from a string like
1,2,3,4,5 (up to 100 numbers)
to get results
1
2
3
4
5
I have searched the cplusplus.com, but I can't seems to find an answer.
I would greatly appreciate any help.

Just read those commas into a char.

int myint;
char mychar;
while (cin) {
cin >> myint;
if (cin) cin >> mychar;
// do whatever you need with 'myint'
}

V
 
D

DTO

is mychar suppose to my string?
I was doing this to input my string
string ref;
cout<<"Please enter the page reference string"<<endl;
getline(cin, ref);
 
D

DTO

I was doing this to input my string
string ref;
cout<<"Please enter the page reference string"<<endl;
getline(cin, ref); //enter 1,2,3,4,5
//ref now contains 1,2,3,4,5
how should I read the string if I want to use
1 then later 2, then 3 later etc.
sorry for not being clearly enough. Thanks a lot!
 
V

Victor Bazarov

DTO said:
//ref now contains 1,2,3,4,5
how should I read the string if I want to use
1 then later 2, then 3 later etc.
sorry for not being clearly enough. Thanks a lot!

You should probably use istringstream to read numbers from
the string you have.

istringstream is(ref);
int num;
char comma;
is >> num >> comma; // and so on
 
D

DTO

Victor said:
You should probably use istringstream to read numbers from
the string you have.

istringstream is(ref);
int num;
char comma;
is >> num >> comma; // and so on

It won't let me compile with the error message:
data type is incomplete
isstringstearm is(ref);
^ here
 
D

DTO

Sorry, I know this sounds really stupid.
How do I know when to stop if I don't know how long the input is?
 
R

Rennie deGraaf

DTO said:
Sorry, I know this sounds really stupid.
How do I know when to stop if I don't know how long the input is?

When you run out of input on a stringstream instr, instr.eof() will
return true. For example:

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
istringstream in(string("asdfghjkl;"));
char ch;
ch = in.get();
while (!in.eof())
{
cout << ch << endl;
ch = in.get();
}

return 0;
}

Rennie
 
D

DTO

Thx, works perfect. However I have one question that is not related to
the above.
I am trying to deal with some alignment
example
heading1 heading2
heading3
(reserved for 10 char) (reserved for 5 char) (reserver 12
char)
(align left) (align left)
(align right)
I think there is a syntax for it, but I can't seems to find it.
Could you have me with that also?
Many thanks!
 
V

Victor Bazarov

adbarnet said:
You need to look at the iomanip library - setwidth, setprecision etc.

First, please don't top-post.

Second, unfortunately this is one of the serious omissions in the C++
streams, no setwidth or setprecision can help you read a field of any
*particular* width from a stream, so you have to use scanf for that or
resort to extracting fields into separate strings and reading them using
strtod or some such.

If you doubt the statement above, I dare you to write code to extract
two 4-digit integers from the stream that contains '12345678' so that
the first one would be 1234 and the second 5678. Simple, isn't it? I
will even help you:

#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
std::istringstream is("12345678");
int one, two;
????
std::cout << one << ' ' << two << std::endl;
}

Fill in the ???? with extraction from 'is' so that the output of this
programa is
1234 5678

(of course, simple ignoring all input and instead assigning values to
'one' and 'two' is considered cheating and is not acceptable). You're
free to add any headers at will, but if I read your claim correctly,
the ones that are already there should be enough...

V
 
D

DTO

Thank you very much everyone!!
I ended up using setw(width) and cout << setiosflags(ios::left)
 
D

DTO

I have come up with a new question again
First, this is my header of my program

#include <iostream>
#include <string.h>
#include <sstream>
#include <iomanip>

I complie my program in Windows Codewarrior without problems, then I
had to move the code into a Unix Env and compile it with g++. It is
returning a ton of error message now such as string not defined, so I
am guess it cannot include my those header file in my program. What do
I need to fix? thx again!
 
V

Victor Bazarov

DTO said:
I have come up with a new question again
First, this is my header of my program

#include <iostream>
#include <string.h>

This is the [old] C header, which declares 'strlen', 'strcpy' and other
C string functions. If you intend to use 'std::string' class, you need
to include <string>. They are not the same thing.

In the future if you need the C standard library, include headers that
begin with 'c' instead of the ones that end with '.h'. For example,

#include <cstring>
instead of
#include <string.h>

and

#include <cstdio>
instead of
#include <stdio.h>

and so on. Get a good book that describes those in detail.
#include <sstream>
#include <iomanip>

I complie my program in Windows Codewarrior without problems, then I
had to move the code into a Unix Env and compile it with g++. It is
returning a ton of error message now such as string not defined, so I
am guess it cannot include my those header file in my program. What do
I need to fix? thx again!

V
 
D

DTO

thanks again. I changed my <string.h> to <string> and then I added the
line using namespace std. Now it would complie.
However, I have another question now (sorry, I am just that stupid)
I would have to use one argument in the program
example:
../a.out type-of-operation

I am suppose to use the above method but currently I don't know how to
do that and I have to ask a questin for the input of the argument
cout<<"What type of operation?"
getline(cin, ops);
 
R

Rennie deGraaf

DTO said:
thanks again. I changed my <string.h> to <string> and then I added the
line using namespace std. Now it would complie.
However, I have another question now (sorry, I am just that stupid)
I would have to use one argument in the program
example:
./a.out type-of-operation

I am suppose to use the above method but currently I don't know how to
do that and I have to ask a questin for the input of the argument
cout<<"What type of operation?"
getline(cin, ops);

You want to know how to use command-line arguments? Declare main like this:

int main(int argc, char** argv)

Then, argc is the number of arguments, and argv is a list of strings.
Note that the program name is always counted as an argument, so argc is
always at least 1, and argv[0] is the name of the program. If argc >=
2, then argv[1] is the first argument.

For example, this code (untested) will print out the command name and
all arguments:

#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout << "command name: " << argv[0] << endl;
for (int i=1; i<argc; i++)
cout << "argument #" << i << ": " << argv << endl;
return 0;
}

Rennie
 
D

Dylan Nicholson

Victor Bazarov said:
Second, unfortunately this is one of the serious omissions in the C++
streams, no setwidth or setprecision can help you read a field of any
*particular* width from a stream, so you have to use scanf for that or
resort to extracting fields into separate strings and reading them using
strtod or some such.

If you doubt the statement above, I dare you to write code to extract
two 4-digit integers from the stream that contains '12345678' so that
the first one would be 1234 and the second 5678. Simple, isn't it? I
will even help you:

#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
std::istringstream is("12345678");
int one, two;
????

char field[5] = "";
is.read(field, 4);
one = atoi(field); // may need extra header for this, or std::atoi
is >> two;
std::cout << one << ' ' << two << std::endl;
}

Fill in the ???? with extraction from 'is' so that the output of this
programa is
1234 5678

Actually I wouldn't really have done it like the above, but it would a
stretch to claim that it's such a serious omission as to make writing
such code a real pain.

FWIW, I actually have my own std::istream-derived class that is able
to represent a substream of the data, so I could have written it:

substream ss(is.rdbuf(), 4);
ss >> one;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top