Improvisation is an important part of programming, so this tutorial will go over some problems with our code so far
The 'Using namespace std;' Problem:
This problem is one of the most common in the C++ language. Let's say we had the following code:
This code is alright. But now let's say we wanted to add some new things to it.
So we have two functions in the main loop and two namespaces. These namespaces both have the cout function in them.
One of cout commands is meant to be using the std namespace and the other is meant to use the foo namespace. Sadly, the compiler does not know what namespace we are using for each cout.
Basically, we are making a level of ambiguity.
We can easily fix this by writing this new code.
One of cout commands is meant to be using the std namespace and the other is meant to use the foo namespace. Sadly, the compiler does not know what namespace we are using for each cout.
Basically, we are making a level of ambiguity.
We can easily fix this by writing this new code.
Sorry for the error. 'Foo' is not a real namespace just an example one.
Anyways, what we have done here by putting the namespace::function is quite useful for the compiler. Because of this, the compiler now knows what namespace we are using for each 'cout' command.
Knowing this, we can fix our programs and make them have a smaller possibility of becoming buggy.
Anyways, what we have done here by putting the namespace::function is quite useful for the compiler. Because of this, the compiler now knows what namespace we are using for each 'cout' command.
Knowing this, we can fix our programs and make them have a smaller possibility of becoming buggy.
Global and Local Variables
It is much more simple to keep variables local to the function they are going to be in. Because of this, we must always put the lines of code that declare variables inside of the loops they will be used in. As we have no programs that have several loops this isn't much of a big worry, but just in case of further development, we must make the variables in these programs local to the function/loop they will be used in.
So, this means that the:
std::string input;
line of code would have to be put into the int main loop.
This same thing goes for the:
float kg, g, oz;
line of code.
Let's do this.
So, this means that the:
std::string input;
line of code would have to be put into the int main loop.
This same thing goes for the:
float kg, g, oz;
line of code.
Let's do this.
Conclusion
This was just a small tutorial, but these two errors usually appear in most programs. Because of this, it is much more useful to get these errors out of the way now rather than later on.
From this, we have also learned about global and local variables and how to make variables global or local. Let's go over it now.
Global variables must be declared outside of a function.
Local variables must be declared inside of a function.
The other thing we learned is the problem with the 'using namespace std;' line and how it creates ambiguity.
Make sure you fix our previous programs to not have these errors at all.
From this, we have also learned about global and local variables and how to make variables global or local. Let's go over it now.
Global variables must be declared outside of a function.
Local variables must be declared inside of a function.
The other thing we learned is the problem with the 'using namespace std;' line and how it creates ambiguity.
Make sure you fix our previous programs to not have these errors at all.