Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By laserlight

Getting string input from keyboard.

This is a discussion on Getting string input from keyboard. within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get string input from the keyboard for my assignment. For my assignment the user can input 1 ...

  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 07:19 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    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.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,888
    That could work too, but you don't have enough space for five 100 marks with a space in between each of them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 07:40 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Something like:
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string line;
        std::getline(std::cin, line);
        std::cout << line << std::endl;
        return 0;
    }
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01:44 PM
  3. Keyboard Input
    By stuartbut in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 10: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, 10:14 PM

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