Hello Tina,
I will do my best to answer your questions.
The reason i was confused was that i thought when i pressed enter that loop was ''done'' |
No, based on the while condition "std::cin >> sen". Only the input was done when you pressed "Enter". As long as "std::cin" remains in a good state the whole condition is evaluated as true and you enter the while loop. This is where
salem c's suggestion,
if ( sen == "STOP" ) break;
, comes in. You are checking if what was entered is equal to the stop sentinel, in this case "STOP", and if it is true then the "break" statement will break out of the while loop.
This may be a simple explanation. C++ as with C does not deal with the keyboard or screen directly. When you include "iostream", the high level header file, it includes "istream" which includes "ostream" which includes "ios" which includes "xlocnum" which includes several other header files to get to the low level code that can deal with the keyboard or the screen. In the end all you have to worry about is the "iostream" lets you use "std::cin" to use the keyboard and "std::cout and std::endl" to use the screen for output. At least this is what I see with VS, used more often than (visual) or (Visual Studio).
Next what you type on the keyboard does not go directly into the variable following "std::cin", but into an input buffer waiting you you to press enter. Then it will try to extract from the input buffer what it can into the variable. This is where formatted input of
std::cin >> sen;
and
tsd::getline(...);
makes a difference.
This may also help. A while loop, do/while loop, if statement and the middle part of a for loop all work on the principal of false (0), zero, or true (1). So, whatever is in the condition part has to evaluate to either false or true. In the case of
std::cin >> sen
what is being checked here is the state bits of the stream, namely the (good) bit. As long as the good bit is (1) you will enter the while loop until this bit is changes. The problem is that you have no way to change the state bits unless you use
salem c's suggestion of
pressing ctrl-d (Unix/Linux) or ctrl-z (Windows). |
i know it doesnt make sense and it might be ridiculous question |
No actually it does make sense and it is a very good question for a beginner.
I recently purchased "Stroustrup's", but I do not think I am as far along as you are. He may have a point for the "std_lib_facilities.h" and I can understand using it until it is explained later, but I feel it is more of a stumbling block than a help. It is like the line
using namespace std;
. This may work in the beginning, but eventually it becomes a problem. Also people are told to use this, but not about the potential problems or what it is actually doing.
As for initializing variables. Variables like char, short, int, double along with their unsigned versions are assigned space on the stack when the program compiles and
only space. What happens is that the variable will try to use whatever is left in that particular part of memory and make it fit into the particular type of variable as defined. This is referred to as a garbage value. On my computer an "int" usually has the garbage value of "-858993460" and a double "-9.2559631349317831e+64". Your computer may be different. Any way not something you want to work with.
Personally I feel it is a good idea to initialize your variables when they are defined. Also this is not always necessary if you give the variable a value not long after it is defined. It is when you try to use a variable before it is given a proper value.
The exception would be strings, vectors, lists, queues and any other type that is considered a type of container. These variable types are empty when they are defined and do not need initialized unless you need it to contain something like
std::string prev = "previous";
.
You used the word "visual". I took this to mean VS (Visual Studio). I do not know which version you are using, but would guess it is new enough to use the C++11 standards. In that case you can define an initialize a variable as:
int num{};
. The empty {}s will set the variable to (0) zero. Other forms of an "int" to (0). A "char" to '\0" and a double to "0.0".
How much of this stuff that we learn on the beginning is actually used? |
All of it. First you have to learn the basics before yo can do it better. Also once you understand how the basics work other code will not be as intimidating. Learning slow and with repetition is a good way to learn. I once took an online course in "Python" where the instructor said that by typing the same thing many times you learn partly be repetition. It does have its use.
As for what or how a program should look it really depends on the program and what you are trying to do. There is no format to the program only what to put where. If you are unsure just ask.
Hope that helps,
Andy