I want to declare a string, initialize it by passing it by reference, and then pass it by value to the 'outputfile' function.
The code below works, but I don't know why. In main I would expect to pass the string 'filename' like
startup(&filename)
But that gives an error, and the code below doesn't. Why? Also, is there a better way to do this without using a return value?
#include <iostream>
#include <string>
using namespace std;
void startup(std::string&);
void outputfile(std::string);
int main()
{
std::string filename;
startup(filename);
outputfile(filename);
}
void startup(std::string& name)
{
cin >> name;
}
void outputfile(std::string name)
{
cout << name;
}