|
Your sort algorithm is terrible and broken, your fit function is unreadable, terrible, and broken, and you really need to learn about booleans and structs. But your code is crashing because the numbers stored in num are in the range 1 to k, whereas the valid (first) indexes into box are in the range 0 to k-1.
|
# ? Jan 23, 2009 12:28 |
|
|
# ? Jun 8, 2024 09:13 |
Ugg boots posted:Are you sure those values are going to fit into an int?
|
|
# ? Jan 23, 2009 15:19 |
|
Im writing a code for a program that makes you guess a number from 1 to 100. The only problem is that when I run it, and enter my number, it shows me everything, it shows me that my number was too high, too low, what close numbers would be, and finally the answer. All I would like it to do is when you enter your number, it tell you if you were too high, or too low, or very close, and then tell you what the number is. Here is my code so far.code:
|
# ? Jan 23, 2009 18:31 |
|
Vandorin posted:Im writing a code for a program that makes you guess a number from 1 to 100. The only problem is that when I run it, and enter my number, it shows me everything, it shows me that my number was too high, too low, what close numbers would be, and finally the answer. All I would like it to do is when you enter your number, it tell you if you were too high, or too low, or very close, and then tell you what the number is. Here is my code so far. Two things: Take the statement terminators (semi-colons) off of the end of your if statements. Right now those are empty conditions. code:
code:
code:
Wow I didn't read that code very closely the first time through. There are also some serious logical flaws in your program, but I'm not going to re-write the whole thing for you. You need to read a book or at least a simple web tutorial, I'll bet some good ones are recommended in the OP of this thread. borkzilla fucked around with this message at 18:46 on Jan 23, 2009 |
# ? Jan 23, 2009 18:38 |
|
Ok, I got it, the double "==" also solved it showing the "Very Close" part, but and now I only get one of the phrases, which is great . Thanks! edit: Ok, when I try to run it, all it says is "0" and then "Was too low" and no matter what number, it keeps putting the 0 in instead of what I type in. Vandorin fucked around with this message at 18:48 on Jan 23, 2009 |
# ? Jan 23, 2009 18:45 |
|
borkzilla posted:the result of an assignment matches to true (at least in C).
|
# ? Jan 23, 2009 18:47 |
|
Also note that '=!' should be '!='; the former will assign the logical negation of the operand on the right to the operand on the left, whereas the latter will test the left and right operands for inequality.
|
# ? Jan 23, 2009 19:21 |
|
Ok, I found out how if statements worked, and saw that my code was horrible, here is my attempt to make it better.code:
|
# ? Jan 23, 2009 19:27 |
|
Consider the logic you're using for the 'very close' section - what happens if you guess a number that's, say, 4 too high? look up abs()
|
# ? Jan 23, 2009 19:41 |
|
Also reconsider your use of 'else'; the way you are using it doesn't do what you want it to do.
|
# ? Jan 23, 2009 19:48 |
|
Otto Skorzeny posted:Consider the logic you're using for the 'very close' section - what happens if you guess a number that's, say, 4 too high? I see what you mean, but I don't understand abs() isn't that just absolute value?
|
# ? Jan 23, 2009 20:36 |
|
Vandorin posted:I don't understand abs() isn't that just absolute value? code:
|
# ? Jan 23, 2009 21:06 |
|
TheSleeper posted:
Oh whoa, I didn't even think of that, my teacher was telling me to do "range checking" and since he hasn't gone over that part yet, I was wondering how he expected us to do this.
|
# ? Jan 23, 2009 23:06 |
|
Here's a question that illustrates my ignorance of C. If I write a C program, but at some point later decide to include a function that is specifically C++, how much will that affect the performance of my program? You can assume gcc as the compiler.
|
# ? Jan 23, 2009 23:08 |
|
functional posted:Here's a question that illustrates my ignorance of C. Apart from the C++ stuff that function needs to initialise and everything, there should not be a performance overhead. You can write a C-linkage wrapper function of the C++ function and call that from a translation unit that is compiled as C, too.
|
# ? Jan 23, 2009 23:17 |
|
I have a void* which I am casting to different types of pointers, e.g., (int*), (mystruct*), etc. As it turns out, there are several different types I would like to cast to and many different combinations which may occur, so it doesn't make sense to type all the cases out by hand. Is there some kind of way, using either functions or macros or whatever, that I kind mimic a sort of dynamic cast? What I mean is, I want something similar to a function that returns a different 'casting type,' like code:
code:
Edit: Added some cases to better illustrate the trickiness Essentially I have a type i associated with each void pointer. I'd like to be able to call f(i) in front of it... If I have to resort to a custom preprocessor for this, recommendations would be welcome. functional fucked around with this message at 01:14 on Jan 25, 2009 |
# ? Jan 24, 2009 02:52 |
|
functional posted:I'm sure this is an old problem. What do people typically do? If this is C, you can just do mystruct *anything = myvoidpointer; without any cast at all - C99 automatically promotes from void *.
|
# ? Jan 24, 2009 02:55 |
|
Using this code:code:
Why is it storing -340 as -84?
|
# ? Jan 24, 2009 03:09 |
|
Car posted:Using this code: You never initialize currIndex, and I'm not clear on why you have it and i. You also never initialize numToRead or open inputPtr, so I'm assuming you're leaving out important bits of the code, without which we can't really help you effectively. Edit: Also, -340 does not fit in a char, which is what your confusingly-named and never-allocated intArr is made of, and the modulo arithmetic works out to about the right value. ShoulderDaemon fucked around with this message at 03:18 on Jan 24, 2009 |
# ? Jan 24, 2009 03:15 |
|
ShoulderDaemon posted:Edit: Also, -340 does not fit in a char, which is what your confusingly-named and never-allocated intArr is made of, and the modulo arithmetic works out to about the right value. This is embarassing.
|
# ? Jan 24, 2009 03:30 |
|
I'm just dicking around with boost::spirit making a parser for a sort of JSON-ish config file format (actually inspired by .irssi/config). The grammar is defined as such:code:
code:
Volte fucked around with this message at 07:33 on Jan 24, 2009 |
# ? Jan 24, 2009 07:13 |
|
For one thing, your string literals are all kinds of hosed up. The Kleene star is greedy. You should be using a confix parser for them:code:
Avenging Dentist fucked around with this message at 07:35 on Jan 24, 2009 |
# ? Jan 24, 2009 07:32 |
|
Thanks, I fixed that as well as changing key to bs::lexeme_d[+(bs::alnum_p | bs::punct_p)]; and it works now. No idea why it doesnt stack overflow anymore... I think maybe VS was loving me over by not recompiling the parser even though I was editing the file, so some old bug stuck around no matter what changes I made. The example document clears the parser now.
|
# ? Jan 24, 2009 08:30 |
|
I posted about my Modern C++ Design-inspired Boost-ified factory patterns some months ago, but I've refined a lot of the code since then and added several new features. I'd really like to hear C&C from some of our resident C++/Boost wizards on what they think of the design/code/documentation etc, and whether or not it seems ready to be submitted to the Boost vault/sandbox any time soon.
|
# ? Jan 26, 2009 02:50 |
|
Can anybody recommend a simple XML parser for C++? All I need it to do for now is parse a string containing XML, allow me to find a certain node and read its contents. There seem to be millions of alternatives and I have no idea which one to choose. I'm using Dev-C++, if that matters.
|
# ? Jan 26, 2009 15:30 |
|
TinyXML or TinyXML++ would be the best for such a small use.
|
# ? Jan 26, 2009 15:38 |
|
If it's simple and consistent enough, you could also cheat and use regular expressions.
|
# ? Jan 26, 2009 19:41 |
|
OneEightHundred posted:If it's simple and consistent enough, you could also cheat and use regular expressions. Depart, ye cursed, to everlasting beta
|
# ? Jan 26, 2009 19:46 |
|
Mustach posted:TinyXML or TinyXML++ would be the best for such a small use. I used TinyXML in a recent project and it's very simple to use. One thing to note, however, is that in the latest version on their website, it will not parse spaces in attributes of tags (it returns everything UP TO the first space).
|
# ? Jan 26, 2009 19:55 |
|
Bitruder posted:I used TinyXML in a recent project and it's very simple to use. One thing to note, however, is that in the latest version on their website, it will not parse spaces in attributes of tags (it returns everything UP TO the first space). What do you mean spaces in attributes of tags? This isn't valid XML: <House The Name="White House" />. Unless you mean spaces in the attribute values, then that's a pretty big bug that would be missed by the TinyXML team.
|
# ? Jan 26, 2009 20:10 |
|
Ugg boots posted:What do you mean spaces in attributes of tags? This isn't valid XML: <House The Name="White House" />. Unless you mean spaces in the attribute values, then that's a pretty big bug that would be missed by the TinyXML team.
|
# ? Jan 26, 2009 21:56 |
|
I use MicroXML, does the job very well.
|
# ? Jan 26, 2009 22:31 |
|
I use libxml++ because I think everybody should be using C++ and depending on Gtkmm.
|
# ? Jan 27, 2009 01:21 |
|
(C++)I am trying to grab characters from a string I am reading in from redirecting the cin to a file. Say I have the string "Test" in the file in.txt. I can read the string into my 'string temp' variable. I can write the string to out.txt file as well. My problem is that I want, say, T from the string. cout << temp[0]; It gives me the following error in gedit when I try to open out.txt Could not open the file out.txt gedit has not been able to detect the character encoding. When I cout to the console, it works fine, but it won't work when writing to a file. What am I doing wrong?
|
# ? Jan 27, 2009 19:48 |
|
Could you post the code and attach in.txt or something?
|
# ? Jan 27, 2009 20:22 |
|
Not sure if this is the exact right thread, but I'll give it a shot. I'm trying to write a managed C++/CLI application and importing an unmanaged C(I think) DLL. I am using: code:
|
# ? Jan 27, 2009 21:36 |
|
I've run into a problem with trying to use boost::interprocess for synchronization between several programs. If a program locks a boost::interprocess::interprocess_mutex and then exits or crashes or something before unlocking it, then the mutex becomes unusable by any process. Here's an example program that demonstrates this: http://pastebin.com/m3432cfe1 Does anyone know of a good workaround for this? The program only needs to run on Windows so I've thought about just using the Win32 synchronization API functions such as CreateMutex -- however, with the flaws in that API I'm not sure if this can even be done correctly. For example, if more than one user is waiting for the same named event, how can we guarantee that they'll all wake up when the event becomes signaled? Edit: Here's an example with the Win32 API functions: http://pastebin.com/m1ee97c19 This one doesn't end up ruining the shared mutex when a process exits while still holding it, but now if you run more than one instance with Process2 then they just take turns waking up for each update. Olly the Otter fucked around with this message at 22:51 on Jan 27, 2009 |
# ? Jan 27, 2009 22:38 |
|
Vanadium posted:Could you post the code and attach in.txt or something? code:
code:
The maps are passed to us in a file (such as the one above). We have to write replicas of the maps with our path to another file. I'm using out.txt. I am running it using: ./proj1 --queue < in.txt > out.txt It gives the error I mentioned in my previous post when I try to access the buildings array to write to my out file. Also, my count variable (which keeps track of the index in the buildings array) gets up to 199 instead of the 192 max for this example.
|
# ? Jan 27, 2009 23:03 |
|
Can you post the relevant parts of proj1 Edit: Like, for example enough code so that it actually compiles, and ideally produces the output that is not to your expectations.
|
# ? Jan 27, 2009 23:06 |
|
|
# ? Jun 8, 2024 09:13 |
|
When I build a DLL with MinGW, is there any way of having functions with __stdcall but still exporting them as "function" instead of "function@16" and so on? I thought "--add-stdcall-alias" would do that, but it doesn't seem to change anything...
|
# ? Jan 27, 2009 23:40 |