Scope and Namespaces, Please clarify

Senile Animal

Baseband Member
Messages
74
Location
USA
OK, I suspect that these will be self evident questions but I need them explained in a less textbookish manner. I was on the cpp.com site and read the what/why of namespaces. The definition starts out with a Namespace is a scope. I assumed that scope is an abstract idea and not some object right? My guess is that scope is just a reference to how a function or other object is being called or used or something simmilar correct? I really don't know.

I understand that a namespace is used to group things together in an orderly fasion. So, int, string, double, bool are these namespaces? I know they are types of variables or functions but is this also examples of namespace?

Namespace std is the standard temp library but what exactly does that contain in it? Where can I find the code?

My last question is if namespace std; is the temp library why would you not just always use this instead of single namespace like the example
using std::string;

Again, I know these are probably obvious questions but I am not the best at textbookish explanations.

*edit: One final question. Is it considered a best practice to prototype a function and have it after the main function or is it ok for me to just write the function first then do main?
 
Last edited:
I also know this is not what I asked above but I did not want to start a new thread.

Code:
#include <iostream>
using namespace std;
int add(int x, int y)
{
   return x+y;
}
int sub(int x, int y)
{
  return x-y;
}
int mult(int x, int y)
{
  return x*y;
}
int div(int x, int y)
{
    return x/y;
}
 
