Hey, first time posting here. I am new to C++ (day 2 since I started learning it) so bear with me.
Ok so my question is if I were to pass some data or values from the main() function through a void function (which main purpose is to display the data in a certain manner); how should one go about doing so?
For instance:
Code:
// example.cpp -- poorly written, just trying to learn
#include <iostream>
void format()
{
using namespace std;
cout << "Time: " << min << hrs << endl;
};
int main()
{
using namespace std;
int hrs;
cin hrs;
cin min;
format(); // call the void format to display them in a certain way
return 0;
}
Obviously this is really poorly written, and confusing for the user. My main goal is to learn how to pass through values to a void function.
Also a bit off-topic, but is there a downside to place "using namespace std;" outside a function say if out of 100 functions only 10 of them use it? Would it make the program slower/unstable in any way or is this something one could do without any downsides?
Please excuse my English if it is hard to understand - it's not my mother tongue.
If you didn't have 'using namespace std;' in your code then the program would look like (which compiles and runs!)
Code:
// example.cpp -- poorly written, just trying to learn
#include <iostream>
int hrs,
min;
void format()
{
std::cout << "Time: " << min << hrs << std::endl;
};
int main()
{
std::cin >> hrs;
std::cin >> min;
format(); // call the void format to display them in a certain way
return 0;
}
As cout and cin are defined as part of what is called the std namespace you need to tell the compiler every time you use them where they are located using the scope operator :: To avoid having to do this everytime, you can tell the compiler to default to using a specific namespace (usually std) by 'using namespace std'. If this is used, it's normally placed after the #include statements.
Code:
// example.cpp -- poorly written, just trying to learn
#include <iostream>
using namespace std;
int hrs,minut;
void format()
{
cout << "Time: " << minut << hrs << endl;
};
int main()
{
cin >>hrs;
cin >>minut;
format(); // call the void format to display them in a certain way
return 0;
}
Note that I've had to change your variable min to minut as min is already defined in the std namespace. Having 'using' in your program does not make it slower etc as its just for the benefit of the compiler. At this stage of your c++ education, don't get too hung up on 'using namespace'. Its use should become clearer much later when you learn about namespaces.
Hope this helps. Good luck in learning c++ and welcome to the c++ community.
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Hey, first time posting here. I am new to C++ (day 2 since I started learning it) so bear with me.
Ok so my question is if I were to pass some data or values from the main() function through a void function (which main purpose is to display the data in a certain manner); how should one go about doing so?
For instance:
Code:
// example.cpp -- poorly written, just trying to learn
#include <iostream>
void format()
{
using namespace std;
cout << "Time: " << min << hrs << endl;
};
int main()
{
using namespace std;
int hrs;
cin hrs;
cin min;
format(); // call the void format to display them in a certain way
return 0;
}
Obviously this is really poorly written, and confusing for the user. My main goal is to learn how to pass through values to a void function.
Also a bit off-topic, but is there a downside to place "using namespace std;" outside a function say if out of 100 functions only 10 of them use it? Would it make the program slower/unstable in any way or is this something one could do without any downsides?
Please excuse my English if it is hard to understand - it's not my mother tongue.
- Heew
Passing arguments as parameters is typically a better way to do it than setting up globals.
Yeah both of you were helpful, I managed to fix it now.
As for the missing operators it was a silly mistake which I noticed shortly after posting this (I found no edit button), anyhow thanks both of you. I was tired at the time of posting this the most crucial part of this post (which was to pass my question/visualize it) was successful. Again thanks!
- Heew
Last edited by heew; March 25th, 2013 at 05:04 AM.
Bookmarks