passing ifstream and ofstream types in functions

Hello, I am new to C++ and trying to get to grips with it. Particularly I am having difficulty using both header files and passing ifstream and ofstream types into a function ( i am attempting to kill two birds with one stone). I am getting numerous errors. Any help would be greatly appreciated.

My codes and errors are listed below

Header.h

#pragma once
#ifndef HEADERCODE_h
#define HEADERCODE_H

int factorial(int number);

void write_in_file(ifstream& instream, ofstream& outstream,int& user_integer);

#endif

Header.cpp

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <fstream>
#include "Header.h"
using namespace std;

int factorial(int number)
{

int product = 1;
for (int i = 1; i <= number; i++) {
product = product*i;
}

return product;
}

void write_in_file(ifstream& instream, ofstream& outstream, int& user_integer) {

outstream << "The sqare root of " << user_integer << " is " << sqrt(user_integer) << " and the factorial is " << factorial(user_integer);

}

main.cpp


#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <fstream>
#include "Header.h"
using namespace std;

int main() {

ifstream in_stream;
ofstream out_stream;
int user_integer;

in_stream.open("file");
out_stream.open("file");

cout << " Enter a positive integer \n";
cin >> user_integer;
write_in_file(in_stream, out_stream,user_integer);

in_stream.close();
out_stream.close();
return 0;
}


errors:


Error C2065 'ifstream': undeclared identifier
Error C2065 'instream': undeclared identifier
Error C2065 'ofstream': undeclared identifier
Error C2065 'outstream': undeclared identifier
Error C2062 type 'int' unexpected
Error identifier "ifstream" is undefined
Error identifier "instream" is undefined
Error (active) expression preceding parentheses of apparent call must have (pointer-to-) function type ( this is when I call the function write_in_file)
I guess you have made a bit of confusion with what you wrote. Code is a bit messy. I am not an experienced programmer but, according Stroustrup's way, he says it is better if you keep ifs/ofs separated.

You can set up two functions that compile a vector to import and another one to export or simply prints results out.
Last edited on
Where are those errors are? Where they are refer to?
It is complaining because your header file is not properly structured -- meaning that the compiler does not know what a ifstream is when it first encounters it.

It would work if you were to swap a couple of lines in your .cpp files:

1
2
using namespace std;
#include "Header.h" 

Rather than do that, though, you should correct your header:

header.h
1
2
3
4
5
6
7
8
9
10
11
#pragma once
#ifndef HEADERCODE_H
#define HEADERCODE_H

#include <fstream>

int factorial(int number);

void write_in_file(std::ifstream& instream, std::ofstream& outstream,int& user_integer);

#endif 

You need to #include the headers required for the function prototypes in header.h.
You need to use std:: namespace prefix for C++ library stuff used in header.h.
(Notice also I fixed line 2.)

Hope this helps.
Duoas, I love you. Now I can finally move on and learn some interesting stuff.
Topic archived. No new replies allowed.