[QUOTE=thelinx;33726460]Ah, yeah. Any clues on how to fix that? I thought the fflush would do the trick but I guess not.
Also, any tips on how to read from the file so I can have spaces in movie names?[/QUOTE]
[cpp]std::cin.ignore(std::numeric_limits<std::streamsize>::max());[/cpp]
Should 'flush' the buffer (execute after an input operator). There are some issues with it in special cases, you can find 'em somewhere online.
That said, you should generally never mix C++ and C streams.
You could have a comma separated list in the text-file instead, or support quotes.
Either way you'll want some way of escaping those though.
Perhaps a key-value system.
It's not really C++y to use exceptions in that way. Exceptions are for exceptional situations and the user typing something dumb is not an exceptional state IMO.
I'd check if the input is acceptable with a bunch of if-statements or whatever, since I just skimmed through and thus don't know where the exception actually happens I can't give you specifics.
And you generally don't pre-declare instances of your exceptions to throw, but just do throw MyException();.
It looks like you have a lot of code-duplication in your main-function. It's also a quite long single function.
See if you can break it up into individual bits to put into functions and if you can share the common code somehow.
If you can't think of how to do it I'll give it a closer look and give you more specific suggestions on that.
Also, why are you calling rand() twice after srand() without doing anything to its return value?
The usage of globals is generally discouraged. See if you can lock those functions and variables up in classes.
Again, if you can't think of how to do it yourself I'll take a closer look.
Be consistent with your coding-style. I've seen member variables being prefixed with _ in one place, and just plain camelCase in another.
I've seen a bunch of pointers where a reference seemed acceptable.
When dealing with pointers you should generally check for NULL/nullptr and it might be a good idea to use smart pointers.
[editline]15th December 2011[/editline]
[QUOTE=DeadKiller987;33733343]So I have this file [url]http://projecteuler.net/project/names.txt[/url]
And I'm trying to read in the names from it one by one, using this
[cpp]fscanf(file, "\"%s\",", buffer);[/cpp]
However instead of getting one name and stopping, it tries to put the entire file except the first " in the buffer. How do I get it to stop at the , ?[/QUOTE]
[cpp]fscanf(file, "\"%[^\",]", buffer);[/cpp]
It's called a scanset. The ^ inverts it.
Also, look out for buffer overflows ;)
First, ZeekyHBomb, massive kudos.
[QUOTE=ZeekyHBomb;33735835][cpp]std::cin.ignore(std::numeric_limits<std::streamsize>::max());[/cpp]
Should 'flush' the buffer (execute after an input operator). There are some issues with it in special cases, you can find 'em somewhere online.
That said, you should generally never mix C++ and C streams.
[/quote]
I tried that now, but I couldn't make it work...
[quote]
You could have a comma separated list in the text-file instead, or support quotes.
Either way you'll want some way of escaping those though.
Perhaps a key-value system.
[/quote]
At the moment I separate each value with a \t character. I just don't how to parse the file properly when I have a separating character.
[quote]
It's not really C++y to use exceptions in that way. Exceptions are for exceptional situations and the user typing something dumb is not an exceptional state IMO.
I'd check if the input is acceptable with a bunch of if-statements or whatever, since I just skimmed through and thus don't know where the exception actually happens I can't give you specifics.
And you generally don't pre-declare instances of your exceptions to throw, but just do throw MyException();.
[/quote]
I see. I won't be making any changes for this project, but I'll keep that in mind for other projects.
[quote]
It looks like you have a lot of code-duplication in your main-function. It's also a quite long single function.
See if you can break it up into individual bits to put into functions and if you can share the common code somehow.
If you can't think of how to do it I'll give it a closer look and give you more specific suggestions on that.
[/quote]
I totally agree. main() is a mess. I should have used some of that dictionary with functions thing that was talked about near the end of the November WAYWO.
[quote]
Also, why are you calling rand() twice after srand() without doing anything to its return value?
[/quote]
In Lua (which also uses the C RNG) you need to call (the equivalent of) rand() 1-3 times after seeding the RNG to get actual new random numbers.
[quote]
The usage of globals is generally discouraged. See if you can lock those functions and variables up in classes.
Again, if you can't think of how to do it yourself I'll take a closer look.
[/quote]
If you take a look at the commit log and go back a few weeks you can see I used to pass around the screenings vector, I just thought that was exhausting.
If you have an example that doesn't involve that kind of thing, please show.
[quote]
Be consistent with your coding-style. I've seen member variables being prefixed with _ in one place, and just plain camelCase in another.
[/quote]
I concur.
[quote]
I've seen a bunch of pointers where a reference seemed acceptable.
When dealing with pointers you should generally check for NULL/nullptr and it might be a good idea to use smart pointers.
[/QUOTE]
I don't actually get the difference between a pointer and a reference. Mind explaining?
I need a key-value store, something lightweight, and I hate SQL with a passion. The language I'm working in is go. I can probably bind something from C if I need to, but I'd prefer something that already provides a go API.
Does anyone have experience with bitcask/gocask? Is it any good?
Any other recommendations?
[QUOTE=thelinx;33737819]First, ZeekyHBomb, massive kudos.[/quote]
:3
[QUOTE=thelinx;33737819]I tried that now, but I couldn't make it work...[/quote]
I might clone it tomorrow and look if I can make it work. I'll get back to ya if I can.
[QUOTE=thelinx;33737819]At the moment I separate each value with a \t character. I just don't how to parse the file properly when I have a separating character.[/quote]
std::getline with the separator-argument set.
If you want to escape the separators it might even make sense to get the whole line and parse the string yourself (using the std::strings
find, first_first_of, substring and so on).
[QUOTE=thelinx;33737819]I totally agree. main() is a mess. I should have used some of that dictionary with functions thing that was talked about near the end of the November WAYWO.[/quote]
For a small and static amount the switch would probably do just fine. But each entry could be put in a function, that would make it look less clustered.
[QUOTE=thelinx;33737819]In Lua (which also uses the C RNG) you need to call (the equivalent of) rand() 1-3 times after seeding the RNG to get actual new random numbers.[/quote]
My experience with Lua is minuscule (though I just used it to adapt my [url=http://awesome.naquadah.org/]awesome window manager[/url]).
Anyway more to the point: I don't think I had this problem yet in C.
If you experience the problem then I'd make sense to keep it I guess.
In the new C++ standard you're gonna use std::random which is superior in many ways anyway
[QUOTE=thelinx;33737819]If you take a look at the commit log and go back a few weeks you can see I used to pass around the screenings vector, I just thought that was exhausting.
If you have an example that doesn't involve that kind of thing, please show.[/quote]
It would seem as you could create a Cinema-class having these vectors as a member.
You could also put the number of movies on top of the text-file so you can pre-allocate the memory for the vector using std::vector::reserve. Or perhaps at least pre-allocate a reasonable number like 16 or something.
For the orders it'd make more sense to use a std::list, which offers superior performance for removal in any place (O(1) (constant time) as opposed to O(n) (linear time proportional to the number of items in the vector)).
And you could look where you can use an std::list::iterator instead of the id.
A better idea might be a std::map or std::set.
Another interesting idea is to use the own address as id. This way you can be sure it's unique and you don't need the overhead of a random-function.
Another small thing: using the operator[] instead of at() does not do bounds-checking (well, it could, but the standard does not require it), so if you are sure your values cannot be out of bounds (since you check i < size) then you can just use operator[] instead.
[QUOTE=thelinx;33737819]I don't actually get the difference between a pointer and a reference. Mind explaining?[/QUOTE]
It's best to forget how they work internally and think of a pointer like an address pointing to the object and of a reference like a meta-object referencing the actual object.
Some differences in usability are that you cannot reassign a reference and that the object is not required to actually have an address (can be useful for some (I guess very minor) optimizations).
You'll also generally have a valid object when dealing with a reference, while a pointer can more easily point to NULL, be uninitialized or point at an echo of the past (an already deleted object).
I think there were some other reasons, but these are the two big ones (actually only the second).
You can also stop an array from decaying into a pointer by taking a reference to it. But you seldom need that. Also, std::array (C++11).
This is a question I find form time to time, so you could probably also bug your preferred internet searching machine to find a "pointer vs references" if you want to know more about it.
[editline]15th December 2011[/editline]
[QUOTE=ROBO_DONUT;33739755]I need a key-value store, something lightweight, and I hate SQL with a passion. The language I'm working in is go. I can probably bind something from C if I need to, but I'd prefer something that already provides a go API.
Does anyone have experience with bitcask/gocask? Is it any good?
Any other recommendations?[/QUOTE]
Could you not use the JSON-parser present in the standard library ([url=http://golang.org/pkg/json/]I think[/url])?
Or perhaps [url=http://code.google.com/p/leveldb-go/]LevelDB[/url], which came up with a quick Google-search. It seems to be a Google-creation and the Go-implementation is by the golang-guys (though you'd have to find out how far the implementation is by yourself, there's even a [url=http://code.google.com/p/leveldb-go/issues/detail?id=3]ticket for creating a documentation[/url]).
[QUOTE=ZeekyHBomb;33740059]Could you not use the JSON-parser present in the standard library ([url=http://golang.org/pkg/json/]I think[/url])?[/quote]
No, JSON is just a plaintext format for storing key-value pairs. It doesn't help with the full problem of disk storage.
[QUOTE=ZeekyHBomb;33740059]Or perhaps [url=http://code.google.com/p/leveldb-go/]LevelDB[/url], which came up with a quick Google-search. It seems to be a Google-creation and the Go-implementation is by the golang-guys (though you'd have to find out how far the implementation is by yourself, there's even a [url=http://code.google.com/p/leveldb-go/issues/detail?id=3]ticket for creating a documentation[/url]).[/QUOTE]
This is more on-target. I was hoping for a more informed opinion, though (don't take it the wrong way). I can google around for databases and keystores myself, but there's lots of them and no good objective comparison. At one point I even had a fit of NIH and was getting ready to write my own database before realizing just how much of my time that would eat up. I'm looking for something small, reliable, and well-tested which is preferably popular and likely to be supported well into the future.
Yeah, you didn't strike me as someone posting a question like this anyway.
But since you didn't really give any specifics I though I'd give it a shot. I have no experience with DBs though.
[QUOTE=ZeekyHBomb;33740059]
std::getline with the separator-argument set.
If you want to escape the separators it might even make sense to get the whole line and parse the string yourself (using the std::strings
find, first_first_of, substring and so on).
[/quote]
That helped a lot! Both my remaining issues are now fixed.
[quote]
For a small and static amount the switch would probably do just fine. But each entry could be put in a function, that would make it look less clustered.
[/quote]
Yeah, I'll see what I can do to make it less of a mess.
[quote]
My experience with Lua is minuscule (though I just used it to adapt my [url=http://awesome.naquadah.org/]awesome window manager[/url]).
Anyway more to the point: I don't think I had this problem yet in C.
If you experience the problem then I'd make sense to keep it I guess.
In the new C++ standard you're gonna use std::random which is superior in many ways anyway
[/quote]
I already started using some new C++11 stuff in this latest commit, so I'll take a look at std::random.
[quote]
It would seem as you could create a Cinema-class having these vectors as a member.
[/quote]
That would make sense. It would also look much cleaner. I'll put it on my todo-list.
[quote]
For the orders it'd make more sense to use a std::list, which offers superior performance for removal in any place (O(1) (constant time) as opposed to O(n) (linear time proportional to the number of items in the vector)).
And you could look where you can use an std::list::iterator instead of the id.
A better idea might be a std::map or std::set.
[/quote]
I'll look into the other data types of standard C++.
[quote]
Another interesting idea is to use the own address as id. This way you can be sure it's unique and you don't need the overhead of a random-function.
[/quote]
That's not the purpose of the id variable, it's there for the customers reference.
[quote]
Another small thing: using the operator[] instead of at() does not do bounds-checking (well, it could, but the standard does not require it), so if you are sure your values cannot be out of bounds (since you check i < size) then you can just use operator[] instead.
[/quote]
Indeed, that's a bit wasteful. Sorted in this latest commit.
Oh god, just concatenate (using the + operator (someone correct me if i'm wrong because I've literally never used C#)) each line onto one string instead of converting it to a char array and turning each character into a string, or whatever you're doing.
Can someone on 64bit Linux give me their CSFML 2.0 binaries? I just can't seem to get those to compile. (Then I'll probably need help compiling SFML.net 2.0 but I'm gonna take this one step at a time.)
[QUOTE]Describe how a computer is able to run applications that require more memory than is physically installed in a computer.[/QUOTE]
sees this on exam
what the actual fuck?
not sure if this is the right place. but seriously...
no they can't?
[QUOTE=Sprite;33748042]sees this on exam
what the actual fuck?
not sure if this is the right place. but seriously...
no they can't?[/QUOTE]
It sounds like a stupid, poorly worded question about virtual memory and paging.
Many OSes can use hard disk space as random access memory if actual RAM is not available.
[QUOTE=thelinx;33741826]That's not the purpose of the id variable, it's there for the customers reference.[/QUOTE]
You seem to just want a random number that is unique. Am I not seeing that correctly?
[cpp]cin.getline(&cmd, 2);[/cpp]
Writing past the bounds of a variable causes undefined behavior.
Try
[cpp]cmd = cin.peek();
cin.ignore(numeric_limits<streamsize>::max()/*, '\n' ?*/); //include <limits>[/cpp]
[QUOTE=Phyxius;33747540]Can someone on 64bit Linux give me their CSFML 2.0 binaries? I just can't seem to get those to compile. (Then I'll probably need help compiling SFML.net 2.0 but I'm gonna take this one step at a time.)[/QUOTE]
[url]https://github.com/LaurentGomila/SFML.Net/tree/master/extlibs/x64[/url]
[QUOTE=Sprite;33748042]sees this on exam
what the actual fuck?
not sure if this is the right place. but seriously...
no they can't?[/QUOTE]
[url]http://en.wikipedia.org/wiki/Paging[/url]
Is there an easier way to blend two hex colours than splitting the rgb values and getting the median of them all?
Could someone explain me what the hell is going on here?
[code]
public String str = "herp";
setString("derp");
Toast.makeText(getApplicationContext(), getString(), Toast.LENGTH_LONG).show(); // This will show a Toast with herp
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show(); // Yet this will show derp
public static String getString() {
LesroosterActivity la = new LesroosterActivity();
return la.str;
}
public static void setString(String s) {
LesroosterActivity la = new LesroosterActivity();
la.str = s;
}
[/code]
I need the functions to be a static, so i can acces them from a different class. Somehow setString is working fine, but getString won't return the new string?
You are creating a new instance of the object each time. You want to be working with the same object. Assuming you are using a singleton model (only one instance of the class is to be used at once);
[code]private static LesroosterActivity _la;
private static LesroosterActivity la
{
get
{
if (_la == null) _la = new LesroosterActivity();
return _la;
}
}
public static String getString()
{
return la.str;
}
public static void setString(String s)
{
la.str = s;
}[/code]
[QUOTE=thomasfn;33753534]You are creating a new instance of the object each time. You want to be working with the same object. Assuming you are using a singleton model (only one instance of the class is to be used at once);
[code]private static LesroosterActivity _la;
private static LesroosterActivity la
{
get
{
if (_la == null) _la = new LesroosterActivity();
return _la;
}
}
public static String getString()
{
return la.str;
}
public static void setString(String s)
{
la.str = s;
}[/code][/QUOTE]
Thanks for the help
[QUOTE=ZeekyHBomb;33749668]
[cpp]cin.getline(&cmd, 2);[/cpp]
Writing past the bounds of a variable causes undefined behavior.
Try
[cpp]cmd = cin.peek();
cin.ignore(numeric_limits<streamsize>::max()/*, '\n' ?*/); //include <limits>[/cpp][/QUOTE]
Fixed.
In Screening::Screening you can get rid of the goto by using a boolean.
You could also return nullptr in getScreening instead of throwing an exception to get rid of the weird flow with the try/catch - unless you rely on getting that exception thrown else where in your code.
And for getScreening you could just check for nullptr(/NULL) and use the pointer as an optional parameter.
[cpp]Screening *getScreening(unsigned location, unsigned *id = nullptr)
{
//...
if(id) *id = found;
//...
}[/cpp]
How can I make my SFML 2 shaders use the colour that is passed in by a draw call?
here's the shader so far:
[code]
uniform sampler2D tex;
void main()
{
gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);
}
[/code]
(displays the texture but without the right colours)
I think there has to be a multiplication in there, that's how it worked with HLSL shaders ( * VertexColor)
[editline]17th December 2011[/editline]
Never mind, it was * gl_Color
Can I make a XNA library that can be used from a Console application, and calls XNA draw/update methods internally?
I want it to be used like this: in my Console application, I can call AddPoint(new Point(20,20)); just once and an XNA window would keep the point there.
Would I have to use the library's classes in a new Thread?
[editline]17th December 2011[/editline]
Oh! Would I just not use the XNA's default Program class?
[QUOTE=ZeekyHBomb;33757902]
And for getScreening you could just check for nullptr(/NULL) and use the pointer as an optional parameter.
[cpp]Screening *getScreening(unsigned location, unsigned *id = nullptr)
{
//...
if(id) *id = found;
//...
}[/cpp][/QUOTE]
Ah, thanks. Fixed.
C#.
When you literally set variable one to variable two, they reference the same object in memory.
[csharp]
myDerp1 = new Derp(); //derp object 1
myDerp2 = new Derp(); //derp object 2
myDerp1 = myDerp2; //now both of them reference derp object 2
[/csharp]
Does this happen with structs?
Does anyone know a string pattern that finds text inside parentheses. Aka "Hi ( This is text )" would come out as "This is text", etc.
In VS2010, is there a way or addon to show #define macro functions with intellisense?
(I'm a sucker for autocomplete)
[QUOTE=ief014;33760935]In VS2010, is there a way or addon to show #define macro functions with intellisense?
(I'm a sucker for autocomplete)[/QUOTE]
C++ IDE functions are very broken in VS2010. It's sad. 2011 is supposed to be better, but I'll have to see it to believe it.
[QUOTE=ROBO_DONUT;33740436]No, JSON is just a plaintext format for storing key-value pairs. It doesn't help with the full problem of disk storage.
This is more on-target. I was hoping for a more informed opinion, though (don't take it the wrong way). I can google around for databases and keystores myself, but there's lots of them and no good objective comparison. At one point I even had a fit of NIH and was getting ready to write my own database before realizing just how much of my time that would eat up. I'm looking for something small, reliable, and well-tested which is preferably popular and likely to be supported well into the future.[/QUOTE]
I've heard [i]really[/i] good things about [url=http://redis.io/clients]redis[/url]. I haven't used it myself, but it shows up enough in Y Combinator funded startups that it's got to be doing something right :)
[QUOTE=Chandler;33761969]I've heard [i]really[/i] good things about [url=http://redis.io/clients]redis[/url]. I haven't used it myself, but it shows up enough in Y Combinator funded startups that it's got to be doing something right :)[/QUOTE]
Redis sounds neat. I've started with MongoDB simply because it had up-to-date bindings and good documentation (although it's beastly and probably overkill for my application), but I may play around with redis tomorrow and see how it compares.
So I just went to open my XNA project, and appparently the content folder imploded upon itsself. It's saying that it is corrupted or unavailable, even though I can compile and run the game without any issues. Additionally, it isn't letting me create a new one. Anyone know how to fix this?
[img]http://dl.dropbox.com/u/12024286/wat.PNG[/img]
Sorry, you need to Log In to post a reply to this thread.