Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie string question: how to read off the first char from a string?

Reply
Thread Tools

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

 
 
DTO
Guest
Posts: n/a
 
      02-25-2005
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-25-2005
"DTO" <(E-Mail Removed)> wrote...
>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


 
Reply With Quote
 
 
 
 
DTO
Guest
Posts: n/a
 
      02-25-2005
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);

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-26-2005
"DTO" <(E-Mail Removed)> wrote...
> is mychar suppose to my string?


I don't understand the question.

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


OK. Then what?


 
Reply With Quote
 
DTO
Guest
Posts: n/a
 
      02-26-2005
> 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!

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-26-2005
"DTO" <(E-Mail Removed)> wrote...
>> 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!


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


 
Reply With Quote
 
DTO
Guest
Posts: n/a
 
      02-26-2005

Victor Bazarov wrote:
> "DTO" <(E-Mail Removed)> wrote...
> >> 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!

>
> 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

 
Reply With Quote
 
Rennie deGraaf
Guest
Posts: n/a
 
      02-27-2005
DTO wrote:
> It won't let me compile with the error message:
> data type is incomplete
> isstringstearm is(ref);
> ^ here
>


That could be because you misspelled "istringstream". Assuming that it
was spelled correctly in your actual code, did you remember to #include
<sstream>?

For more information, see
http://www.cplusplus.com/ref/iostream/istringstream/

Rennie
 
Reply With Quote
 
DTO
Guest
Posts: n/a
 
      02-27-2005
I forgot to include <sstream>
it's is working now. Thanks very much!

 
Reply With Quote
 
DTO
Guest
Posts: n/a
 
      02-27-2005
Sorry, I know this sounds really stupid.
How do I know when to stop if I don't know how long the input is?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments