Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ Programming ➜ General ➜ beginner question.

beginner question.

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by kenny31539   USA  (6 posts)  Bio
Date Wed 31 Mar 2010 10:38 PM (UTC)
Message
Ok, this might seem like a rather silly question. I'm not-so-new at coding, but I still don't know my tail-end from a hole in the ground. I've managed to track down and fix a few bugs since updating to ubuntu's Koala, but this one is simply giving me fits.

void Project_Handler::set_topwidth(void)
{
	Project_Data *project;
	string strbuf;
	if(!first_project)
	{
		topwidth = 0;
		return;
	}
	
	for(project = first_project; project; project = project->get_next())
	{
		strbuf = project->get_heading();
		if(topwidth < (int)strlen(strbuf.c_str()))
		{
			topwidth = strlen(strbuf.c_str());
		}
	}
	return;
}

Now, the error that it's throwing when I compile is strlen is undeclared in this scope. So, I tried the logical thing and tried declaring it...and it threw another error saying th at strlen can't be used as a descriptor or some such (I lost my password so waiting for the admin to show up)

Anyone familliar at all with this? OH! Idiota me...it's in swfotefuss.

Any help or advice (other than suicide or burning my fingertips off with a blowtorch) would be greatly appreciated. :)
Top

Posted by Keberus   (31 posts)  Bio
Date Reply #1 on Wed 31 Mar 2010 11:33 PM (UTC)
Message
You might want to check if strings.h is declared at the top of the file. It should look like

#include <string.h>

Top

Posted by kenny31539   USA  (6 posts)  Bio
Date Reply #2 on Wed 31 Mar 2010 11:35 PM (UTC)
Message
Ever...just really feel like an idiot? I took a look at the top of the file and...what do you know...the only header there was project.h Ok ok...so maybe I DO need to take a blowtorch to my fingers. Thanks a bundle! Soon as the admin comes around I'll let you know if it worked or not. :P
Top

Posted by Nick Cash   USA  (626 posts)  Bio
Date Reply #3 on Thu 01 Apr 2010 01:50 AM (UTC)
Message
Your way is a little roundabout. You could always change:


strlen(strbuf.c_str())


to:


strbuf.length()


:)

~Nick Cash
http://www.nick-cash.com
Top

Posted by kenny31539   USA  (6 posts)  Bio
Date Reply #4 on Thu 01 Apr 2010 02:54 AM (UTC)
Message
yeah, but:



	if(topwidth < (int)strlen(strbuf.c_str()))




is the line that's throwing the error. Tried changing it to


if(topwidth <strlen(strbuf.c_str) );

and putting


int strbuf]

earlier in the function, but then it gave me some other screwy error I can't remember at the moment.
Top

Posted by Nick Cash   USA  (626 posts)  Bio
Date Reply #5 on Thu 01 Apr 2010 03:51 AM (UTC)

Amended on Thu 01 Apr 2010 03:55 AM (UTC) by Nick Cash

Message
strbuf is already declared as a string, you wouldn't want to define it as an integer.


if(topwidth <strlen(strbuf.c_str) );


There are a couple things wrong with that statement. First, if strlen is undeclared you will still get the same error you were before.

If you redefined strbuf to be an int then it would throw an error about calling c_str.

The correct way to call the c_str function is
strbuf.c_str()

So you are missing parens and it would give you a syntax error.

The semi-colon would not trigger an error, but would almost certainly not do what you would expect. It signifies an empty block, thus nullifying the code the if statement was supposed to execute. Conditional statements should not have semi-colons after them unless it is intended, which is typically rare.

I see no reason why this wouldn't work:

void Project_Handler::set_topwidth(void)
{
	Project_Data *project;
	string strbuf;

	if(!first_project)
	{
		topwidth = 0;
		return;
	}
	
	for(project = first_project; project; project = project->get_next())
	{
		strbuf = project->get_heading();

		if(topwidth < strbuf.length())
		{
			topwidth = strbuf.length();
		}
	}
}


~Nick Cash
http://www.nick-cash.com
Top

Posted by kenny31539   USA  (6 posts)  Bio
Date Reply #6 on Sat 03 Apr 2010 06:13 PM (UTC)
Message
Turned out that adding strings.h to the header worked like a charm. :) Much appreciate for all the help! I'm sure that I'll come up with a million more questions and seeking advice in the very near future. :)
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


19,223 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.