function to output file problem

I have a lab that I have been working on and need a little help with it. I am trying to call the do_stuff function, get user inputs for 4 numbers (n1, n2, n3, n4) and have the user inputs be written to an output file. It is pretty basic but can not link the two together for the life of me. Have searched for ways to do this.

#include <iostream>
#include <fstream>

void do_stuff(int par1_val, int par2_val, int par3_val, int par4_val );

int main( )
{
using namespace std;
int n1, n2, n3, n4;

n1 = 1;
n2 = 2;
n3 = 3;
n4 = 4;

ofstream outputFile;
outputFile.open("lab1.txt");

do_stuff(n1, n2, n3, n4);

outputFile.close();
cout << "Done!\n";

return 0;
}

void do_stuff(int $n1, int $n2, int $n3, int $n4)
{
using namespace std;
cout << "Please enter a number for n1: " << endl;
cin >> $n1;
outputFile << $n1 << endl;
cout << "Please enter a number for n2: " << endl;
cin >> $n2;
outputFile << $n2 << endl;
cout << "Please enter a number for n3: " << endl;
cin >> $n3;
outputFile << $n3 << endl;
cout << "Please enter a number for n4: " << endl;
cin >> $n4;
outputFile << $n4 << endl;
}
Last edited on
you didn't pass the address of outputFile to the do_stuff function
If i understood well you want to creat a program that asks you to insert a number and then he will write the number you tyed in, right? If that's it here is the code I've just made:

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
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n1,n2,n3,n4;
    
    cout <<"Insert number for n1:"<<endl;
    cin >> n1;
    cout <<"The number is "<<n1<<endl;
    cout <<"Insert number for n2:"<<endl;
    cin >> n2;
    cout <<"The number is "<<n2<<endl;
    cout <<"Insert number for n3:"<<endl;
    cin >> n3;
    cout <<"The number is "<<n3<<endl;
    cout <<"Insert number for n4:"<<endl;
    cin >> n4;
    cout <<"The number is "<<n4<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Thank you for the quick replies, what I am trying to do is write a program where in main the do_stuff function is called. The do_stuff function needs to accept 4 user inputs (n1, n2, n3, n4) and then output those to a txt file. I believe by reference. Alex cpp, that would have worked if I did not need a function.
Last edited on
All i can do for you is write down the code for a program that makes .txt files so..

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
#include <fstream>
#include <iostream>
#include <string.h>
using namespace std;

int main(int argc, char *argv[])
{
    string testo;
    
    ofstream File;
    File.open("TextWriter.txt");
    cout <<"                          .:::Text Writer:::."<<endl;
    cout <<"                             By Alex ******  "<<endl;
    cout <<" "<<endl;
    cout <<" "<<endl;
    cout <<" "<<endl;
    cout <<"Instead of putting space: \" \" you must put underscore: \"_\""<<endl;
    cout <<"Insert Text:"<<endl;
    cout <<" "<<endl;
    cin >> testo;
    File <<" "<<testo<<endl;
    File.close();
    cout <<"File has been written!"<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


I know it might not be of any help but it's all I can do.
cout <<"Instead of putting space: \" \" you must put underscore: \"_\""<<endl;

This is awkward and not necessary at all. Instead of cin >> testo; read input with std::getline:

std::getline(std::cin, testo);
Man..Thanks filipe i was looking for this for ages. :)
Topic archived. No new replies allowed.