CodeNewbie Community

loading...
Cover image for C++ Input

C++ Input

Tristan
・6 min read

Introduction

  • This series is going to be dedicated to my reading of the book, C++ programming principles and practice by Bjarne Stroustrup. The resources I used to create this post can be found in C++ programming principles and practice by Bjarne Stroustrup. Extra resources I used for this post can be found HERE

A quick word

  • This will not be a full encompassing tutorial C++ input streams. Instead I will provide you with a light introduction and leave further exploration up to you the reader.

Input

  • If you are coming directly from my Hello World tutorial, which can be found HERE. Then you would of noticed that the previous program only dealt with output. There was no way for us to receive any input form the user. This tutorial will provide some insight on how we receive information from a user.

  • When dealing with input we need somewhere to store the input. In C++ we would say that we need somewhere to read into. Somewhere is a place in our computer's memory and a we call it an object.In order to access an object we need to give it a name, a named object is called a variable. Bellow is an example of us creating a program that takes user input. I will paste the code and then we will walk through it line by line.

#include <iostream>

int main()
{
    std::cout << "please enter your first name, followed by enter/n";
    std::string first_name;  //string
    std::cin >> first_name;
    std::cout << "Hello" << first_name;
    return 0;

}
Enter fullscreen mode Exit fullscreen mode

include < iostream >

  • This should be familiar to us from the previous post. However, if you have not read the previous post. Then know that #include is how we tell the compiler what we want to include in this file. Then we have the <iostream> it is the standard library in C++ for dealing with input and output. I recommend that you actually delete #include <iostream> from your code and notice the errors that you get. It is because of this line of code that we have access to the std(standard) namespace, which we use to access cin and cout.

int main()

  • This is also another part that you should be familiar with. Every C++ program has a main function and it will act as the starting point for our application.

std::cout << "please enter your first name.../n";

  • This is using the std name space to access cout(see-out), which for all intense and purposes represents our console. Just a quick reminder that you will not be able to use cout if you delete std::. Then we have out output operator << which is how we send data to the console. We then have a string literal, in C++ string literals are represented with "". Lastly we end with /n, this newline character is how we insert a newline at the end of our string and tell the compiler to end that line.

std::string first_name;

  • This is us defining a variable, remember that a variable is just a named object with a type. We give an object a name so that we are able to reference it.

std::cin >> first_name;

  • This line of code reads data from our keyboard and places it into the first_name variable. cin(see-in) refers to the standard input stream defined inside the standard library which we have access through std::. Then the input operator >> specifies where the the value will go and for us it goes into the first_name variable.

  • when we run our program as a whole and type "bob" and then hit enter we will get an output of Hello bob. The enter is very important because it will send a newline character and a newline is how the computer tells when to stop running the program. Without hitting enter(sending a newline character) the computer will simply continue to collect characters.

Variables

  • we have talked about variables so lets just recap what we know. Variables are really just named objects with a specified type. Speaking of types there are quite a number of types in C++ but there are 5 main types that we should be familiar with at this point.

1) int : int nums = 39; represents an integer

2) double : double nums = 39.4; represents floating point numbers.

3) char : char single= 'm'; represents a single character

4) string: int nums = 39; represents a string of characters

5) bool: int nums = 39; represents a true/false value

-I am sure you have quite a few questions, so I will try to preventively answer them for both of our sakes.

Why is a double called a double?

  • double is actually short for double-precision floating point. A floating point is a computers approximation for a real number. A real number being any number that can be represented on a number line. So that means every number except infinity and imaginary numbers.

What are the differences between int and double

  • there are a few reasons, so I will try to list as many as I can, if I missed any important differences please let me know.

1) Precision : as I am sure you noticed a double has a decimal point while an integer does not. This means that when you need to deal with precision, you should use doubles.

2) Memory intensity : since an integer does not have to deal with decimal values it is less memory insensitive. Integers are typically 4 bytes, where a double is 8 bytes, so literally half the size. This would really come in handy when dealing with embedded systems, where memory is limited.

  • Obviously these two types can not be used interchangeably but I encourage you to try and store some int values in double variables and vise versa. The results will give rise to some interesting questions.

What are the differences between char and string;

1) Syntax : notice that when we use a string we use the double "" quotation and for char we use the single syntax ''.

2) Location : Notice that there is no std:: when we are using char. This means that they do not come from the same namespace.

  • I also want to point out you can not store a char type inside of a string variable and vice versa.

Input and Types

  • We can actually define multiple parameters with different types and even read into them, like below:
int main()
{
   1) std::cout << "Please enter your name and age\n";
   2) std::string first_name;
   3) int age;
   4)std::cin >> first_name >> age;
   5) std::cout << "Hello "<<first_name << ", age :" << age;
   6) return 0;

}
Enter fullscreen mode Exit fullscreen mode
  • At first glance at the code above everything seems pretty normal, we have a function with a return type of int and a name of main. Remember that main is special in C++ and a main function will act as our starting point. Then we have the body, line 1) is us accessing the standard output stream to print the desired text to the console. Line two is us declaring a variable of type string , in reality this is us reserving a chunk of our computer's memory for further use. Line 3 is where we define a variable of type int and call it age. Line 4 might seem a little strange but all we are doing is telling our machine to read a string and an integer into the first_name and age variables. If that syntax is unsettling to you, the equivalent would be this:
    std::cin >> first_name;
    std::cin >> age;
Enter fullscreen mode Exit fullscreen mode
  • As you can see it is just us combining two lines. Lastly on line 6 we have us returning 0, which is an indicator that this function is finished calling. Notice that the 0 matches the return type of the function. So a question you are probably asking yourself now is, How does our program tell when the string ends and the number begins? Excellent question and the answer is Whitespaces. Whitespace being defined as a newline, tab or space characters. A whitespace terminates the reading of strings. So us hitting the space bar is actually what ends the string. Once we have ended the string the program will expect us to enter a integer value.

Garbage values

  • What happens if we try to run out program but put a number first and a string second? If we ran the program and then typed 22 Bob into the promp, we would get Hello 22, age :0. We get 22 because 22 is just a string of characters, but where did the 0 come from? Since Bob is not a integer we can not read it into a integer variable. Instead we will get 0, which is called a Garbage value. This is just a random number or 0,that happens to be memory when the variable is used. We get garbage values because unlike other programming languages, variables in C++ are not initialized when they are defined.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Discussion (0)