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;
}
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:
#include <cstdlib>
#include <iostream>
usingnamespace 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.