Returning a string from function

Hi guys,

For a program I am writing I trying to return a string from a file, and I use that string in a different cpp file. The cpp code that read from the file is not in the same cpp file which I want to use the string in. I have linked the two using a header file. My question is how do I return the string called number from the following function. If I put string in place of void it gives me an error, if I leave it as void it also gives me an error and if I put char* it gives me an error saying "cannot convert 'std::string' to 'char*' in return".
Please help thank you.

#include <fstream>
#include <string>
#include <iostream>
#include "getBoard.h"

using namespace std;
using std::string;

void File(){
ifstream readFile;
readFile.open("ReadTest.txt");
size_t marker;
string data, line, number="";

if(readFile.is_open()){

while(readFile.good()){
getline(readFile, line);
data = data +line;
}
}
else{
cout << "error opening file \n";
return "Error";
}

marker = data.find('a');
marker = marker +3;
number = data.substr(marker, 81);
cout << number << "\n";
return number;
}
To return a string you just do this:

1
2
string File(){
//... 


Make sure the function prototype in the header also returns a string.

The error you described doesn't make any sense to me, based on the code I see. Which line is that error occuring on?
I tried doing what you said but it gives me an error on line 3 saying that string doesn't name a type.

And the other error I was talking about occurs on line 31 which is the line right before the last bracket
Topic archived. No new replies allowed.