Regarding Scanf and other similar functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phatinc
    New Member
    • Apr 2008
    • 1

    Regarding Scanf and other similar functions

    I was writing a program that calculates volume of a triangular prism. However it came to my attention that integers larger than what can be stored will cause errors so I placed certain restrictions onto what can be inputted.

    Now the problem at hand is when someone types more than the set number of integers that i tell the program to read.
    eg
    Code:
    scanf("%d %d %d", &a, &b, &c);
    and I enter 4 integers the program will still run correctly based on the first three integers (or if less than 3 integers were entered the program will not cease). I was wondering if there was a solution to this (i've tried a loop but that doesn't seem to work), or a function that can be used.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Scanf returns an integer: the number of items successfully entered. You can use this to help check if not enough data was entered, but for too much you'd need to use getline and parse the line yourself.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      I would scanf() the integers one at a time.

      Comment

      Working...