Introduction to programming in c include I am pretty new at

Introduction to programming in c++

#include<iostream>

I am pretty new at programming, and this is the first time my professor told me not to use using namespace std i dont get how to do anything without it..

1. Here\'s a program that has the `using namespace std;` line at the

   beginning commented out. Without adding any `using` declarations,

   or removing the comment, modify this program so that it will

   run and do what we would expect (prompt the user for their name

   and reply back to them).

~~~~

#include <iostream>

#include <string>

// using namespace std;

int main() {

cout << \"Please enter your name: \";

string name;

getline(cin, name);

cout << \"Hello, \" << name << \" it\'s nice to meet you.\";

cout << endl;

return 0;

}

Also,

What is the *purpose* of namespaces; what problem do they solve?

Can you describe a situation where not having namespaces would

   cause problems, and then point out how namespaces would solve those

   problems. Thank you.

Solution

Namespace are used for the following reasons .

A large number of users declare user-defined functions / variables with the names of their choices. All are stored in the global namespace ;

If 2 users coincidentally use the same name for a function or a variable, then the functionality gets adversely affected.

So namespaces come into picture.

You can declare the namespaces of your choices. When you do so, a part of the global namespace is allotted to you and your cariables and function names are stored in it thus avoiding clashes.

The above program can be changed to as

#include <iostream>
#include <string>
// using namespace std;

int main() {
std::cout << \"Please enter your name: \";
std::string name;
std::cin>>name;
std::cout << \"Hello, \" << name << \" it\'s nice to meet you.\";
std::cout << std::endl;
return 0;
}

Introduction to programming in c++ #include<iostream> I am pretty new at programming, and this is the first time my professor told me not to use using nam
Introduction to programming in c++ #include<iostream> I am pretty new at programming, and this is the first time my professor told me not to use using nam

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site