Advertisement

Pretty simple file input question(parsing).

Started by December 14, 2000 10:38 PM
3 comments, last by Decoder 24 years, 1 month ago
Ok I have a small program.
          
#include <fstream.h>

int main()
{
	
    char fileName[80]; // string to hold file name for input

    float total_swings = 0; // used to hold total swings made

    float obj_miss = 0; // used to hold total misses

    char ch,            // used to hold each character in file

         firstletter,   // used to hold first letter of object calculated

         yorn;          // used to hold yes or no answer for log output


    float misses;       // used to hold percentage calculation of misses


    cout << "Input file name: ";
    cin >> fileName;

    ifstream fin(fileName, ios::nocreate);    // open for readin


    if (!fin)
	cout << "No such file exist.\n\n";
    else 
    {
        cout << "Insert the **capitalized** first letter of the name of the object you "
        cout << "want \ncalculated. ";	

	cin >> firstletter;	
	
	cout << "\n\n";
	
        while (fin) // while not at the end of the file

	{
	    fin.get(ch); // read every character

		
	    if (ch == firstletter) // total number of times swung(input the capital letter of the object you wanted totalled)

		total_swings++;
	    if (ch == '!') // total number of misses of object you want calculated

	        obj_miss++;
	}	
    
	misses = (obj_miss / total_swings) * 100;

	cout << "Of " << total_swings << " attempts, there were " << obj_miss << " misses.\n";
	cout << "The percentage of misses is: " << misses << "%.\n" << endl;

        fin.close();            // always pays to be tidy

    }

    cout << "Do you wish for a log of the results to be placed on your desktop? \n";
    cout << "y (for yes) or n (for no) ";
	
    cin.ignore(20, '\n');
    cin >> yorn;
    if (yorn == 'y')
    {
	ofstream fout("c:/windows/desktop/%Log.txt", ios::app);    // open file for input at end

	fout << "Of " << total_swings << " attempts, there were " << obj_miss << " misses.\n";
	fout << "The percentage of misses is: " << misses << "%.\n" << endl;
	cout << "Log appended to end of file (%Log.txt)";

	fout.close();
    }

    cout << "\n\n";

    return 0;
}
        
Now, how it is now, I can only parse one character at a time. I want it to parse whole(single) words out of a string. Here's the text of a sample file I'm trying to parse:

[Wed Dec 06 22:32:56 2000] Kann hits greater barbed skeleton for 28 points of damage.
[Wed Dec 06 22:32:56 2000] Kann hits greater barbed skeleton for 55 points of damage.
[Wed Dec 06 22:32:56 2000] Kann tries to hit greater barbed skeleton, but misses!
[Wed Dec 06 22:32:56 2000] Kann hits greater barbed skeleton for 55 points of damage.
[Wed Dec 06 22:32:56 2000] Kann hits greater barbed skeleton for 55 points of damage.
[Wed Dec 06 22:33:02 2000] Kann tries to hit greater barbed skeleton, but misses!
[Wed Dec 06 22:33:02 2000] Kann tries to hit greater barbed skeleton, but misses!
    
Ok, I need it so that everytime the name 'Kann' appears, the 'total_swings' variable increments. As of now, I can only ask the user to input the firstletter(I.E. 'K') to check if the 'total_swings' variable should be incremented. And I need for the 'obj_miss' variable to be incremented when the word 'misses!' gets read by the program. It would be great if the phrase 'Kann tries to hit' could be used to increment the 'obj_miss' variable. Thanks in advance. P.S. If my question is complicated, I'll rephrase in greater detail. Edited by - Decoder on 12/14/00 11:00:58 PM Edited by - Decoder on 12/14/00 11:12:06 PM
Hey, bugs happen, deal with em. :)
Try this:

  #include <iostream>#include <fstream>#include <string>int main(){    float total_swings = 0;    float total_misses = 0;    using namespace std;   // done for simplicity    cout << "Input filename: ";        string filename;    getline(cin, filename);    cin.ignore(1, ''\n'');    ifstream fin(filename.c_str());// , std::ios::nocreate);    if (!fin)    {    	cout << "No such file exist.\n\n";        return 1;    }            cout << "Enter the characters name to collect stats on: ";    string name;    getline(cin, name);    string line;          while(getline(fin, line))    {        if(line.find(name) != string::npos)        {            if(line.find("hits") != string::npos) { ++total_swings; }            else if(line.find("misses") != string::npos) { ++total_misses; }        }    }    fin.close();                float misses = (total_misses / total_swings) * 100;	    cout << "Of " << total_swings << " attempts, there were " << total_misses << " misses." << endl;    cout << "The percentage of misses is: " << misses << "%." << endl;        return 0;}  


