Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
That Turkey Story
Mar 30, 2003

Ugg boots posted:

We talked a couple of days ago, you mentioned that you lost your job and seemed pretty bummed about it. Nothing a week of video games won't fix though, right? :)

Edit: Or you could spend your time in COBOL and make some origami versions of peoples' avatars!

Whoever you are you're gay as hell!

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
If it helps, they're (probably) going to extend the lower-price early registration fee for Boostcon until a week before it starts. :)

Contero
Mar 28, 2004

What is the simple, clean way to read in either a float or int from stdin in C?

>5
oh an int! (5)
>5.3
a float! (5.3)
>blah
not a float or int!


code:
void readit(void)
{
   int i;
   float f;

   //...
   if (...)
      printf("oh an int! (%d)\n", i);
   else if (...)
      printf("a float! (%f)\n", f);
   else
      printf("not a float or int!\n");
}
Because the only solutions I can think of are pretty gross/hackish.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
You're going to have to be a lot more specific. Is 1.23E2 a float or an int?

Contero
Mar 28, 2004

#.#[E#] / nan / inf is a float
# is an int
anything else falls through.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge
Probably an overcomplicated and long-winded method but...

Read a string. Check for a decimal point, E etc.

Use boost::lexical_cast to get the string into the required format. (int or float)

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Zakalwe posted:

Use boost::lexical_cast to get the string into the required format. (int or float)

Contero posted:

in C?

:colbert:


