Ok I need to know how to set my class to read only the values after the = sign.

Here's my class so far.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Config
{
	int pos;
	string search;
public:
	char * confname;
	void configsearch ()
	{
		fstream filename (confname);
		while (!filename.eof())
		{
badsearch:
			getline (filename,search);
		pos = search.find("#");
			if (pos != -1)
			{
				goto badsearch;
			}
			cout << search;
			pos = search.find("=");
			if (pos != -1)
			{
				cout << "\n" << pos;
			}
			cout << "\n";
		}
	};
};

Recommended Answers

All 19 Replies

Look at the code here:

if (pos != -1)
{
	goto badsearch;
}

I think you meant:

if (pos == -1)
{
	goto badsearch;
}

Another thing is that string::search() returns string::npos if the search function does not find what it was told to find, and not -1, so the code should be:

if (pos == string::npos)
{
	goto badsearch;
}
cout << search;
pos = search.find("=");
if (pos != string::npos)
{
	cout << "\n" << pos;
}

And one last thing: you can avoid using goto in your 'if' statement by replacing it with the statement continue; - this will jump to the beginning of the while loop:

if (pos == string::npos)
{
	continue;
}
commented: Neat +4

^ nice answer.

except one thing needs a bit stronger emphasis.

never, ever use "GOTO" this is not BASIC programming. You will be tarred and feathered.


(there may be an exception to the absolute ban on using GOTO, but that's not available until you reach Level 70.)

>except one thing needs a bit stronger emphasis.
>never, ever use "GOTO" this is not BASIC programming.
Note to the OP: Avoid listening to programming advice that says you absolutely must or must not do something.

my question still hasn't been awnsered i want to read from an equals sign in my configuration file how do i do that

Avoid listening to programming advice that says you absolutely must or must not do something.

Good metaprogramming advice ;)
Never forget to declare C++ variables before...
To ignore or not to ignore? ;)
Is it an advice or a requirement? Where is the border?...

>Where is the border?...
Common sense, of course. Note that I said "avoid listening to" and not "never listen to". ;) The whole reason we have this "goto is teh EBIL!!!!!" crap is because a bunch of people were mindlessly following orders rather than thinking for themselves.

A. Whats wrong with goto
B. still dosn't awnser my origonal question

>A. Whats wrong with goto
Nothing. The problem is people using it in an undisciplined way such that the code becomes an unruly mess where control flow is difficult to follow.

>B. still dosn't awnser my origonal question
Be patient. When someone is ready to give you an answer, you'll get it. I notice you didn't even bother to thank unbeatable for taking the time to help you. Did you make the suggested changes?

unbeatable thanks for taking your time to awnser me however firstly I didn't realy need to change my != to == because it was supposed to go to the next line if it found a '#' because '#' is a comment sign and I changed goto to continue i don't know how it will affect execution time but realy execution time isn't realy an issue in this project

"goto is teh EBIL!!!!!" crap is because a bunch of people were mindlessly following orders rather than thinking for themselves.

goto is teh ebil, because beginners mindlessly use it as a crutch to compensate for poorly-planned designs, and this perpetuates on into their professional work, where they cleverly develop a tangled mess of unreadable code that they only understand

then they finally realize that it's really crappy design, so they rationalize it as "job security" (with a wink wink nudge nudge),which is all funny and collegial, until they finally quit and leave the mess to be sorted out by someone else.

GOTO should be discouraged with extreme prejudice in any sort of forum that is dedicated to teaching beginners the basics of C language.

if an experienced programmer chooses to use a sparingly few well-positioned GOTOS in, for instance, some error handling routines of hardware drivers, then that's their business.

but beginners should be taught how to complete their pedantic CSC 101 programs using traditional C-language loop structures.


.

I can certainly agree with well justified recommendations. But banning a feature just because it has the potential for abuse is hardly the way to educate anyone. And "discourage with extreme prejudice" does indeed amount to banning, if my experience is any indicator.

From what I've seen, the presence of goto in anyone's code always results in some know-it-all saying "never use goto!". I've even had people attempt to chastise me for using it both here and on other forums. You can imagine how well that goes. :twisted:

i have had to debug 100K lines of production code littered with GLOBALS and GOTO statements, after it had been hacked to death by every stripe of self-taught jackleg programmer.

i do not pretend to lecture experienced developers using a few sparing GOTOs. Such as yourself: you are obviously a very experienced programmer who understands the concept of writing maintainable code. Even more than me.

But I have a serious aversion to any beginner asking how to code their "hello world!" program and using GOTOs and GLOBALS to do it.

GOTOS and GLOBALS taught at the beginner's level inevitably leads to years of shiite programming that eventually has to either be continually maintained at high cost of time and labor, or just tossed out and rewritten by someone else.

i, personally, am tired of being the" someone else" who seems to keep inheriting this crap.

Another thing is that string::search() returns string::npos if the search function does not find what it was told to find, and not -1

This is an interesting point. I notice in the standard, clause 21.3 (Class template basic_string), paragraph 6, it does say that: static const size_type npos = -1; size_type is defined a little above that as: typedef typename Allocator::size_type size_type;

Please someone tell me how to locate and read from an '=' sign in a text file

is there any function that allows me to do this

You find the = sign just like you found the # sign. Like this: posEqual = search.find("="); If posEqual is equal to string::npos, there was no = sign. Maybe skip that line (and print an error msg).

If posEqual is equal to line.size() - 1 then the = sign is at the very end of the string, which is a special case. Basically the value is a null string, or maybe you consider this to be an error.

If posEqual < line.size() - 1 then

value = line.substr(posEqual + 1, string::npos);

You may wish to trim spaces from before and after the value thus retrieved.

commented: THANKS ! +1

There's nothing wrong with using the goto-statement, but overwhelming use of it makes your code very difficult to understand and extremely hard to debug, so my advice is: avoid it as much as you can and use it only when there's no other possibility left :) ...

Thank you ! SOLVED

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.