I'm having issues with a rather simple program that prints a string.

I have two .cpp files, main and login, with login.h header file.

The problem at hand is that when I want to print the string in main.cpp that was entered from login.cpp - nothing is shown in which I assume the string is not being returned correctly.

main.cpp :

#include <iostream>
#include <fstream>
#include <string>
#include "login.h"
using namespace std;

string pfile;

int main () {		

	login(pfile);
        cout << "Main." << endl;	
	cout << pfile;

	return 0;
}

login.cpp:

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

string login(string pfile) {

	cout << "Login." << endl;
	getline(cin, pfile);

	return pfile;	
}

login.h:

#include <string>
using namespace std;
#ifndef LOGIN_H
#define LOGIN_H

string login(string);

#endif

You need to pass the string by reference, not by value. string login(string& pfile); or you can catch the return value in main pfile = login(pfile); When you do that there is no reason to have a parameter to login().

Edited 5 Years Ago by Ancient Dragon: n/a

You need to pass the string by reference, not by value. string login(string& pfile); or you can catch the return value in main pfile = login(pfile); When you do that there is no reason to have a parameter to login().

Ah I get it, thanks a lot.

This question has already been answered. Start a new discussion instead.