(P.S. http://codepad.org/l9GkajTc)

Null Pointer
May 20, 2004

Oh no!

Zakalwe posted:

Probably an overcomplicated and long-winded method but...

Avenging Dentist posted:

:colbert:

Or meet somewhere in the middle: search the string for a decimal or an E, and then use sscanf's return value to determine valid input. :colbert:

Vanadium
Jan 8, 2005

I tried to come up with a way to use scanf's return value for this but I am seriously at a loss there. And I did not even think of strtol. :(

Null Pointer
May 20, 2004

Oh no!

Vanadium posted:

I tried to come up with a way to use scanf's return value for this but I am seriously at a loss there. And I did not even think of strtol. :(

scanf requires you to specify the input's type in the format string. There is no way to use scanf to detect the type of the input, only whether or not an input is matched.

Vanadium
Jan 8, 2005

Yeah, the plan was kind of to try reading it as both integer and floating-point type, but since scanf only returns the amount of items matched and not the amount of bytes consumed, that was not of any use at all. welp.

Contero
Mar 28, 2004

Avenging Dentist posted:

:colbert:


(P.S. http://codepad.org/l9GkajTc)

strtol we meet again!

(thanks)

Lexical Unit
Sep 16, 2003

I had pretty much the exact same code AD posted but whenever I tried to print out the value of the double it would come out as garbage. Finally, including stdlib.h fixed the issue. I'm not very versed in straight C, what's the reason for this?

Fake edit: I should have turned on all warnings.

gcc posted:

warning: implicit declaration of function ‘strtol’
warning: implicit declaration of function ‘strtod’
How is this only a warning, and not an error?

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Lexical Unit posted:

How is this only a warning, and not an error?

Builtins are the excuse iirc

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Lexical Unit posted:

I had pretty much the exact same code AD posted but whenever I tried to print out the value of the double it would come out as garbage. Finally, including stdlib.h fixed the issue. I'm not very versed in straight C, what's the reason for this?

C specifies what happens when you call a function with no declaration; essentially, it's as if you were calling a function declared int (*)(...). Now, first off, doubles generally get returned in totally different registers from ints, but even if they didn't, the logical operation here as far as C is concerned is to take an int value from somewhere (in this case, probably trash) and transform it to a double.

Lexical Unit posted:

Fake edit: I should have turned on all warnings.
How is this only a warning, and not an error?

Foolishness.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

rjmccall posted:

Foolishness.
Yes, so foolish that it was made an error in C99.

Contero
Mar 28, 2004

Mustach posted:

Yes, so foolish that it was made an error in C99.
$ gcc -std=c99 -Wall -o bla test.c
test.c: In function âmainâ:
test.c:17: warning: implicit declaration of function âstrtolâ
test.c:19: warning: implicit declaration of function âstrtodâ

code:
#include <errno.h>
#include <stdio.h>
//#include <stdlib.h>
#include <string.h>

int main ()
{
   int i;
   double f;
   char buffer[256];
   char *end;
   char *out;

   while(EOF != fscanf(stdin, " %s", buffer)) {
      end = buffer + strlen(buffer);
      errno = 0;
      if ( ((i = strtol(buffer, &out, 10)) || errno == 0) && out == end)
         printf("long: %d\n", i);
      else if ( ((f = strtod(buffer, &out)) || errno == 0) && out == end)
         printf("double: %lf\n", f);
      else
         printf("whatever: %s\n", buffer);
   }
   
   return 0;
}
:confused:

Vanadium
Jan 8, 2005

$ gcc -x c++ -o test{,.c}
test.c: In function ‘int main()’:
test.c:17: error: ‘strtol’ was not declared in this scope
test.c:19: error: ‘strtod’ was not declared in this scope


:smug:

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
gcc is not a stardards-conforming compiler by any stretch of the imagination, Contrero

Also try it with -std=c99 -pedantic

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

Contero posted:

:confused:

http://gcc.gnu.org/c99status.html posted:

remove implicit function declaration - Done
Don't ask me. Anyhow, it says so in paragraph 5 of the standard's Foreward.

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->
I'm working on UVa Problem 103 right now, and I need a little help. I've got everything working except the function which finds out which boxes fit into each other. The problem is with the solution I came up with, the UVa Judge tells me "Wrong Answer." I think part of my problem is that I'm just coming up with unnecessarily complex solutions.

This is an outline of the logic I came up with. This function is being sent a box which is sorted according to the sum of their elements:


Click here for the full 1260x297 image.


Could someone pitch me a simpler idea, or tell me what's wrong with mine?

Cosmopolitan fucked around with this message at 01:00 on Apr 7, 2009

shrughes
Oct 11, 2008

(call/cc call/cc)

Anunnaki posted:

Could someone pitch me a simpler idea, or tell me what's wrong with mine?

That you're trying to express it with a flowchart. Why don't you explain your idea with something more readable, like actual code?

Edit: Heck, even pseudocode!

floWenoL
Oct 23, 2002

Anunnaki posted:

Could someone pitch me a simpler idea, or tell me what's wrong with mine?

Wow flowcharts really *are* useless for anything except the most trivial of algorithms. If you rewrite it as pseudocode I'm sure it'll be easier to see if you did anything wrong.

Contero
Mar 28, 2004

Mustach posted:

Don't ask me. Anyhow, it says so in paragraph 5 of the standard's Foreward.

I was sincerely confused, not being a jackass. It's hard to tell the difference in CoC I guess.

Mr.Radar
Nov 5, 2005

You guys aren't going to believe this, but that guy is our games teacher.
Does anyone have any suggestions for good books or tutorials on test driven development and unit testing with C++? I'm starting a new project and I'd like to give TDD a try. Google's top results look pretty good, but some of that material is out of date and isn't very in-depth and there's a lot of .Net results in there.

Mr.Radar fucked around with this message at 03:27 on Apr 7, 2009

narfanator
Dec 16, 2008
I found the Boost TDD tutorial thing really helpful in general, you might take a look.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

Contero posted:

I was sincerely confused, not being a jackass. It's hard to tell the difference in CoC I guess.
Oh no, I didn't think you were a jackass. Sorry if I seemed that way.

newsomnuke
Feb 25, 2007

code:
class Foo
{
	friend std::ostream& operator << (std::ostream& os, const Foo& a_var);

	int m_type;
	union
	{
		int m_int;
		float m_float;
	};
	
	// Member functions
	// ...
};
Why does sizeof(Foo) give me 12? Is there some odd padding going on, or is there something in the member functions which might be taking up space?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

ultra-inquisitor posted:

Why does sizeof(Foo) give me 12? Is there some odd padding going on, or is there something in the member functions which might be taking up space?

If any of them are virtual, or if you have a virtual base class (which I mention only for completion, since you obviously don't), yes. Other than that, what platform are you compiling on?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

ultra-inquisitor posted:

Why does sizeof(Foo) give me 12? Is there some odd padding going on, or is there something in the member functions which might be taking up space?

Virtual members take up space because a function pointer needs to be passed around. I'm not aware of any architectures that need floats aligned on 8-byte boundaries instead of 4-, but I wouldn't be shocked if there were some.

newsomnuke
Feb 25, 2007

ShoulderDaemon posted:

Virtual members take up space because a function pointer needs to be passed around.
This'll be it. Isn't it a pointer to the vtable, though?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

ultra-inquisitor posted:

This'll be it. Isn't it a pointer to the vtable, though?

As far as I know, compilers are free to implement vtables as either separate structures or inline with objects - if there's only a single virtual method, storing it inline saves an indirection, for example.

Standish
May 21, 2001

ultra-inquisitor posted:

This'll be it. Isn't it a pointer to the vtable, though?
Are you on 64 bits?

newsomnuke
Feb 25, 2007

Standish posted:

Are you on 64 bits?
Nah, bog-standard win32.

schnarf
Jun 1, 2002
I WIN.

ultra-inquisitor posted:

code:
class Foo
{
	friend std::ostream& operator << (std::ostream& os, const Foo& a_var);

	int m_type;
	union
	{
		int m_int;
		float m_float;
	};
	
	// Member functions
	// ...
};
Why does sizeof(Foo) give me 12? Is there some odd padding going on, or is there something in the member functions which might be taking up space?
I'm betting the union isn't 4 bytes as you'd expect. The union size is basically up to the compiler. Somebody else can quote the spec better than I can, but the compiler is probably padding the union for whatever reason.

digibawb
Dec 15, 2004
got moo?

schnarf posted:

I'm betting the union isn't 4 bytes as you'd expect.

How much you willing to bet?

It will be the vtable, as has already been covered.

huge sesh
Jun 9, 2008

Is there any way to get the compiler to pretend that two enum types are one and the same? The situation I want is something like this:
code:
===base.h===
class base
{
  enum {
    a,
    b,
    c,
    base_last,
  } Activity;
};

===derived.h===
class derived : public base
{
  enum {
    d = base_last,
    e,
    f,
  };
};
Can I trick the compiler in some way into accepting d, e, and f as type Activity?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
No, that would be awful and violate the basic principle of enums (that they are a specific list of possible values).

huge sesh
Jun 9, 2008

Avenging Dentist posted:

No, that would be awful and violate the basic principle of enums (that they are a specific list of possible values).

It's not my choice of how to do things, it's the way the Source engine works.

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

huge sesh posted:

It's not my choice of how to do things, it's the way the Source engine works.

I doubt that the Source engine dictates that you use a language feature that does not exist.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply