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
Fecotourist
Nov 1, 2008
Use boost.format.

str(boost::format("%5.3f") % x)

prints the value of x formatted into a std::string as you would expect from experience with printf. It's type-safe, and there's no need to worry about overrunning a buffer.

Adbot
ADBOT LOVES YOU

Elswyyr
Mar 4, 2009
Is there any online guides to learn c++ that you can recommend? I apologize if this question already has been asked.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

If you're serious about wanting to learn the language, you should buy a book instead of relying on lovely and outdated web tutorials.

See the first post for recommendations.

narfanator
Dec 16, 2008
Is there any way to do have two functions identical, except for the order of default-valued parameters of different types?
code:
void foo(int x, double y = 1, float z = 2)
{
   return x*y*z;
}
void bar(int x, float z = 2, double y = 1);
{
   return x*y*z;
}

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
I am assuming you mean for both of the functions to be named foo because there's nothing wrong with your code now (except that you can't return a value from a void function, and that your second definition has an extra semicolon).

In that case, foo(1) is ambiguous. There are lots of ways to resolve this, but the real answer is that you probably shouldn't be using default parameters like that. Any more than one default per function is just annoying to use.

Boost.Parameter probably resolves the core issue you have, at the expense of some hilarious templates.

Contero
Mar 28, 2004

I'd like to have a vector class that has 3 floats. I'd like to be able to access them either as an array: myvec.val[0] or as individual members: myvec.x. Is there a way to do this with unions or something?

chips
Dec 25, 2004
Mein Führer! I can walk!
You could always do:

code:
class Vec3
{
  double x, y, z;

  double operator[](int i)
  {
    return (&x)[i];
  }
};
Probably considered bad form.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge
code:

union
{
  struct
  {
    float x, y, z;
  };

  float val[3];
}

Anonymous union with anon struct. Not standard but supported by pretty much any compiler you'll use.



edit: VV Suck it AD :P


No. you just sucked off the trailing ; :P

Zakalwe fucked around with this message at 00:00 on Apr 14, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
^^ At least mine compiles. :colbert:

Contero posted:

I'd like to have a vector class that has 3 floats. I'd like to be able to access them either as an array: myvec.val[0] or as individual members: myvec.x. Is there a way to do this with unions or something?

Yes, through non-standard extensions supported in gcc and MSVC (stolen verbatim from one of my projects):

code:
struct vec2d
{
	union
	{
		struct
		{
			float x;
			float y;
		};
		float coord[2];
	};
};

Contero
Mar 28, 2004

Sweet. Thanks guys.

narfanator
Dec 16, 2008

Avenging Dentist posted:

I am assuming you mean for both of the functions to be named foo because there's nothing wrong with your code now (except that you can't return a value from a void function, and that your second definition has an extra semicolon).

In that case, foo(1) is ambiguous. There are lots of ways to resolve this, but the real answer is that you probably shouldn't be using default parameters like that. Any more than one default per function is just annoying to use.

Boost.Parameter probably resolves the core issue you have, at the expense of some hilarious templates.

Yes, yes, and those aren't hilarious, they're kinda hideous. (Not as bad as the same for policy class templates!)

It technically resolves the core issue, but not nicely enough to warrant use. Thanks for the info, tho.

Great Britain
Dec 17, 2005

& Northern Ireland
I have an SDL app with loads of bitmaps that I want to to include in the .exe and I'm using Dev-C++. Does anyone know where I can find a simple guide to writing resource files?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Please don't use Dev-C++, it is an absolutely terrible IDE. If you're hell-bent on not using Visual Studio, use windres to compile the .rc files.

Madoushi
May 9, 2003

Some days, you just get up on the wrong side of the bed...
Can anyone tell me what the problem with this code is?

code:
#include <iostream>
#include <string>
using namespace std;

class myException{

	public:
	myException(string exType){cout << "ERROR: "<< exType;}
};

int main(){
throw myException("TEST");
return 0;
}
I can compile and run it, but after it prints the message, I get this error:

9 [sig] extest 236 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
Segmentation fault (core dumped)

awesmoe
Nov 30, 2005

Pillbug

Madoushi posted:

Can anyone tell me what the problem with this code is?

code:
#include <iostream>
#include <string>
using namespace std;

class myException{

	public:
	myException(string exType){cout << "ERROR: "<< exType;}
};

int main(){
throw myException("TEST");
return 0;
}
I can compile and run it, but after it prints the message, I get this error:

9 [sig] extest 236 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
Segmentation fault (core dumped)

What do you expect it to do?

edit: what does the backtrace from the core file tell you?

agscala
Jul 12, 2008