Hope that helps.

Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Advertisement
Thanks for the help, but I''m having some problems with the ''string'' class. Here are all 17 errors I get while compiling your source.

C:\Windows\Desktop\test\io\Cpp1.cpp(65) : error C2871: ''std'' : does not exist or is not a namespaceC:\Windows\Desktop\test\io\Cpp1.cpp(67) : error C2065: ''string'' : undeclared identifierC:\Windows\Desktop\test\io\Cpp1.cpp(67) : error C2146: syntax error : missing '';'' before identifier ''filename''C:\Windows\Desktop\test\io\Cpp1.cpp(67) : error C2065: ''filename'' : undeclared identifierC:\Windows\Desktop\test\io\Cpp1.cpp(68) : error C2065: ''getline'' : undeclared identifierC:\Windows\Desktop\test\io\Cpp1.cpp(70) : error C2228: left of ''.c_str'' must have class/struct/union typeC:\Windows\Desktop\test\io\Cpp1.cpp(77) : error C2146: syntax error : missing '';'' before identifier ''name''C:\Windows\Desktop\test\io\Cpp1.cpp(77) : error C2065: ''name'' : undeclared identifierC:\Windows\Desktop\test\io\Cpp1.cpp(79) : error C2146: syntax error : missing '';'' before identifier ''line''C:\Windows\Desktop\test\io\Cpp1.cpp(79) : error C2065: ''line'' : undeclared identifierC:\Windows\Desktop\test\io\Cpp1.cpp(82) : error C2228: left of ''.find'' must have class/struct/union typeC:\Windows\Desktop\test\io\Cpp1.cpp(82) : error C2653: ''string'' : is not a class or namespace nameC:\Windows\Desktop\test\io\Cpp1.cpp(82) : error C2065: ''npos'' : undeclared identifierC:\Windows\Desktop\test\io\Cpp1.cpp(84) : error C2228: left of ''.find'' must have class/struct/union typeC:\Windows\Desktop\test\io\Cpp1.cpp(84) : error C2653: ''string'' : is not a class or namespace nameC:\Windows\Desktop\test\io\Cpp1.cpp(85) : error C2228: left of ''.find'' must have class/struct/union typeC:\Windows\Desktop\test\io\Cpp1.cpp(85) : error C2653: ''string'' : is not a class or namespace nameError executing cl.exe.Cpp1.exe - 17 error(s), 0 warning(s) 


I see you have the following commented out: , std::ios::nocreate

Could that have something to do with it?
Hey, bugs happen, deal with em. :)
I can't get std::ios::nocreate to compile. The reason? Microsoft doesn't define it in their headers - except for in ios.h. I think its an oversite in their STL headers.

My guess is that you are using .h header files which automatically include all classes into the global namespace by using namespace std.

Using .h STL headers is an old-style approach. The prefered way is to use the non-.h headers:

Example:

instead of:
  #include <fstream.h> #include <iostream.h> #include <string.h> [/source] use: [source]#include <fstream>#include <iostream> #include <string>   


Something you'll notice immediately is that you now have to qualify all the STL classes with std::. You can no longer just write:

string mystring;

You must write:

std::string mystring;

Now to make things easier you can write:

using namespace std;

Then you don't have to qualify the STL classes with std:: anymore.

I hope this clarifies things a bit.

Dire Wolf
direwolf@digitalfiends.com

P.S. The reason why std::ios::nocreate compiles for you is because you are using the .h header files. As I stated, Microsoft has seemed to have left out this type in their ios header file.

Edited by - Dire.Wolf on December 15, 2000 12:00:59 PM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Thanks alot Dire.Wolf!! It was because of the headers.

The only slight problem now is that after every user input, the user has to press [Enter] one additional time for the program to go into next operation. I.E. After inputing the name of the file and pressing [enter], the user has to press [enter] one additional time.

Sans that little problem, the program works great. Even better than before. It caught a little problem where there was an extra "!" in the text file. So all my previous calculations were wrong.

Thanks again
Hey, bugs happen, deal with em. :)

This topic is closed to new replies.

Advertisement