Thread: parsing command line

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    8

    parsing command line

    Hi there,

    I have done a fair amount of research in order to figure out how to parse a command line for, say, and integer. The code below almost works, except when the user types in, for example, "4s". The result is that find_this_number gets assigned 4, whereas I would prefer to reject the input "4s" because the string does not represent an integer.

    Some people have recommended using the boost library, but I would like to know if there is a way to do this using either strict C or the STL libraries only. Can you please help? Thanks!

    Aaron

    insert
    Code:
    string cmd_line = " ";
    string cmd = " ";
    int find_this_number = 0;
    
    
    // Read in what-ever the user typed in...
    getline(cin, cmd_line);
    istringstream ss(cmd_line);
    
    
    // Get the first word in the string
    getline(ss, cmd, ' ');
    
    
    // Check whether the user typed in an integer
    stringstream scmd(cmd);
    if ((scmd >> find_this_number).fail() )
        {
            cin.clear();
            cout << "oops, you typed: " << cmd << endl;
        }
        else
        {
            cout << "The number is: " << find_this_number << endl;
        }

  2. #2
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Then you're just going to have to check if all the characters in that stream are integers aren't you?

    There's http://www.cplusplus.com/reference/std/locale/isdigit/ or just do what you're doing but in a loop for each character.
    Last edited by Rodaxoleaux; 06-28-2012 at 01:22 PM.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Or you could use std::string.find_first_not_of() to check that the entire string doesn't contain any letters, or whatever else is invalid.

    Also note: Most people consider the "command line" to be the variables passed to your program when you start it, not the use of an input stream.

    Jim

  4. #4
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by jimblumberg View Post
    Or you could use std::string.find_first_not_of() to check that the entire string doesn't contain any letters, or whatever else is invalid.

    Also note: Most people consider the "command line" to be the variables passed to your program when you start it, not the use of an input stream.

    Jim
    Easier and better idea. Outshow me, will ya? *gets the mallet* o3o
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing Command Line String Into Array
    By Illusionist15 in forum C Programming
    Replies: 10
    Last Post: 03-31-2011, 09:16 AM
  2. Command line how to handle optional command.
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2010, 11:41 PM
  3. parsing command line strings
    By John_L in forum C Programming
    Replies: 15
    Last Post: 05-28-2008, 08:26 AM
  4. Command Line Argument Parsing
    By lithium in forum Windows Programming
    Replies: 3
    Last Post: 07-13-2005, 07:01 PM
  5. parsing command line problem
    By Newworld in forum C Programming
    Replies: 1
    Last Post: 11-27-2004, 11:11 PM

Tags for this Thread