Working with user inputs in a C++ console application

Working with user inputs in a C++ console application

PSU CMPSC Activity 3

I want to go over how I went about this simple task of working with user inputs. There are a few things to talk about first since this was originally done for a class. The first and most important being that I do not condone cheating and this is for educational purposes only. The second being this class had artificial limitations on the things we could and could not do at any given time in the course. The last being that I am by no means an expert and this is how I went about solving this problem and may not be the best.

Alright now that I have all the caveats out of the way let's talk about the task given and initial thoughts of how to achieve this problem. The problem asks us to accept 5 numbers from a user and print the average of those numbers. This is simple and intended for you to dip your toes into working with the input and output buffers. Another important thing to note is for the sake of simplicity I will be omitting any error catching and will expect proper inputs into the program. This program is also only supposed to run once and has no looping structure.

First we need to ask the user to enter numbers by printing something to the screen and then take an input from the user. For printing our prompt to the user we will push a string to the Cout buffer. There are two methods that come to mind for accepting input, Cin and getline(). For this we will use Cin. We also need to declare variables to handle the user input and to make the code more readable I declared another variable we will use to store the average.

//The variables we will use to store the users inputs
int a, b, c, d, e;
//The variable we will use to store the average
double average = 0.f;
//Ask the user our prompt by inserting a string into the output buffer
//using the << operator.
cout << "Please enter 5 seperate integers seperated by a space.\n";
//This line is how we accept input from the user. Because of the spaces
//between characters the >> operator will conviently break up the inputs into
//each variable until they are all handled.
cin >> a >> b >> c >> d >> e;

The next step is to average these numbers, store the result in our average variable and return the result in a nice formatted way to the user. We will use push the formatted string to Cout using the << operator.

//Do the average calulation and store it in average
average = (a + b + c + d + e) / 5.f;
//Push a string and the average to the output buffer.
//By using multiple << operators we can have the effect of a single string
//without concatination. endl is another way of inserting a newline character.
cout << "The average is " << average << endl;

A typical run of the program would look something like this when put together.

Please enter 5 seperate integers seperated by a space.
5 1 1 1 4
The average is 2.4

Put together this is not a complex program by any stretch of the imagination but is a good starting point for any beginner looking to learning programming. This is just slightly more complex than a simple hello world program. The source code for all of my projects can be found here on my github.

Did you find this article valuable?

Support James Wolfley Jr. by becoming a sponsor. Any amount is appreciated!