int main()
{
    int x;
    int y;
    int choice; // This is the users choice of operation.
    cout<<"Hello and welcome to the basic arithmetic calculator!\n";
    cout<<"please input 2 numbers to calculate: \n(2)";
    cin>>x >>y;
    cin.ignore();
    cout<<"1. Add\n";
    cout<<"2. subtract\n";
    cout<<"3. Multiply\n";
    cout<<"4. Divide\n";
    cout<<"Which operation do you want to perform?\n";
    cin>>choice;
    switch(choice)
    {
        case 1: add();
        break;
        case 2: sub();
        break;
        case 3: mult();
        break;
        case 4: div();
        break;
    }

I know what is wrong at line 46. I need to put int values into the functions for the switch case but I want to use the input of variable x and y in main for all these functions. How can I do this?
 
Last edited:
OK, I suspect that these will be self evident questions but I need them explained in a less textbookish manner. I was on the cpp.com site and read the what/why of namespaces. The definition starts out with a Namespace is a scope. I assumed that scope is an abstract idea and not some object right? My guess is that scope is just a reference to how a function or other object is being called or used or something simmilar correct? I really don't know.

I understand that a namespace is used to group things together in an orderly fasion. So, int, string, double, bool are these namespaces? I know they are types of variables or functions but is this also examples of namespace?

Those are data types, not necessarily namespaces. They are, however, included in the std library, which you need to include or specify to use them.

Scoping is an entirely different thing. In super basic / condensed version, it's the areas of code that variables/functions can be used.

For example, if you define a variable "int x" inside of main, you can use that variable in any function that main calls.

However, if you define a variable "int y" inside of a separate function A, you cannot use that variable in any other function that A does not call. So for instance you cannot use it in main unless function A returns the value of variable y at the end of the function call.

Likewise, a global variable can be used in any function anytime, as its scoping is global (base level scope).

Namespace std is the standard temp library but what exactly does that contain in it? Where can I find the code?
Just look at the documentation for std to see what it all includes:
C++ Standard Library - Wikipedia, the free encyclopedia

My last question is if namespace std; is the temp library why would you not just always use this instead of single namespace like the example
using std::string;

Again, I know these are probably obvious questions but I am not the best at textbookish explanations.[/quote]

It's mainly a matter of so you don't have to do that constantly. If you didn't include the "using namespace std;" at the beginning, you'd have to do this all the time:

"std::cout" or "std::cin", or whatever other functions are included in the std library.

*edit: One final question. Is it considered a best practice to prototype a function and have it after the main function or is it ok for me to just write the function first then do main?
The way I learned it is to prototype my functions before main, and then define them below main. It's mainly up to you, but that's the general practice on how I learned it early on in my classes. It's just a way to have neater code organization.

I also know this is not what I asked above but I did not want to start a new thread.

Code:
#include <iostream>
using namespace std;
int add(int x, int y)
{
   return x+y;
}
int sub(int x, int y)
{
  return x-y;
}
int mult(int x, int y)
{
  return x*y;
}
int div(int x, int y)
{
    return x/y;
}
 
int main()
{
    int x;
    int y;
    int choice; // This is the users choice of operation.
    cout<<"Hello and welcome to the basic arithmetic calculator!\n";
    cout<<"please input 2 numbers to calculate: \n(2)";
    cin>>x >>y;
    cin.ignore();
    cout<<"1. Add\n";
    cout<<"2. subtract\n";
    cout<<"3. Multiply\n";
    cout<<"4. Divide\n";
    cout<<"Which operation do you want to perform?\n";
    cin>>choice;
    switch(choice)
    {
        case 1: add();
        break;
        case 2: sub();
        break;
        case 3: mult();
        break;
        case 4: div();
        break;
    }

I know what is wrong at line 46. I need to put int values into the functions for the switch case but I want to use the input of variable x and y in main for all these functions. How can I do this?

To use the values in the functions, you need to pass them in as parameters/arguments.

For instance, your function call should be:

Code:
add(x, y);

You're getting an error on that line (and the other function calls as well) because you are missing arguments. If you look at your function definitions, you'll see that you have parameters defined. Pass 2 int variables (or just plain numbers as long as its an int), and the error(s) will go away.

Code:
int add(int x, int y) //notice the int x and int y here? those are required parameters that you should be passing into the function
{
   return x+y;
}
 
Once again you have helped me in my programming quest. I would have thanked you more but I "Need to spread more around" to others first. You explained this all very well and in a clear and concise manner. Thanks
 
Not a problem, glad to help.

Don't hesitate to ask if you need anything else clarified.
 
Ok I made the correction to the code and it works great. For a basic calculator would it be better to use all double floating points instead of intergers? Also How would I get my program to start over after it has been run? Try executing the code and you will see what I mean. I want to restart after the calculation. This is not in my book yet and there must be another term to use because it is not in the index.

Code:
#include <iostream>
using namespace std;
int add(int x, int y)
{
   return x+y;
}
int sub(int x, int y)
{
  return x-y;
}
int mult(int x, int y)
{
  return x*y;
}
int div(int x, int y)
{
    return x/y;
}

int main()
{
    int x;
    int y;
    int choice; // This is the users choice of operation.
    cout<<"Hello and welcome to the basic arithmetic calculator!\n";
    cout<<"please input 2 numbers to calculate: \n";
    cin>>x >>y;
    cin.ignore();
    cout<<"1. Add\n";
    cout<<"2. subtract\n";
    cout<<"3. Multiply\n";
    cout<<"4. Divide\n";
    cout<<"Which operation do you want to perform?\n";
    cin>>choice;
    switch(choice)
    {
        case 1:
            cout<<"The total is " <<add(x,y)<<"\n";
            break;
        case 2:
            cout<<"The difference is" <<sub(x, y) <<"\n";
            break;
        case 3:
            cout<<"The product is " <<mult(x, y) <<"\n";
            break;
        case 4:
            cout<<"The quotient is " <<div(x, y) <<"\n";
            break;
    }
 
 
 

}
 
Yes, it would probably be better to use doubles for a simple calculator app; otherwise, your calculator won't support decimals, and the divide function wouldn't work that well ;).

As for getting your program to repeat, you could encapsulate your content in main inside of a do-while loop, e.g.

Code:
int main()
{
    char again;
    do
    {
        //your existing code here
        //add new code here
        cout << "Run again? <Y / N>" << endl;
        cin >> again;
    }
    while(again == 'y' || again == 'Y');
}

That's the simplest way to do it, IMO.
 
|| means a logical "OR".

So when you see || you can say "either this OR this". Only 1 statement in an OR comparison needs to be true for the "entire" statement to be true.
It works like this:

We'll take my above code,
again == 'y' || again == 'Y'

What it's doing is comparing the value in again to either lowercase y or uppercase Y. If the value in again is 'y' then the left hand side is "TRUE", and the right hand side is "FALSE". Even though the right side is false, the left side is true, so execution continues. The same would be for if the value in again was 'Y'. The left side would be FALSE, but the right side would be TRUE, and since 1 value is true, execution continues.

However, if you enter in the character T. It would be FALSE for both the left and right side, so execution of the loop would stop.

More info here: Operators - C++ Documentation

Scroll down to Logical Operators and you'll see a better explanation.

You should also be aware of what's called "short circuit" which is also explained on that page, just below the Logical Operators explanations.
 
Thanks for the info. I was actually messing around with code the other day and was think if there was a way to do an "or" type of command. You just solved 2 of my code problems in one suggestion with one answer.
 
Back
Top Bottom