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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 -

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