Giter Club home page Giter Club logo

input-integer-c's Introduction

Integer Input on Stdin in C

To include the header file in your project, run:

wget https://raw.githubusercontent.com/csknk/input-integer-C/master/include/integer-input.h

View the file before downloading

Learning notes and exercises on integer input in C.

Objective: Get an Integer From Stdin

At first glance, scanf() looks like a reasonable way to collect integer input. The function allows formatted input to be collected (it's name comes from the words "scan formatted").

To scan integer input, first declare an integer and pass a pointer to this to scanf:

int input;
scanf("%d", &input);

However, because stdin is inherently unconstrained (users can type anything), scanf() may not be the best choice. You need to handle whitespace characters appropriately as well as clearing the input stream for subsequent input because scanf() doesn't read newlines.

Note that fscanf() which scans a FILE stream and sscanf() which scans a string are rational and useful choices because they operate on structured data.

fgets: Read the Entire Line

It may be better to read the whole line using fgets(), and then process the input as required:

  • Use fgets() to read an entire line of stdin within a while loop.
  • Check the length of the input buffer - even though fgets() discards extra input, the user should be informed if input has exceeded available space and allowed to re-enter the data.
  • Convert input to an integer using strtol().
  • If input meets the length constraint and can be interpreted as an integer, the loop ends.

Example: Read Integer

See integer-input.h in this repo.

C++

Example for integer input in C++:

#include <iostream>
#include <typeinfo>
#include <limits>

int main()
{
	int input = 0;
	while (1) {
		std::cout << "Enter an int:\n";
	
		// Enter this block if taking input from cin has failed.
		if (!(std::cin >> input)) {
	
			// The error flag is set on std::cin - future attempts to
			// get input will fail unless the error is cleared.
			std::cin.clear();

			// The failed input is in the input buffer. The default for `ignore` is
			// to skip a single character. To be sure, remove the max streamsize
			// number of chars up until a newline is encountered
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
			continue;
		}
		break;
	}
	std::cout << "You entered: " << input << '\n';
	return 0;
}

References

input-integer-c's People

Contributors

csknk avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.