Just a quick question, is it possible to pass C++ strings from main into a function. May sound simple but it's not working for me.
Also does anyone know of a good tutorial on writing to and reading from a .txt file.
Thanks for your help.
Printable View
Just a quick question, is it possible to pass C++ strings from main into a function. May sound simple but it's not working for me.
Also does anyone know of a good tutorial on writing to and reading from a .txt file.
Thanks for your help.
You mean something like this:
Output:Code:#include <string>
#include <iostream>
void func(std::string str)
{
std::cout << str << std::endl;
}
int main()
{
std::string str = "Hello World";
func(str);
}
Code:Hello World
You mean something like this:
As you probably know, I think the string class is complex enough to pass a const reference instead of making a copy.Code:#include <string>
#include <iostream>
void func(const std::string& str)
{
std::cout << str << std::endl;
}
int main()
{
std::string str = "Hello World";
func(str);
}
Thanks for your input on this, they have both helped me in my functions, sorry to be so vague but the prob is i'm only starting my development and very little to offre by way of concrete code.