Help needed

Hi! Appreciate some help in this... Does anyone know what's wrong? I'm trying to get the program to read a text file I have on my desktop and do a word count. However, when I run the program, it prompts me to key in the path of the file and I did so, and I receive an error message:

'Lab 9.exe': Loaded 'C:\Users\Lim\Documents\Visual Studio 2010\Projects\Lab 9\Lab 9\Debug\Lab 9.exe', Symbols loaded.
'Lab 9.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'Lab 9.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'Lab 9.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
'Lab 9.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
'Lab 9.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
The program '[4512] Lab 9.exe: Native' has exited with code 0 (0x0).


#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;


int numCharsInFile( ifstream &in, int &numLines );

void main ()
{
int nLines,
nChars,
avgCharsPerLine;

ifstream inFile;

string fileName;

fileName = getInputFileName();

inFile.open(fileName.c_str());

if( !inFile.is_open() )
{
cerr << "Cannot open file: " << fileName << endl << endl;
exit (0);
}

nChars = numCharsInFile( inFile, nLines );

avgCharsPerLine = nChars / nLines;


cout << "The number of characters in the file: " << fileName
<< " is = " << nChars << endl << endl;

cout << "The number of lines in the file: " << fileName
<< " is = " << nLines << endl << endl;

cout << "The average number of characters per line in the text file: "
<< fileName << " is: " << avgCharsPerLine << endl << endl;

inFile.close();

}

string getInputFileName()
{
string fName;

cout << "Please enter the fully qualified name of the " << endl
<< "input text file (i.e. including the path): ";
cin >> fName;
cout << endl;

return fName;
}


int numCharsInFile( ifstream &in, int &numLines )
{
int numChars = 0;

char ch; // character holder;

numLines = 0;

while ( in.get(ch) )
{
if ( ch != '\n' )
numChars++;
else
numLines++;
}

return numChars;
}
The return type of main should be int.

You are trying to use getInputFileName() before it has been declared. put a function declaration for getInputFileName above main as you have done with numCharsInFile.
Topic archived. No new replies allowed.