I'm getting the following error when trying to write to a file:

1>Linking...
1>mit_lesson1.obj : error LNK2019: unresolved external symbol "int __cdecl do_stuff(int,int,int,int)" (?do_stuff@@YAHHHHH@Z) referenced in function _main
1>C:\Users\Michael\Documents\Visual Studio 2008\Projects\MIT\Debug\MIT.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\Michael\Documents\Visual Studio 2008\Projects\MIT\MIT\Debug\BuildLog.htm"
1>MIT - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Any advice would be deeply appreciated! Thanks!

#include <iostream>
#include <fstream>
using namespace std;
int by_reference(int &n1, int &n2, int &n3, int &n4);
int do_stuff(int n1_val, int n2_val, int n3_val, int n4_val);
//int swap_stuff(int &n1,int &n4);
void write_file(int n1, int n2, int n3, int n4);
int main()
{
	int n1,n2,n3,n4;
	
    by_reference(n1,n2,n3,n4); //call by_reference
	do_stuff(n1,n2,n3,n4);     //call do_stuff
	//swap_stuff(n1,n4);         //call swap_stuff
system("pause");
	system("CLS");
	cout<<"Results after the swap"<<endl;
	cout<<n1<<endl;
	cout<<n2<<endl;
	cout<<n3<<endl;
	cout<<n4<<endl;
	write_file(n1,n2,n3,n4);
system("pause");
	return 0;
}

int by_reference(int &n1, int &n2, int &n3, int &n4)
//get the values from user by reference and display//
{
    cout<<"Please enter a number for n1:"<<endl;
	cin>>n1;
	system("CLS");
	cout<<"Please enter a number for n2:"<<endl;
	cin>>n2;
	system("CLS");
	cout<<"Please enter a number for n3:"<<endl;
	cin>>n3;
	system("CLS");
	cout<<"Please enter a number for n4:"<<endl;
	cin>>n4;
	system("CLS");
	return n1,n2,n3,n4;
}
int do_stuff(int &n1_val,int &n2_val,int &n3_val,int &n4_val)

{
	int temp;

	temp = n1_val;
	n1_val = n4_val;
	n4_val= temp;
	



	return n1_val,n2_val,n3_val,n4_val;
}

/*int swap_stuff(int &n1,int &n4)
//Swap n1 and n4//
{
	int temp;
	temp = n1;
	n1=n4;
	n4=temp;

	return n1,n4;
}   */

void write_file(int n1, int n2, int n3, int n4)
{
	cout<<"Writing results to a file..."<<endl;
	ofstream myfile;
	myfile.open("stuff.dat");
	myfile<<"These are the results of n1, n2, n3,and n4: "<<n1<<n2<<n3<<n4;
	myfile.close();
	return;
}

Your prototype and definition of do_stuff don't match. int and a reference to an int are two different types.

Wow, that's incredibly embarrassing, I know better than that haha. Thanks for the advice! I have one more question. I want my program to exit if the file fails to open. I guess for that I'd need to check for the end of file? Any advice here? Thanks in advance!

Look up the function fstream::is_open(). There are also versions for ifstream and ofstream objects.

So I took your suggestion, and I wrote some more code. This is what I came up with, I'm getting the following errors though:


1>mit_lesson1.cpp
1>c:\users\michael\documents\visual studio 2008\projects\mit\mit\mit_lesson1.cpp(76) : error C2059: syntax error : '.'
1>c:\users\michael\documents\visual studio 2008\projects\mit\mit\mit_lesson1.cpp(77) : error C2143: syntax error : missing ';' before '{'
1>c:\users\michael\documents\visual studio 2008\projects\mit\mit\mit_lesson1.cpp(83) : error C2181: illegal else without matching if
1>c:\users\michael\documents\visual studio 2008\projects\mit\mit\mit_lesson1.cpp(87) : error C2043: illegal break

void write_file(int n1, int n2, int n3, int n4)
{

	cout<<"Writing results to a file..."<<endl;
    ofstream myfile;

	if (ofstream.is_open())
	{
   
    cout<<"<File successfully open...>"<<endl;
	myfile<<"These are the results of n1, n2, n3,and n4: "<<n1<<n2<<n3<<n4;
	myfile.close();
	}
	else
	{
		cout<<"<Error opening file...>"<<endl;
		cout<<"Exiting the program...>"<<endl;
		break;
	
	}
	return;
}

Thanks once again for the help!

...
1>c:\users\michael\documents\visual studio 2008\projects\mit\mit\mit_lesson1.cpp(76) : error C2059: syntax error : '.'
...

In the quote, the bolded portion indicates the file and line number where the error is likely to be. In this case, the error is on Line 76. If you look in your file at that line, you'll probably see this line (Line 7 in the posted code):

if (ofstream.is_open())

The error here is that you are incorrectly attempting to execute a non-static method from its class instead of from an object that is an instance of that class (please note that the syntax for what I mentioned is different, what you have wouldn't work). The identifier "ofstream" is a class name. You should be using the object that's an instance of that class, which is "myfile". I believe that the errors indicated on Lines 77 and 83 will disappear when you correct this as I do not see anything else that would confuse the compiler to that extent.

The error that is indicated on Line 88 is Line 18 in the posted excerpt. This should not be a "break", this is not a legal use. The word "break" is used to either terminate a loop or cause execution to jump out of a switch. It should be an "exit". Although, this sort of program termination isn't generally advised.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.