Hi, jmuzsik.
It's only been about 4 weeks since I've been learning programming |
Really? But do you mean you’ve been learning C++ for 4 weeks, or is this the very first time you’re studying programming? C++ is said to be a four/five years plan to be fully mastered, perhaps it’s not the best choice for absolute beginners...
However, that’s up to you, I don’t want to discourage you.
But, in this case, if you have very little or no experience in programming, if I were you I’d change drastically my approach.
Splitting the code into several files is a must for ‘real’ programmers (it ensures benefits when building libraries and keeping one’s code organized), but it adds very little at the early stages of learning. Plus, you could soon being forced to mess with linkage issues, which are problems you can safely put off for a long time.
My personal advice would then be to start writing all your code inside a single file and start splitting it when you get to classes. As an immediate advantage, you could stop worrying about global scope for now and focus on syntax.
About your questions:
1)
1 2 3 4
|
#include <limits>
//why is limits used in this case?
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
//What in the world is going on here?
|
The simple answer is that those statements make the following line appear on screen
and than the program stops until you press ENTER (it means “ignore every input apart from the ENTER key”).
This is useful to prevent the program from closing without any notice.
Do you need those instructions? If you delete those lines and you don’t experience any issue, they aren’t of any use for you (because of the environment you are working in).
So, this block of code can be deleted entirely, or must stay as it is:
1 2 3 4
|
#include <limits>
...
std::cout << "\nPress ENTER to exit...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
2)
1 2
|
constexpr int MAX_RESULTS = 2;
//is above simply giving a maximum value for the global integer?
|
That statement defines a global variable of type integer, called ‘MAX_RESULTS’, and initializes it to 2.
The variable is required to be not-modifiable (constant).
That variable will be used later inside the program.
The first usage is here:
int jmuzsik_global_results[MAX_RESULTS];
which could be alternatively written
int jmuzsik_global_results[2];
and here:
for(int i=0; i<MAX_RESULTS; i++)
which could be alternatively written
for( int i = 0; i < 2; i++ )
Does it makes sense to define a global constant variable just to keep a value?
Yes, it does, because you can use it two hundreds of times and the day you want to change your code you only need to change one single line, for example from
constexpr int MAX_RESULTS = 2;
to
constexpr int MAX_RESULTS = 5;
But this is a programming strategy, whilst now you need to focus on syntax.
3)
1 2 3 4 5 6
|
int jmuzsik_global_results[MAX_RESULTS];
void printResults();
//is this a pointer? I honestly cannot understand your last paragraph.
//Is what is within this program not ideal but required to make the program work?
|
jmuzsik_global_results[]
is an array, and it means is somewhat a pointer.
If you don’t know what a pointer is, it means you should not mess with them yet. They are largely used in C, but much less in C++.
4)
1 2
|
//What does this do? I see it gives a value 064064. Why does the statement:
//"jmuzsik_global_results" is now 064064... print?
|
What?! :-O
Are you sure? Could you please provide your output?
This is mine:
main.cpp:
=========
main(): starting the program.
"jmuzsik_global_results" is now 0 0
Shall we brush up math?
Question1.cpp:
==============
askSumTwoNumbers(): called by main().
What is 2 plus 3?
5
Very good! Let's move on.
Question2.cpp:
==============
askMultiplication(): called by main().
What is 3 times 3?
9
Brilliant!
"jmuzsik_global_results" is now 1 1
Press ENTER to exit... |
5)
1 2 3
|
if(askSumTwoNumbers(2, 3, 1))
//So the 1 at the end relates to the first value of the
//jmuzsik_global_results? That is what it is allocated to?
|
The ‘1’ at the end relates to the first position inside the array “jmuzsik_global_results”, and that position has index ‘0’. That’s why, when we get to the moment to write in it, we state:
jmuzsik_global_results[position-1]
This way, ‘1’ becomes ‘0’.
That is what it is allocated to?
Giving a position (=an index) to an array let you access what’s allocated inside that position.
6)
1 2 3 4 5 6 7 8 9
|
bool isCorrect = false;
if(answer == one+two) {
jmuzsik_global_results[position-1]++;
isCorrect = true;
}
//what is the isCorrect?
//Why is the false put first?
//What does [position-1]++; exactly do?
|
isCorrect is defined as a variable of type ‘bool’. It means it can only be assigned one of these two value, ‘true’ or ‘false’.
‘false’ is put first because we want that variable to become ‘true’ only if
“answer == one+two”
is true, otherwise it must keep ‘false’.
jmuzsik_global_results[position-1]++
accesses the ‘position-1’ element in jmuzsik_global_results[] and increases it by one.
7)
1 2
|
return isCorrect;
//Why is this put in at the end?
|
It causes the function to hand the value of “isCorrect” back to main().
“main()” calls “askSumTwoNumbers()” here:
if(askSumTwoNumbers(2, 3, 1)
According to what “askSumTwoNumbers()” returns (i.e. “isCorrect” ‘true’ or ‘false’ value), the ‘if’ instruction decides to execute either the line
std::cout << "Very good! Let's move on.\n";
or the line:
std::cout << "Embarassing...\n";
8)
//printResults(); relates to?
It invokes the function “printResults()”, which only display the values inside “jmuzsik_global_results[]”, and so doesn’t need any argument and does not returns any value.
//void section...?
“void” means: this function does not returns any value.
Compare:
void printResults();
--> does not return any value
bool askSumTwoNumbers(int one, int two, int position);
--> returns a ‘bool’
9)
1 2 3 4
|
//It prints the values to be 1 1. Can that be 2 instead?
//Can it be a value that grades the user, in a way?
//What does this, ' ', do?
//What doers '\n' do?
|
The function printResults() sends to screen the values which are inside “jmuzsik_global_results[]” in the moment it is invoked.
If the values are:
jmuzsik_global_results[0] == 1
and
jmuzsik_global_results[1] == 1
then the output will be
If values are different, the output is different.
<< ' '
adds a space behind every values of jmuzsik_global_results[], otherwise the output would be
without spaces.
Likewise,
std::cout << '\n';
adds a newline after all the values have been displayed.