read chars from a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • costantinos@gmail.com

    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_cas t, 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_cas t, 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

  • edd@nunswithguns.net

    #2
    Re: read chars from a string


    costantinos@gma il.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::istringstr eam in(numbers);

    typedef std::istream_it erator<intsrc_i ter;
    typedef std::ostream_it erator<intdst_i ter;

    std::copy(src_i ter(in), src_iter(), dst_iter(std::c out, " "));

    return 0;
    }

    Regards,

    Edd

    Comment

    • mlimber

      #3
      Re: read chars from a string

      costantinos@gma il.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::istringstr eam:

      #include <sstream>
      #include <vector>

      using namespace std;

      vector<intConve rtStringToVecto r( 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 = ConvertStringTo Vector( 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_cas t, 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:



      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_cas t, 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

      Comment

      • Kai-Uwe Bux

        #4
        Re: read chars from a string

        costantinos@gma il.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_cas t, 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

        Comment

        • costantinos@gmail.com

          #5
          Re: read chars from a string


          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

          Comment

          Working...