Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 16th, 2007, 5:41 AM   #31
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
Quote:
Originally Posted by rwm View Post
hey Pegasus,
well your program seems to have a little bug in it:
Yes i know, but as i said it was a beggining class. I have modified this way :
void StringTokenizer::findTokens()
{
	ifstream in(filename, ios::in | ios::binary);
	char c;
	int beg = 0, length = 0;

	while (!in.eof())
	{
		in.get(c);
		if (c != ' ' && c != '\r' && c != '\n')
			length++;
		else beg++;

		if (((c == ' ' || c == '\r') && length > 0) || in.eof())
		{
			Token t;

			t.firstpos = beg - 1;
			t.length = length;

			allTokens.push_back(t);
			beg += length;
			length = 0;
		}
		else if (c == '\r' || c == '\n')
		{
			beg += length;
			length = 0;
		}
	}

	in.close();
	numberOfTokens = allTokens.size();
}
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Feb 16th, 2007, 6:32 AM   #32
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I have no problem with people asking for help with simple problems. That's what the forum is for, for God's sake. Sometimes that help comes in the form of code. Sometimes it comes in the form of advice regarding HOW to solve problems. Your statement that a person doesn't have time to study the libraries is ludicrous. Far less time is taken in reviewing the methods available for opening and reading a file than is expended in writing and debugging bad code. That's why the documentation is there. That's why the tutorial section is packed with links. The C++ streams are based on classes. If they don't meet all your needs, you can flesh them out. Presenting bad code to a poster is NOT a favor, however much you may think that it is. You are, to quote your own signature, testing the depth of the water with both feet. You don't have time to do it with one foot -- you might get old.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 16th, 2007, 6:35 AM   #33
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
Well can u give me a hint were my code is bad so that i can improve it.

EDIT:
Quote:
Presenting bad code to a poster is NOT a favor...
I`m not doing it as a favor. I posted the code so that the others could see it and tell if it has pitfalls. Nobodys perfect.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Feb 16th, 2007, 8:34 AM   #34
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
This is probably more complicated than it needs to be for several reasons. I have presumed that a comment mark may be part of a token (# comment or #comment), I have presumed that a comment may appear anywhere on a line, I have presumed that arguments following the trigger word may span lines, and I have further presumed that a comment may intervene between arguments. That complicates things a little. As an exercise, you might want to derive a class from ifstream and overload the >> operator so that it strips comments automatically.

Pegasus, there's no need to whine when you receive some criticism. It's part of life and learning. Grow up.

The code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using std::string;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::vector;

struct argPair
{
	string first;
	string second;
};

string getToken (ifstream &theStream)
{
	string token;
	while (true)
	{
		theStream >> token;
		if (token.substr (0, 1) == "#") getline (theStream, token);
		else return token;
	}
}

int biteMe (string trouble)
{
	cerr << trouble << endl;
	return EOF;
}
int main() 
{
	string trigger = "trigger";
	string comment = "#";
	string token;
	argPair thePair;
	vector <argPair> theArgs;
	// Open file
	ifstream theStuff ("stuff.txt");
	ifstream &theStream = theStuff;
	if (!theStuff.is_open ()) return biteMe ("The file didn't open");
	while (true)
	{
		token = getToken (theStream);
		if (!theStuff.good ()) break;
		if (token == "trigger")
		{
			thePair.first = "failure";
			thePair.second = "failure";
			thePair.first = getToken (theStream);
			thePair.second = getToken (theStream);
			theArgs.push_back (thePair);
			if (!theStuff.good ()) break;
		}
	}
	if (theStuff.bad ()) return biteMe ("Serious input error");
	for (unsigned i = 0; i < theArgs.size (); ++i)
	{
		cout << theArgs [i].first << ", " << theArgs [i].second << endl;
	}

	return 0;
}
Quote:
Originally Posted by The input file
# comment
The quick brown trigger 1.1 2.1 fox
jumped trigger 1.2
2.2 over the lazy dog.
Now is the trigger #comment
1.3
# time for all good
2.3 men to read trigger
1.4 2.4 "Thinking in C++"
Quote:
Originally Posted by The output
1.1, 2.1
1.2, 2.2
1.3, 2.3
1.4, 2.4
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 19th, 2007, 6:56 AM   #35
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
Quote:
Originally Posted by DaWei View Post
Pegasus, there's no need to whine when you receive some criticism. It's part of life and learning. Grow up.
Im ok with criticism, but u have also to make your critics depending on my level of knowing c++, not on your level.

Youth never comes back, so i`ve got to enjoy it.

PS:Thanx for the code i will study more deeply the libraries.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Feb 19th, 2007, 8:01 AM   #36
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You're dealing in non-sequiturs. No one is telling you not to enjoy life or growing up. Someone is suggesting that if you post poor, unworking solutions, perhaps you should refrain from posting a solution.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 20th, 2007, 1:41 AM   #37
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
Hey,

Sorry im only replying now, had to do a bunch of work here at the studio... Now can get bac k to my code...

Hey, well even if the solution Pegasus gave had a little bug thats fine, at least he gave a hint that I learnt alot from... I really appreciated it...

Hey DaWei, thx for the example - ill take a look now... Looks interesting!

Sure it does make sense to understand fully how the libraries work, rather than going in and just hacking away... Next time ill do that...

BTW got the program all working... Feel my skills are getting much better

Thx again for all help!
rwm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing BLOBs in a database - problem jonyzz Other Programming Languages 8 Jan 31st, 2007 4:38 AM
Changing icons problem Pedja C# 8 Mar 25th, 2006 8:03 AM
Huge arrays in C (game-oriented problem theme) Rather Generic C 6 Mar 19th, 2006 1:09 AM
cgi/perl script + IE problem joyceshee Perl 2 Jan 24th, 2006 11:10 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC