read chars from a string

C

costantinos

Hello. I have a string which looks like
[5 64 3 22 1...] and I need to read these numbers.
The logical implementation was to read the string char by char and
check whether I have a space and when I find one I could create a
vector with all the numbers (after i convert them to number of course).
the problem is that when i am trying to do so I have errors with both
implementations that i have tried.

for(my_iter = mla.begin(); my_iter != mla.end(); my_iter++)
{


if (*my_iter != " ")
cout << *my_iter;

}


thesis.cpp(1103) : error C2446: '!=' : no conversion from 'const char
*' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or
function-style cast
thesis.cpp(1103) : error C2040: '!=' : 'int' differs in levels of
indirection from 'const char [2]'

or by this code

for(iiii = 0; iiii < mla.length(); iiii++)
{
if (mla[iiii]* != " ")
cout << mla [iiii] << "-";
}

thesis.cpp(1103) : error C2446: '!=' : no conversion from 'const char
*' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or
function-style cast
thesis.cpp(1103) : error C2040: '!=' : 'int' differs in levels of
indirection from 'const char [2]'



Can anyone help

Cheers
Costantinos
 
K

Kai-Uwe Bux

Hello. I have a string which looks like
[5 64 3 22 1...] and I need to read these numbers.
The logical implementation was to read the string char by char and
check whether I have a space and when I find one I could create a
vector with all the numbers (after i convert them to number of course).

No: the logical implementation is to create a stringstream from the string
and let the extraction operator>> handle things.
the problem is that when i am trying to do so I have errors with both
implementations that i have tried.

for(my_iter = mla.begin(); my_iter != mla.end(); my_iter++)
{


if (*my_iter != " ")

" " is not a character literal but a c-string literal. Try ' '.
cout << *my_iter;

}


thesis.cpp(1103) : error C2446: '!=' : no conversion from 'const char
*' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or
function-style cast
thesis.cpp(1103) : error C2040: '!=' : 'int' differs in levels of
indirection from 'const char [2]'

or by this code

for(iiii = 0; iiii < mla.length(); iiii++)
{
if (mla[iiii]* != " ")

try ' ' instead of " ".
cout << mla [iiii] << "-";
}
[snip]



Best

Kai-Uwe Bux
 
E

edd

Hello. I have a string which looks like
[5 64 3 22 1...] and I need to read these numbers.
The logical implementation was to read the string char by char and
check whether I have a space and when I find one I could create a
vector with all the numbers (after i convert them to number of course).
the problem is that when i am trying to do so I have errors with both
implementations that i have tried.

for(my_iter = mla.begin(); my_iter != mla.end(); my_iter++)
{
if (*my_iter != " ")

if (*my_iter != ' ') // note single-quotes
cout << *my_iter;
}

snip --- 8<---
or by this code

for(iiii = 0; iiii < mla.length(); iiii++)
{
if (mla[iiii]* != " ")
cout << mla [iiii] << "-";
}

Same problem.

But, why not just use a stringstream?

#include <algorithm>
#include <iterator>
#include <iostream>
#include <sstream>

int main()
{
std::string numbers("100 -12 321 6 999");
std::istringstream in(numbers);

typedef std::istream_iterator<int> src_iter;
typedef std::eek:stream_iterator<int> dst_iter;

std::copy(src_iter(in), src_iter(), dst_iter(std::cout, " "));

return 0;
}

Regards,

Edd
 
M

mlimber

Hello. I have a string which looks like
[5 64 3 22 1...] and I need to read these numbers.
The logical implementation was to read the string char by char and
check whether I have a space and when I find one I could create a
vector with all the numbers (after i convert them to number of course).

A more standard approach is to use std::istringstream:

#include <sstream>
#include <vector>

using namespace std;

vector<int> ConvertStringToVector( const string& s )
{
istringstream iss( s );
vector<int> v;
int n;
while( iss >> n )
{
v.push_back( n );
}
return v;
}

void Bar()
{
const string s = "5 64 3 22 1";
const vector<int> v = ConvertStringToVector( s );
// ...
}
the problem is that when i am trying to do so I have errors with both
implementations that i have tried.

for(my_iter = mla.begin(); my_iter != mla.end(); my_iter++)
{


if (*my_iter != " ")
cout << *my_iter;

}


thesis.cpp(1103) : error C2446: '!=' : no conversion from 'const char
*' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or
function-style cast
thesis.cpp(1103) : error C2040: '!=' : 'int' differs in levels of
indirection from 'const char [2]'

What types are my_iter and mla, and why is my_iter not declared in the
loop itself to restrict its scope? Please see the FAQ on posting code
that doesn't work:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Also note that ++my_iter is never slower and sometimes faster than
my_iter++ (see
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15).
or by this code

for(iiii = 0; iiii < mla.length(); iiii++)
{
if (mla[iiii]* != " ")
Huh?

cout << mla [iiii] << "-";
}

thesis.cpp(1103) : error C2446: '!=' : no conversion from 'const char
*' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or
function-style cast
thesis.cpp(1103) : error C2040: '!=' : 'int' differs in levels of
indirection from 'const char [2]'

Cheers! --M
 
C

costantinos

Kai-Uwe Bux said:
No: the logical implementation is to create a stringstream from the string
and let the extraction operator>> handle things.
thanks guys.
Indeed things are much better now.

cheers
Costantinos
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top