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

Recommended Answers

All 2 Replies

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().

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.