Thread: Getting string input from keyboard.

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    21

    Getting string input from keyboard.

    I'm trying to get string input from the keyboard for my assignment. For my assignment the user can input 1 of 3 things (there's no menu system to determine which - i have to be able to tell what they did and respond.

    The user can input:
    1. a float number (75.6)
    2. a letter grade, or other letter representing something (A+, DNF, etc.)
    3. up to 5 ints (could be: "70 80 60 30 100" or "70 80"

    This is the code I have currently but it does not work, has it forces the user to input 5 things, regardless of whether or not they press enter: I need to be able to input a string, and then analyze that string to determine what sort of input they did, and if necessary pull the numbers from the string.

    Code:
    int main()
    {
        char mark1[4] = "", mark2[4] = "", mark3[4] = "", mark4[4] = "", mark5[4] = "";
    
    
        cout << "input the students mark(s)";
        cin >> mark1 >> mark2 >> mark3 >> mark4 >> mark5;
        
        cout << "numbers are:" << mark1 << mark2; // this is just for testing
        return 0;
    }
    Last edited by SneakySnake; 01-30-2013 at 08:19 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use std::getline to read into a std::string, then inspect the contents of the string read to decide what it is, and parse it accordingly. To decide what it is, you could do things like search for '.' in the string, or use std::isalpha from <cctype> to check if any character is alphabetic.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    21
    thank you.

    This is what I'm using now: is this the best way to do it? ( it seems to be working fine)

    Code:
    int main()
    {
        char marks[15];
    
    
        cin.getline (marks, 15, '\n');
        
        cout << marks;
        return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That could work too, but you don't have enough space for five 100 marks with a space in between each of them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    21
    Ah yes, fixed that.

    I'm not familiar with the std::getline way. What would the code look like for that?
    Last edited by SneakySnake; 01-30-2013 at 08:40 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Something like:
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string line;
        std::getline(std::cin, line);
        std::cout << line << std::endl;
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Keyboard input
    By Darkinyuasha1 in forum Windows Programming
    Replies: 7
    Last Post: 06-19-2007, 03:21 PM
  2. Need help with keyboard input
    By wiramu in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2003, 02:44 PM
  3. Keyboard Input
    By stuartbut in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 11:09 AM
  4. Keyboard Input
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-29-2002, 11:41 AM
  5. Keyboard input ?
    By Malikive in forum Game Programming
    Replies: 4
    Last Post: 11-06-2001, 11:14 PM