CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: User input

Threaded View

  1. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: User input

    Quote Originally Posted by fleg14 View Post
    I have console aplication, user is typing command through console and I should do some action according those inputs. One command is on one line only.

    for example cp is a command which has 4 parameters, between every 2 parameters can be many whitespaces they doesnt matter, so valid command is e.g.-> cp 4 5 6 9 thats valid.
    First, we don't know what state your program is in when you call this function, whether you've already corrupted memory when you call this function, whether "io" is a valid object, etc.

    Let's get a full program, and then show us what is the input to make it crash:
    Code:
    #include <vector>
    #include <string>
    #include <sstream>
    #include <fstream>
    #include <cctype>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    bool has_only_spaces(const std::string &str);
    
    void start(std::istream& in, std::ostream& out)
    {
        bool end = true;
        out << ">>>";
        while (end)
        {
            vector<string> tokens;
            string input;
            getline(in,input);    
            if (input.empty())
                input += '\0';
    
            stringstream ss(input); 
            string buf;
    
            if (!has_only_spaces(input))
            {
                while (ss >> buf)
                    tokens.push_back(buf);
            }
        }
    }
    
    bool has_only_spaces(const std::string &str)
    {
        return std::count_if(str.begin(), str.end(), isspace) == str.size();
    }
    
    int main()
    {
        start(cin, cout);
    }
    That full program is basically your code, except for the has_only_spaces which calls count_if() (no loop required). Take the program above, compile and run it. Tell us if this program crashes, and what input you used to crash the program.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 6th, 2013 at 12:28 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured