In this series, I’m going to present 5 problems in different areas of C++ and then provide the solutions along with my explanations of the solutions. You should try the problems first before looking at the solutions to maximize your learning efforts.

For this first problem set, here are 5 problems to solve on declaring variables and initializing them with data.

Problem 1

Declare variables for integer, double, string, and bool data. These should be declaration statements only. After you’ve declared the variables, write assignment statements to assign data to each variable.

Problem 2

Rewrite your solutions to Problem 1 so that the declaration statement also includes an assignment (initialization) of data to the variable.

Problem 3

Declare and assign data to three integer variables on the same line so that you only declare the data type once.

Problem 4

Declare and initialize variables for integer, double, string, and bool data using the message-passing style of variable initialization.

Problem 5

Rewrite your solution to Problem 5 using curly braces and explain why this is a good way to initialize variables with data.

Problem 1 Solution

This is the most straightforward way of creating variables and assigning data to the variables. Here is one set of solutions to this problem:


int intNumber;

double dblNumber;

string strngVar;

bool boolVar;

intNumber = 100;

dblNumber = 2.22;

strngVar = "Hello, world!";

boolVar = false;

If you are going to follow this pattern to declare and then assign data to variables you should go ahead and do it in one statement, as the solution to Problem 2 demonstrates.

#cpp #programming-tips #learning-to-program #programming #learning-to-code #deep learning

5 Variable Declaration and Initialization Problems
5.85 GEEK