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

Thread: User input

  1. #1
    Join Date
    Jan 2013
    Posts
    4

    User input

    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.

    valid command is new line too, it is not supposed to do anything just a know new line

    valid command is, any number of white spaces and new line, it does the same thing as new line. But here is my problem, in my code where I am spliting strings according whitespaces it does SIGSEV and I dont why.
    Code:
    void io::start(std::istream& in, std::ostream& out, std::ostream& err){
        bool end = true;
    
        out << ">>>";
    
    
    
        while(end){
    
            vector<string> tokens;
            string input;
            getline(in,input);    // read the line from console
    
            if(input.size()==0)    //if input was just newLine than i add a string zero
                input += '\0';
    
    
    
            stringstream ss(input); // Insert the string into a stream
            string buf;
    
            if(!has_only_spaces(input)){       //has_only white spaces is method which tries to not let string only with whitespaces to be splitted, but sigsev occurs in it too
                while (ss >> buf){                 //SIGSEV here,  if theres no if around it, spliting the string here
                    tokens.push_back(buf);
                }
            }
    }
    
    
        bool has_only_spaces(const std::string &str)
    {
        for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
        {
            char c = *it;
            if(!isspace(c)){              //sigsev here on last char if string is "     "    if string is "     a" everything is fine
                return false;
            }
        }
        return true;
    }
    So i would like to know wheres the problem and how to fix it, or how can read user input effectively than I do now.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    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 11:28 AM.

  3. #3
    Join Date
    Jan 2013
    Posts
    4

    Re: User input

    Thanks for help.

    i am getting C:\Users\Ivan\qqq\main.cpp||In function 'bool has_only_spaces(const std::string&)':|
    C:\Users\Ivan\qqq\main.cpp|39|error: no matching function for call to 'count_if(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)'|

    i dont know why i cant use count_if, i am using codeblocks IDE
    Sorry for trouble, i am novice in c++, I am used to java and C#, but C++ makes me headaches as hell. Google havent helped me here :/

  4. #4
    Join Date
    Apr 1999
    Posts
    27,424

    Re: User input

    Try this revised version:
    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 IsSpace(char ch)
    {
       return isspace(ch)?true:false;
    }
    
    bool has_only_spaces(const std::string &str)
    {
        return std::count_if(str.begin(), str.end(), IsSpace) == str.size();
    }
    
    int main()
    {
        start(cin, cout);
    }
    Regards,

    Paul McKenzie

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center