Pass String Through Function

I'm trying to pass the contents of a the file (Assignment3.txt) into a string. The text in the file is Ole Miss Stinks. I am able to retrieve the contents of the file and place it into string str in function readString. The value does get stored, but I'm having trouble passing it back up into main so that the string can be used throughout other functions. For example, I want the contents of str in readString to be able to be called in sayString. Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>

using namespace std;

void askFileName();
void readString(string& newStr);
void sayString(string newStr);

int main()
{
    string str;
    string newStr;

    string filename;
    askFileName();
    cin >> filename;
    readString(str);
    sayString(newStr);
    
    system ("PAUSE");
    return 0;
    
}

void askFileName()
{
     cout << "Please type in the file name: "; 
}

void readString(string& newStr)
{
     ifstream x;
     string str;
     x.open("Assignment3.txt");
     getline(x,str);
     x.close();
     
     newStr = str;
     
     //cout << newStr;  //Used just to test and make sure that
                     //the value is stored in str.
}

void sayString(string finalString)
{
       cout << finalString;

} 
Look at lines 18,19

You're reading the string into 'str', but then you're printing 'newStr'.

IE: you're not printing the same string you're reading.
Topic archived. No new replies allowed.