Thread: TicTacToe How to read multiple lines with scanf

  1. #1
    Registered User
    Join Date
    Aug 2016
    Location
    Sun Valley, CA
    Posts
    1

    TicTacToe How to read multiple lines with scanf

    Essentially I'm required to write a program which scans a tic-tac-toe board with the following input.
    2 1 0
    1 1 0
    2 0 2
    and gives the following output:
    . X 0
    X X 0
    . 0 .
    Where I'm stuck is how do I receive multiple lines of input with scanf? Any words of advice would be appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you are only using scanf to read integers, then you don't need to worry about multiple lines. The scanf documentation states that all format specifiers except %c and %[ skip leading white space, which includes spaces, tabs and newlines. So if you just keep reading with scanf("%d", ...) you will get the desired behavior.

    However, if for some strange reason you need to read line-by-line, you can read a line with fgets and then use sscanf to scan the line you just read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read multiple lines of input
    By veera in forum C Programming
    Replies: 4
    Last Post: 06-28-2015, 11:52 PM
  2. Reading multiple lines Scanf?
    By dexstar in forum C Programming
    Replies: 13
    Last Post: 08-01-2013, 08:57 PM
  3. read a number with scanf() and read char[] with fgets()
    By nutzu2010 in forum C Programming
    Replies: 5
    Last Post: 03-11-2011, 05:05 AM
  4. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  5. read multiple lines from a file
    By YankeePride13 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 10:30 PM

Tags for this Thread