Hi there, I’m fairly new a c++ and am having trouble figuring out how to use a returned function variable.
Here’s my code
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
std::string OMPS (char const* path)
{
//set string array length
string line[1000];
//loop counter
int n = 0;
//set text file to read
ifstream myfile(path);
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line[n]);
cout << line[n] << endl;
//move on to next line
n++;
}
myfile.close();
}
else cout << "Unable to open file";
string x = line[3];
cout << x << endl;
return x;
}
int main()
{
OMPS("test.txt");
cout << OMPS <<endl;
system("pause");
return 0;
}
When i print x from the OMPS function it give the correct output which is an entry in the line array
(6522 2 0 44 789 325 2.001 26.33 65.22 3.11)
But when i call it from main it just gives me the memory address, as you can probably tell i don't really understand pointers!
Ideally i would like to put each "line" into an array and pass the whole lot to main, but that seems like a whole world of pain!
Any Ideas
Many Thanks in advance
Al