Hi guys,

When I try to compile the code below I get an error saying: error C2065: 'file' : undeclared identifier. But if I run the program it still works.

// WordStreamTest.cpp
//--------------------------------------------------------------------

#include "WordStream.h"

//--------------------------------------------------------------------

using namespace std;

//--------------------------------------------------------------------

void OutputNumber (const string &str);
void OutputLine (int length);
void testNext (WordStream &file);

//--------------------------------------------------------------------
int main()
{
    string filename;
    cout << "Enter file name: ";
    cin >> filename;
    
    //error here
    try
    {
        WordStream file(filename);

    }
    catch(...)
    {
        cout << "File cannot be opened!" << endl;
        exit(1);
    }

    testNext(file);
    
    

    return 0;
}

//--------------------------------------------------------------------

void testNext (WordStream &file)
{
    string word;

    OutputNumber("Getting word from file stream");
    cout << "Expected Output: this is a test to see if this number 1 test works\n";
    cout << "Actual Output: ";
    while (file.Next(word))
        cout << word << " ";
    cout << "\nIF Expected Ouput matches Actual Output:\n"
         << "THEN Test Passed\n";
    system("pause");
}

//--------------------------------------------------------------------

void OutputNumber (const string &str)
{
    static int num = 0;
    
    num++;
    
    cout << "\n";
    
    OutputLine ((int)str.length() + 17);
    
    cout << " TEST NUMBER " << num << ": " << str << "\n";
    
    OutputLine ((int)str.length() + 17);
}

//--------------------------------------------------------------------

void OutputLine (int length)
{
    for (int index = 0; index < length; index++)
    {
        cout << "-";
    }
        cout << "\n";
}
//--------------------------------------------------------------------

Here is the class definition to go with the above code:

// ----------------------------------------------------------------------------------------
// WordStream.h
//
// Author: Warren Knox
// Version: 01
// Last Revised: 31/10/2006
// Description: A file stream that reads in one word at a time from the file and then 
//                strips the word of any non alphanumeric trailing and leading characters
//  
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------

#ifndef WORDSTREAM_H
#define WORDSTREAM_H

//-----------------------------------------------------------------------------------------

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

//-----------------------------------------------------------------------------------------

using namespace std;

//-----------------------------------------------------------------------------------------

class WordStream
{
public:
    //Default Constructor
    WordStream () {};

    //Constructor that takes a filename as a parameter
    WordStream (string &filename);

    //Deconstructor that close the filestream
    ~WordStream () {Close();};

    //Closes the file stream if one is open
    void Close ();

    //Fetches the next word from the input file
    bool Next (string &word);

private:
    //Pointer to the input file
    ifstream m_file;

    //Removes the trailing and leading characters if they are non alphanumeric
    void Strip( string &word);

    //Converts the word to all lower case characters
    void MakeLower( string &word);
    
    WordStream &operator = (WordStream const &stream) {};

};
#endif

If anyone can point me in the right direction to what I need to do it would be greatly appreciated.

>>But if I run the program it still works.
You are not running the program that you think you are unless you are using a really really old and ancient compiler. None of the compilers I have seen in recent years will produce an executable program when there are compiler errors.

>>When I try to compile the code below I get an error saying: error C2065: 'file' : undeclared identifier.

Tis true -- that variable only has scope within the try block where it was declared. If you want to use it later you have to declare the variable above the try block.

I see no advantage of that try block anyway, but possibly your program is not finished yet???

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.