I'm trying to make a simple roguelike on windows using Visual Studio 2008. I want to use pdcurses for console manipulation. I have the .h files, the .lib, and the .dll. I added the .h files and the .lib to the project preferences, but how to I get it to use the .dll?

currently all I have is:
code:
#include "stdafx.h"
#include <curses.h>

int main(int argc, char *argv[])
{
	initscr();
	return 0;
}
and the error I get is:
code:
1>curses.obj : error LNK2019: unresolved external symbol _initscr referenced in function _main
1>C:\Users\Andrew Scala\Documents\Visual Studio 2008\Projects\curses\Debug\curses.exe : fatal error LNK1120: 1 unresolved externals

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.
If you're getting a link error at build time, it's not because the DLL can't be found, it's because you're not linking with the lib. Did you add the lib's path to the "additional places to look for libraries"* setting in Project Preferences, in addition to adding the name of the lib to "additional library dependencies"?

* Something like that, I can never remember

agscala
Jul 12, 2008

I believe so:


Click here for the full 972x789 image.

csammis
Aug 26, 2003

Mental Institution
Did you explicitly add the library to the Linker -> Input properties?

Kamikaze!
Jul 10, 2001
On probation for postcount++
Go to linker->input and add the .lib to Additional Dependencies.

agscala
Jul 12, 2008

I've been playing with this for a good hour and a half straight and I'm getting no luck. I've put all of the libraries straight into the visual studio 9 folder in the appropriate places even. Maybe I'll just abandon using this and find some other way to control the console.

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.
All you should need are curses.lib in Configuration Properties > C/C++ > Linker > Input > Additional Dependencies, and the path in Linker > General > Additional Library Directories. I can think of three things that are wrong, in order of rapidly decreasing likelihood: 1) The path needs quotes around it because it has spaces, 2) initscr() isn't a curses function and you didn't define it yourself, 3) curses.lib doesn't reference initscr().

agscala
Jul 12, 2008

Bah, I'll just let it go. I recently found libtcod which does everything that I want and more, and I got it running with no problems. :shobon:

Thanks for the help though.

huge sesh
Jun 9, 2008

I think I've been coding too long and I'm going crazy but for some reason this isn't working
code:
queue<int> q;
int x = 1;
q.push(x); //std::bad_alloc
Depending on chance I either get:

First-chance exception at 0x777f0ab0 in mapmaker.exe: 0xC0000005: Access violation reading location 0x00d43c42.
First-chance exception at 0x75b7f35f in mapmaker.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0017dfd0..
Unhandled exception at 0x75b7f35f in mapmaker.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0017dfd0..

or a message about heap corruption. It's almost exactly the same as the cplusplus.com example std::queue usage and it's making me feel like an idiot that I can't use a drat stl container right.

POKEMAN SAM
Jul 8, 2004

huge sesh posted:

I think I've been coding too long and I'm going crazy but for some reason this isn't working
code:
queue<int> q;
int x = 1;
q.push(x); //std::bad_alloc
Depending on chance I either get:

First-chance exception at 0x777f0ab0 in mapmaker.exe: 0xC0000005: Access violation reading location 0x00d43c42.
First-chance exception at 0x75b7f35f in mapmaker.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0017dfd0..
Unhandled exception at 0x75b7f35f in mapmaker.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0017dfd0..

or a message about heap corruption. It's almost exactly the same as the cplusplus.com example std::queue usage and it's making me feel like an idiot that I can't use a drat stl container right.

Works fine for me:
http://codepad.org/9JxpwM65

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.
Is that all your program is doing? Or are you allocating lots of memory elsewhere? Did you disable your page file or something wacky like that? Do you get the same error when you try to use other containers, like vector?

Mach Won
Jun 17, 2004

Is UNC playing? My post is either whining about (1) ref favoritism, (2) unfair recruiting/player caliber, (3) opposing team sucking or (4) the inevitable UNC win. The day you see me giving UNC credit for anything is the day someone hacked my account.

Roy era: 1-16 :qq: RIVALS!!!!!
I use Code::Blocks just because it's pretty straight forward. I found Visual Studio to be overly complicated.

Vinterstum
Jul 30, 2003

Mach Won posted:

I use Code::Blocks just because it's pretty straight forward. I found Visual Studio to be overly complicated.

The code you posted is fine, there'll be something that's corrupting the heap preceding it.

Plank Walker
Aug 11, 2005
I'm not all that familiar with C, but while working on a grad-level assignment came across something weird. The assignment was filling in a couple dummied out functions in a c program and we were given a bunch of pre-written functions to call. The source code was all written by the professor in a format I had never seen:

