I'm having trouble with this program from right out of the book. Its just supposed to write an integer array to a file that it creates. I've compiled it without any errors, but when I try to run it, it fails and tries to debug. can anyone help me figure out whats wrong?

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std; 

int main()
{
	const int ROWS = 3, 
		      COLS = 3; 
	int num[ROWS][COLS] = {34, 55, 1899, 
				 77, 999, 2837, 
				 78, 3882, 1}; 

	fstream writeFile; 
	writeFile.open ("testFile.txt", ios::in);    // To test if the file already exists
	if (writeFile.fail())
	{
		writeFile.open ("testFile.txt", ios::out | ios::in);  
		for (int row = 0; row < ROWS; row++)
		{
			for (int col = 0; col < COLS; col++)
			{
				writeFile << setw(8) << num[row][col]; 
			} writeFile << endl;
		}
		writeFile.close(); 
	}

	else 
	{
		writeFile.close();	// Is there another way to add ios::out with out closing the file first? 
		writeFile.open ("testFile.txt", ios::out | ios::in); 
		for (int row = 0; row < ROWS; row++)
		{
			for (int col = 0; col < COLS; col++)
			{
				writeFile << setw(8) << num[row][col]; 
			} writeFile << endl; 
		}
		writeFile.close(); 
	}
	return 0; 
}

these are the errors after running the program.

'messing around.exe': Loaded 'C:\Users\Tyler\Documents\Visual Studio 2005\Projects\messing around\debug\messing around.exe', Symbols loaded.
'messing around.exe': Loaded 'C:\Windows\System32\ntdll.dll', No symbols loaded.
'messing around.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', No symbols loaded.
'messing around.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc80.debugcrt_1fc8b3b9a1e18e3b_8.0.50727.762_none_24c8a196583ff03b\msvcp80d.dll', Symbols loaded.
'messing around.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc80.debugcrt_1fc8b3b9a1e18e3b_8.0.50727.762_none_24c8a196583ff03b\msvcr80d.dll', Symbols loaded.
'messing around.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', No symbols loaded.
The program '[6212] messing around.exe: Native' has exited with code 0 (0x0).

I just tried to run some other programs and the same error message came up. any ideas?

1) Try rebuilding it.
2) How did you create this project?

I did it on Dev C++, not Visual Studio. It compiled and ran, but did not work. When I changed the first part to this:

fstream writeFile; 
	writeFile.open ("testFile.txt", ios::in);    // To test if the file already exists
	if (writeFile.fail())
	{
                writeFile.clear ();
		writeFile.open ("testFile.txt", ios::out);  
		for (int row = 0; row < ROWS; row++)
		{
			for (int col = 0; col < COLS; col++)
			{
				writeFile << setw(8) << num[row][col]; 
			} writeFile << endl;
		}
		writeFile.close(); 
	}

Note lines 5 and 6. I added the "clear" since the stream had already failed and I wanted to reset it. For line 6, it worked better if I just specified ios::out.

Doing so created the file with the numbers. As to the second part, I don't know what you are trying to do when the file with the numbers already exists, so I didn't touch that one. As to the run-time errors you got, again I didn't get any. Maybe it's a Visual Studio thing. I used Dev C++.

The program compiled and runned just fine. Check the file in the directory.

Those message that you receive are extra debug information.
"Symbols Loaded" and "No symbols loaded" has to do with whether or not you can see the source code. You see that only your program shows "Symbols loaded". This is because you have source code of it.

Also in general applications will exit with "0" if no errors occured. Afterall "int main" ends with "return 0"

Hope it helped :)

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.