Thread: Is there a way to get user input (maybe using getchar()) without erasing the buffer?

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    4

    Is there a way to get user input (maybe using getchar()) without erasing the buffer?

    So i need to allow the user to input numbers of a matrix one line at a time, and if they input $ it means the matrix is completed and they want to move on, so i wrote this but having to look if the user has inputed $ using getchar() eats the first digit of each number. Is there a way for getchar to not erase the character it gets from the buffer? i also take other suggestions, thanks.
    Code:
    for (int dato = 0; dato < 400; dato++) {
      scanf("%i", &Matriz1[fila][dato]);
      if (getchar() == '\n') {
        fila++;
      }
      if (getchar() == '$') {
        break;
      }
    }
    Last edited by Salem; 07-14-2020 at 10:53 PM. Reason: crayola -

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,924
    Read the entire line of input into a string, then parse that string. This way, you can check if the next item is a "$", and if not, you can re-try by trying to parse it as an integer to be stored in the current row of the matrix. You will also be able to determine how many items were provided for the current row of the matrix since you will eventually reach the end of the string: if at that point you still did not encounter a "$", then you know that you should continue to the next row.

    You presumably also need to store the number or rows and columns of the matrix: it is alright to create a 2D array with plenty of space, but you might not use all of it. Then, you might want to check that each row was entered with the same number of items.
    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. Using getchar() to flush newline from input buffer
    By Valour549 in forum C Programming
    Replies: 15
    Last Post: 11-03-2018, 07:49 AM
  2. Beginner's question on getchar, putchar, and buffer
    By wicking1009 in forum C Programming
    Replies: 1
    Last Post: 11-28-2016, 03:03 AM
  3. understanding getchar(), buffered user input, and so on...
    By FernandoBasso in forum C Programming
    Replies: 4
    Last Post: 10-06-2011, 04:12 PM
  4. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  5. getchar when buffer is empty??
    By spike232 in forum C Programming
    Replies: 10
    Last Post: 04-29-2002, 11:26 PM

Tags for this Thread