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

Discussion in 'C++' started by DTO, Feb 25, 2005.

  1. DTO

    DTO Guest

    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.
     
    DTO, Feb 25, 2005
    #1
    1. Advertising

  2. "DTO" <> 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
     
    Victor Bazarov, Feb 25, 2005
    #2
    1. Advertising

  3. DTO

    DTO Guest

    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);
     
    DTO, Feb 25, 2005
    #3
  4. "DTO" <> 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?
     
    Victor Bazarov, Feb 26, 2005
    #4
  5. DTO

    DTO Guest

    > 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!
     
    DTO, Feb 26, 2005
    #5
  6. "DTO" <> 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
     
    Victor Bazarov, Feb 26, 2005
    #6
  7. DTO

    DTO Guest

    Victor Bazarov wrote:
    > "DTO" <> 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
     
    DTO, Feb 26, 2005
    #7
  8. Re: Newbie string question: how to read off the first char from astring?

    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
     
    Rennie deGraaf, Feb 27, 2005
    #8
  9. DTO

    DTO Guest

    I forgot to include <sstream>
    it's is working now. Thanks very much!
     
    DTO, Feb 27, 2005
    #9
  10. DTO

    DTO Guest

    Sorry, I know this sounds really stupid.
    How do I know when to stop if I don't know how long the input is?
     
    DTO, Feb 27, 2005
    #10
  11. Re: Newbie string question: how to read off the first char from astring?

    DTO wrote:
    > 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
     
    Rennie deGraaf, Feb 27, 2005
    #11
  12. DTO

    DTO Guest

    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!
     
    DTO, Feb 27, 2005
    #12
  13. DTO

    adbarnet Guest

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

    rgds,
    Aiden

    "DTO" <> wrote in message
    news:...
    > 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!
    >




    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
     
    adbarnet, Feb 27, 2005
    #13
  14. "adbarnet" <> wrote...
    > 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

    >
    > rgds,
    > Aiden
    >
    > "DTO" <> wrote in message
    > news:...
    >> 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!
    >>
     
    Victor Bazarov, Feb 27, 2005
    #14
  15. DTO

    DTO Guest

    Thank you very much everyone!!
    I ended up using setw(width) and cout << setiosflags(ios::left)
     
    DTO, Feb 27, 2005
    #15
  16. DTO

    DTO Guest

    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!
     
    DTO, Feb 27, 2005
    #16
  17. "DTO" <> wrote...
    >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
     
    Victor Bazarov, Feb 27, 2005
    #17
  18. DTO

    DTO Guest

    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);
     
    DTO, Feb 27, 2005
    #18
  19. Re: Newbie string question: how to read off the first char from astring?

    DTO wrote:
    > 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
     
    Rennie deGraaf, Feb 27, 2005
    #19
  20. "Victor Bazarov" <> wrote in message news:<>...
    >
    > 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;
     
    Dylan Nicholson, Feb 27, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    558
  2. Guest
    Replies:
    7
    Views:
    678
    Walter Wang [MSFT]
    Jun 28, 2006
  3. wwj
    Replies:
    24
    Views:
    2,520
    Mike Wahler
    Nov 7, 2003
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,060
    Ian Collins
    May 9, 2006
  5. Replies:
    8
    Views:
    422
Loading...

Share This Page