returning a string

...

Alright this is much better! But i dont know the primary expression that is exepected on 27 before openInputFile.

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
int main()
{
    string MyFile, 
    sFile = "hello.cpp";
    
    cout << "Enter a file: ";
    cin >> MyFile ;
    
    fstream ReadFile( fstream openInputFile );  // declare the fp within main()
       
    
    if ( MyFile != sFile )    // file must be 'hello.cpp' to proceed
    {
    cout << "Error";
    exit(0);
    }
                         // if correct file is entered print the contents of it
            cout << ReadFile( fstream openInputFile ); 
                        
    
    system("pause");    // temporary! 
    return 0;
}

// done with main now we can start this function
fstream ReadFile( fstream openInputFile )
{ 
       string sLine;   
       while ( getline ( openInputFile, sLine ) )
         {
			if ( sLine.length() == 0 ) continue;  // to proceed
			
      		 for ( int chgLine = 0; chgLine < sLine.length(); chgLine++ )
	     	 {
                if ( sLine[chgLine] >= 97 && sLine[chgLine] <= 122 || sLine[chgLine] >= 65 && sLine[chgLine] <= 90  )
                {
                     cout << sLine[chgLine];
                }
                
             }
          }
}

Last edited on
Line 9: that's not how you call a function: ReadFile(???);
There are things missing:
* What is openInputFile?
* Why does ReadFile() return an fstream?

If you have a function declared as
int f(int);
you don't call it like this:
int f(int 4);
You call it like this:
var=f(4);

Are you getting any other errors?
No other errors..

Are their tutorials for these things? I have yet to find any.


You call it like this:
var=f(4);

How would i use this?
Streams don't have copy constructors, so you have to pass them by reference.
As helios said you are calling the function in a wrong way.
Here some tutorials on using functions:
http://www.cplusplus.com/doc/tutorial/functions.html
http://www.cplusplus.com/doc/tutorial/functions2.html
http://www.cprogramming.com/tutorial/print/lesson4.html
Last edited on

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
#include <fstream>
#include <string>
#include <iostream>

using namespace std;

ifstream ReadFile( ifstream& openInputFile, string& sLine, int& chgLine )
{ 
       
       while ( getline ( openInputFile, sLine ) )
       {
			if ( sLine.length() == 0 ) continue;  // to proceed
			
             for ( chgLine = 0; chgLine < sLine.length(); chgLine++ )
	     	 {
                if ( sLine[chgLine] >= 97 && sLine[chgLine] <= 122 || sLine[chgLine] >= 65 && sLine[chgLine] <= 90  )
                {
                     cout << sLine[chgLine];
                }
                
             }
        }
}

int main()
{
    int chgLine;
    string MyFile, sLine, 
    sFile = "hello.cpp";
    
    cout << "Enter a file: ";
    cin >> MyFile ;
    
    ifstream openInputFile;
      
    
    if ( MyFile != sFile )    // file must be 'hello.cpp' to proceed
    {
       cout << "Error";
       exit(0);
    }
                         // if correct file is entered print the contents of it
   
        ReadFile( openInputFile, sLine, chgLine );
    
    system("pause");
    return 0;
}
Last edited on
Declaration:ifstream ReadFile( ifstream& openInputFile, string& sLine, int chgLine ) (three arguments: openInputFile, sLine, chgLine)
Call: ReadFile( open ); (one argument: open)
You have to pass the same number of arguments and of the same type used on the declaration. (If you want it, you can declare an argument to be optional)
Last edited on
It complies now but im getting some oct code. My Plan was to use the ReadFile func to shorten the length of main.

I dont like the fact that i have to re-declare my paramters but evidently thats just the way life is.

Anyways, I think im going to do this step by step, one FP that does its own individual task, i.e. openFile, readFile, do something with the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ifstream openInputFile()
{
        string MyFile,
        ifstream inFile;
        
        inFile.open( MyFile.c_str(), ios::in );
        // return a pointer is what im looking for.    
}

int main()
{
   // prompt user for the file
   // validate
   // use openInputFile to find if userInput matchs the file
}


Its so simple. Im so ashamed.
Keep in mind that local objects go out of scope when the function that created them returns, so if you return a pointer to one and try to use that pointer, you'll cause a segmentation fault.
Example:
1
2
3
4
int *f(){
	int a;
	return &a; //Error!
}
Topic archived. No new replies allowed.