473,322 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

read chars from a string

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

Aug 13 '06 #1
4 6108
edd

co*********@gmail.com wrote:
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<intsrc_iter;
typedef std::ostream_iterator<intdst_iter;

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

return 0;
}

Regards,

Edd

Aug 13 '06 #2
co*********@gmail.com wrote:
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<intConvertStringToVector( const string& s )
{
istringstream iss( s );
vector<intv;
int n;
while( iss >n )
{
v.push_back( n );
}
return v;
}

void Bar()
{
const string s = "5 64 3 22 1";
const vector<intv = 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-lit...t.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-lit...tml#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

Aug 13 '06 #3
co*********@gmail.com wrote:
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
Aug 13 '06 #4

Kai-Uwe Bux wrote:
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

Aug 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: wolfgang haefelinger | last post by:
Greetings, I'm trying to read (japanese) chars from a file. While doing so I encounter that a char with length 2 is returned. Is this to be expected or is there something wrong? Basically...
3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
4
by: yo_mismo | last post by:
Hi everyone, I'm trying to read the first line of a file this way: .... .... .... .... new_line=0; while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){ if (strcmp(&info, "\n") !=...
2
by: Profetas | last post by:
I have the following code that detects a <c> and </c> #include <stdio.h> main(int argc, char *argv) { FILE* fp; char data;
9
by: poison.summer | last post by:
if I wanna copy a part of the string to another string. It is my code, Is it OK? but I have a core dump. include<stdio.h> int main() { FILE *fp; fp = fopen("test.txt","r"); char s1,s2,s3;...
1
by: tor | last post by:
Hi I need to find the last occurrence of a string in a textfile. I don't want to read from the start of the file because its huge 60+Mb. Torfinn
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
2
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have...
13
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
1
by: FadeOut | last post by:
Hi everyone, I'm trying to make an application who sends querys to a tcp server who will then send an answer containing "rows" of commas separated values as an answer. I'm pretty new to tcp...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.