code:
void someFunction(arg1, arg2, arg3)
type1 arg1;
type2 arg2;
type3 arg3;
{
// Function here
}
Every function call worked fine, except for one:
code:
void somefunction(anInt, aFloat)
int anInt;
float aFloat;
{
    someGlobalFloat += aFloat;
}
No matter how I passed the float value to the function, aFloat never got set to a value. Debugging showed that immediately before my call to somefunction it was undefined, and immediately after it was a double with a garbage value like 3.90898089097e-293. I "fixed" the problem by rewriting the function definition as
code:
void somefunction (int anInt, float aFloat)
and it worked just fine, but I'd really like to know what "feature" of C made it behave the way it did when every other function written in the author's format worked normally.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Plank Walker posted:

I'm not all that familiar with C, but while working on a grad-level assignment came across something weird. The assignment was filling in a couple dummied out functions in a c program and we were given a bunch of pre-written functions to call. The source code was all written by the professor in a format I had never seen:

Your professor is using traditional C. If I remember correctly, float parameters are automatically converted to doubles in some cases. Is the procedure defined before any code that calls it?

Plank Walker
Aug 11, 2005

Janin posted:

Your professor is using traditional C. If I remember correctly, float parameters are automatically converted to doubles in some cases. Is the procedure defined before any code that calls it?

The function prototype is written as somefunction(int, float);. I've sent it the actual parameter as
code:
20
20.0
20.0f
(float) 20.0
tried declaring a const float and sending that, and basically every format I could think of. The value that the function actually used as its parameter differed every time, making me think it allocated room for a double and half of it was overwritten with the float I was passing. Casting float on the value I was given always yielded 0.0.

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.

Janin posted:

Your professor is using traditional C.
"K&R" C is not a tradition. I would be suspicious of the quality of any compiler that quietly accepts it, and extremely suspicious of the quality of any man that willingly writes in it.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Plank Walker posted:

The function prototype is written as somefunction(int, float);. I've sent it the actual parameter as
[...]
tried declaring a const float and sending that, and basically every format I could think of. The value that the function actually used as its parameter differed every time, making me think it allocated room for a double and half of it was overwritten with the float I was passing. Casting float on the value I was given always yielded 0.0.
You could always just ask your professor -- it works fine on my system, so it might be a compiler bug.

Mustach posted:

"K&R" C is not a tradition. I would be suspicious of the quality of any compiler that quietly accepts it, and extremely suspicious of the quality of any man that willingly writes in it.
waaaah compilers aren't modified to gratuitously reject perfectly valid code waaah

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.
Yep I'm cryin' my eyes out here.

Glasgerion
Jul 25, 2007
Strike on strike on
Does anyone know of any function or other way to have a program resize the terminal window it's running in?

POKEMAN SAM
Jul 8, 2004

Glasgerion posted:

Does anyone know of any function or other way to have a program resize the terminal window it's running in?

That doesn't sound very portable to you, now does it?

Glasgerion
Jul 25, 2007
Strike on strike on

Ugg boots posted:

That doesn't sound very portable to you, now does it?

Are you saying there's no good way to do it?

huge sesh
Jun 9, 2008

Glasgerion posted:

Does anyone know of any function or other way to have a program resize the terminal window it's running in?

libncurses?

huge sesh
Jun 9, 2008

Mustach posted:

Is that all your program is doing? Or are you allocating lots of memory elsewhere? Did you disable your page file or something wacky like that? Do you get the same error when you try to use other containers, like vector?

It's certainly not all the program's doing but it's really not that complicated. I don't allocate that much memory, page file is normal. std::vector works fine elsewhere in the program. I guess I'll look around for possible heap corruption. Just seems weird that it would happen every time.

Adbot
ADBOT LOVES YOU

Lexical Unit
Sep 16, 2003

Glasgerion posted:

Does anyone know of any function or other way to have a program resize the terminal window it's running in?
How you'd resize the window depends on a million factors. What OS are you using? Assuming *NIX, what Window Manager are you using? It may also depend on what Terminal application you want to resize. Will you interact with other applications the user may have running? It also doesn't have anything to do with C/C++.

Just for grins, let's assume you're using Linux with the Metacity WM. You might be able to pull it off like this:
code:
// Somehow find the X Window ID of your terminal window, this is left as an exercise to the reader.
Window window = magic (); // the X Window ID of your terminal window
int new_widget, new_height;
XWindowChanges changes;
changes.width = new_width;
changes.height = new_height;
XConfigueWindow (display, window, CWidth | CHeight, &changes);
XFlush (display);
Of course all that code presupposes that you've opened a X Display correctly and have code in place to handle spurious BadMatch, BadValue, or BadWindow errors.

Like huge sesh said, you might also be able to leverage ncurses's resizeterm() function. It all depends on what you mean by "resize the terminal window" and what environment you're working with.

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