formatted input using cin

This is a discussion on formatted input using cin within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to use cin to get input in a specific format. Say I have this string as an ...

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    formatted input using cin

    Hi,
    I'm trying to use cin to get input in a specific format.
    Say I have this string as an input: "+ Baraq 112540783 AB1234"
    I want my variables to look like that:
    name = "Baraq"
    id = 112540783
    book = "AB1234"

    now I am sure there is something with providing the format of the input " %s %s %s" or something like that, just not sure how to use it.
    I have tried to find an example but couldn't get how to use it.

    Thank you (:
    gavra.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    IF you know you have an input that looks like the above, you can take advantage of the fact that std::cout skips whitespace, so:

    std::string Plus; // For the "+" char.
    std::string Name, Id, Book;
    std::cin >> Plus >> Name >> Id >> Book;

    Of course, Id could be an int, too.
    If your input is NOT as the above, then this may produce unpredictable results. Just so you know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Thank you, that's what I was looking for, But since I have two format variations I can't use it like that. Is there a way to read the whole line(to a string) and then split to my variables with the format I choose?
    First format: "+ name id book"
    Second format: "+ name id"

    Thanks again.
    gavra.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    Yes, you could use std::getline.

    std::string Line;
    std::getline(std::cin, Line);

    Now you have the entire line in your variable which you can parse.
    For example, you can do:

    std::stringstream sstr(Line);
    sstr >> Plus >> Name >> Id;
    if (sstr >> Book)
    /* Whatever */ ; // There was a book in there, too. Do something with it.

    Extraction will fail if there is nothing left to extract. Stringstreams also skips whitespace.
    Last edited by Elysia; 04-28-2013 at 01:31 PM. Reason: Syntax error
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    ok, great. Thank you!
    gavra.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    This works too:
    Code:
    string name, book, id, plus;
    cin >> plus >> name >> id;
    if ( cin >> book ) ... else cin.clear();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A little question about formatted input ..
    By manasij7479 in forum C Programming
    Replies: 1
    Last Post: 10-25-2011, 01:49 AM
  2. Accepting formatted input
    By fxtdr79 in forum C Programming
    Replies: 15
    Last Post: 06-28-2010, 01:25 PM
  3. Never find EOF after improperly formatted input
    By MALDATA in forum C Programming
    Replies: 5
    Last Post: 09-30-2008, 01:24 PM
  4. Formatted input
    By vaibhav in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2005, 11:19 PM
  5. formatted file input
    By Flikm in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21