|
I talked to my tutor and from that I got my checks working. However now I can't do the binary to decimal conversion. I'm trying to use the algorithm that my lecturer showed us but for some reason I get a garbage answer when I print it. code:
|
# ? Aug 2, 2011 21:51 |
|
|
# ? Jun 10, 2024 09:04 |
|
Well, unless you've got logic elsewhere that converts '1' to 1 then you will have problems with that loop. '0' is the character that looks like 0, if you treat that character as a number then it comes out to 49. So in your for loop if you were given the string 11111111 then it comes out as 49*1 + 49*2 + 49*4 + ... + 49*128, which is more than 255, which is what 11111111 would represent in binary. Also, I'm not sure what you're trying to do with the scanf and printf. EDIT: In fact, I'm not sure what you're doing with most of it. Edison was a dick fucked around with this message at 23:21 on Aug 2, 2011 |
# ? Aug 2, 2011 23:14 |
|
Yeah, I also caught the '0' == 49 thing, but the rest isn't clear at all. Did you mean to comment out the loop? Because it makes no sense if nothing in the loop is using the counter variable. Also, the stuff in the loop will give you problems if len is, say, anything other than 8 (to be honest, I never tried a negative array index. Does it just roll over or what?). Tell us more about what (and how) exactly you're trying to achieve at each step.
|
# ? Aug 2, 2011 23:33 |
Looking at your decimal/binary check first, you've got something messed up. If both the "first character is '0'" and the "first character is '2'" test fail, you then ask the user for some input. You write that input to a string variable named "answer". But for the two other cases, you write the input type to s[0]. You never check the value of "answer" later either. (Your check is also not very flexible. What if the user enters "093"?) Then later, for the actual conversion stuff, you have a loop with an "i" variable as the control variable. But you never reference that variable anywhere in the loop. You also just attempt to handle the entire input on every iteration. You also assume that the input is always 8 characters in that code. You also forget that array indexes start at zero and count to length minus one (visible because you attempt to index the string with its length.) Also check whether there is a number between 4 and 6 Making an integer from a string representing a binary number is really very simple: number = 0; for (i = 0; i < length; i++) { number *= 2; if (string[i] == '1') { number += 1; } } If you do read my solution, then explain to me what it does and why it works! (And also tell what happens if the string contains any other characters than '0' and '1'.)
|
|
# ? Aug 2, 2011 23:34 |
|
Red_Fred posted:Hey guys got a new assignment for a different paper and a new problem! Yay, or something. Okay this post from earlier basically explains what I have to do. So my program needs to figure out if the input value is binary or decimal. If it can't tell it is supposed to ask the user which it is. Invalid input are any other characters that aren't 0-9 or input like 093. I should assume that input beginning with 0 will be binary. I tried the switch nielsm but I couldn't get it to work and the above is what my tutor suggested I do with regard to input checking. She also said that I should use sscanf to convert from a char array to integers and not atoi which I was going to use. All the printf calls are me just checking things are working along the way. Mobby_6kl yeah that * is not supposed to be there, sorry. So the tests that are there are supposed to test the first variable that is input. If it is a 0 then the assumption that it is binary is taken, if it is a 2 then the assumption that it is decimal is made. If it is 1 then the user is asked which it is. So far I have just tried the binary to decimal conversion which I'm having a hard time getting my head around (among everything else.)
|
# ? Aug 2, 2011 23:56 |
|
Otto Skorzeny posted:yo falafel how'd you get cancered I posted in LF for three years and don't want to spend $8 so people will take me seriously when I'm talking about technical stuff?
|
# ? Aug 2, 2011 23:58 |
|
That's what I mean, I thought the LF people got decancered when the forum got closed. Guess I was wrong!
|
# ? Aug 3, 2011 04:47 |
|
mobby_6kl posted:I never tried a negative array index. Does it just roll over or what? It blindly indexes into whatever is in memory before the start of the array, and interprets whatever is there as the type you're looking at (int, float, some class type, whatever). C and thus C++ arrays do no bounds checking.
|
# ? Aug 3, 2011 05:17 |
|
Having some trouble with converting something to float which also happens to be a pointer. Note: projectTestFace is a float* and I'm using OpenCV2.2 for the Matrices. The original: code:
code:
|
# ? Aug 6, 2011 09:04 |
|
Haven't touched OpenCV in a little while, I think you want something like:code:
code:
|
# ? Aug 6, 2011 09:12 |
|
I asked this in the general purpose questions mega-thread last week and didn't get anywhere, so I'm going to try again here and hopefully someone will have a solution. I'm integrating Lua support into my application and I built both 64 and 32 bit static Lua libraries using TDM-GCC; these of course correlate to the 64 and 32 bit builds of the program I'm integrating Lua into. I have no problem with either build on my 64-bit machine (using gcc flags -m64 and -m32). I'm on my laptop though and it's 32-bit only, and attempting to build a 32 bit build on it provides me with these errors: code:
All that said, this only affects compilation. 32-bit builds on the home computer run just fine on the laptop.
|
# ? Aug 6, 2011 16:14 |
|
I'm trying to learn the win32 api. My tutor has given each of us a task for the week. My first one was easy enough (a temporary file cleaner), but the second one is to determine weather or not windows is up to date. I'm reading around on MSDN and there seems to be a COM interface for something called the Windows Update Agent API - http://msdn.microsoft.com/en-us/library/aa387287(v=VS.85).aspx Now, the interfaces described in here require you to retrieve the coclass that implements these interfaces, I assume there is a CLSID defined for the windows update agent? Where do I find it? Apologies if this question doesn't make sense, I'm really taking my first steps into COM programming. EDIT: I figured out that you just prefix the interface name with CLSID_ Amethyst fucked around with this message at 10:28 on Aug 8, 2011 |
# ? Aug 8, 2011 09:33 |
|
I'm a bit confused as to a constructor I was given in a template:code:
|
# ? Aug 9, 2011 03:08 |
|
Oxyclean posted:I don't understand the first constructor; what am I supposed to do with it? Implement it.
|
# ? Aug 9, 2011 03:12 |
|
e: realized stupid mistake, ugh. Forgot about the whole "Course::" poo poo. e: Right, that too, thanks. Oxyclean fucked around with this message at 03:31 on Aug 9, 2011 |
# ? Aug 9, 2011 03:16 |
|
That's because one of Course's methods, Report, is declared virtual with no implementation. You'd have to implement a subclass for that.
|
# ? Aug 9, 2011 03:23 |
|
I'm writing a c preprocessor, and something about visual studio's C preprocessor isn't working as I would expect. The following code: code:
edit: it could just be pasting the newline in
|
# ? Aug 9, 2011 04:29 |
|
It requires token pasting http://msdn.microsoft.com/en-us/library/09dwwt6y%28v=VS.100%29.aspx
|
# ? Aug 9, 2011 04:35 |
|
emonkey posted:I'm writing a c preprocessor, and something about visual studio's C preprocessor isn't working as I would expect. Don't try to implement the C preprocessor without referring to the standard. That is correct behavior on the part of Visual Studio. There are plenty of non-compliant C preprocessors out there already (Microsoft's being one of them), the world doesn't need another one!
|
# ? Aug 9, 2011 05:06 |
|
One of my friends does a lot of work in C++, and he recommended to me that I don't use .cpp/.hpp pairs but instead do all my method definitions right inside the class definitions in the .hpp file. I trusted him at the time because hey, he does this all day long, but I'm beginning to encounter some difficulties as a result (especially when circular definitions enter the picture). Is this a respectable practice I should continue or a horror I should purge from my code?
|
# ? Aug 9, 2011 20:11 |
|
Hot Dog Day #42 posted:One of my friends does a lot of work in C++, and he recommended to me that I don't use .cpp/.hpp pairs but instead do all my method definitions right inside the class definitions in the .hpp file. I trusted him at the time because hey, he does this all day long, but I'm beginning to encounter some difficulties as a result (especially when circular definitions enter the picture). For templates, you effectively have to do this, but otherwise I don't even see how it's possible without running into massive amounts of link errors?
|
# ? Aug 9, 2011 20:25 |
|
Yes, the code uses lots of templates, and my friend is big into generic programming which is probably where the advice came from.
|
# ? Aug 9, 2011 20:28 |
|
Hot Dog Day #42 posted:Yes, the code uses lots of templates, and my friend is big into generic programming which is probably where the advice came from. Template methods effectively need to be in headers if they are used in more than one translation unit, otherwise the compiler is not aware of their implementations at template instantiation time. But in the case of non-templated methods, you're going to run into major problems: a.cpp: code:
code:
code:
|
# ? Aug 9, 2011 20:41 |
|
I think he means defining the methods inline in the class definition.
|
# ? Aug 9, 2011 21:33 |
|
Hot Dog Day #42 posted:One of my friends does a lot of work in C++, and he recommended to me that I don't use .cpp/.hpp pairs but instead do all my method definitions right inside the class definitions in the .hpp file. I trusted him at the time because hey, he does this all day long, but I'm beginning to encounter some difficulties as a result (especially when circular definitions enter the picture). It's not a horror but it will increase your compile times, and sometimes you will have to put implementations in a cpp file and then you've got some code in cpp files, other code in hpp files, and it's better just to put it all in cpp files, neh? Increasing your compile times is of course a project-dependent problem, but it's been enough of a problem for me to get anal about removing extraneous stuff from headers. So just put your nontemplated implementations in cpp files, except, perhaps, for really short methods, and except for other general circumstances where you don't give a gently caress. You certainly aren't coding your project into a corner with this practice, so it's nothing to worry too hard about.
|
# ? Aug 9, 2011 22:23 |
|
Hot Dog Day #42 posted:One of my friends does a lot of work in C++, and he recommended to me that I don't use .cpp/.hpp pairs but instead do all my method definitions right inside the class definitions in the .hpp file. I trusted him at the time because hey, he does this all day long, but I'm beginning to encounter some difficulties as a result (especially when circular definitions enter the picture). Purge it. When you look at a class definition, the thing you usually want to see is the function definitions, so you know what you can do with the class. Having all the the code mixed in makes the actual interface of the class much harder to read. As your system gets more complex, the issues you have with circular definitions are likely to just get worse as well. Java coders (and C# coders, I think) put code into class definitions as standard practice, but they have access to things that C++ coders don't (pure virtual interfaces that actually work properly, plus a different way of importing symbols from other files). Gerblyn fucked around with this message at 22:35 on Aug 9, 2011 |
# ? Aug 9, 2011 22:31 |
|
The main thing that Java coders have is a language that requires compilers to deal with dependency cycles between different files and classes, instead of a language that requires compilers to process things in exactly the order they were written.
|
# ? Aug 10, 2011 00:04 |
|
Gerblyn posted:Java coders (and C# coders, I think) put code into class definitions as standard practice, but they have access to things that C++ coders don't (pure virtual interfaces that actually work properly
|
# ? Aug 10, 2011 02:12 |
|
rjmccall posted:The main thing that Java coders have is a language that requires compilers to deal with dependency cycles between different files and classes, instead of a language that requires compilers to process things in exactly the order they were written. This is literally the only thing that I wish C++ had that it isn't getting in 0xb (are we still calling it that or is that joke played out) (besides concepts )
|
# ? Aug 10, 2011 02:41 |
|
TasteMyHouse posted:This is literally the only thing that I wish C++ had that it isn't getting in 0xb (are we still calling it that or is that joke played out) (besides concepts ) A good module system for C++ is overdue but I don't think it will ever happen
|
# ? Aug 10, 2011 04:02 |
|
So I'm having some problems with this assignment because I'm really unfamiliar with the way he wants us to do things. The particular problem comes with this line: code:
When I replace fname with something like "test", it ends up displaying fine when I use cout to print it, but attempting to use a variable, such as fname (which is intialzed with a cin ) causes it to print garbage. This is the GetName function. code:
In other words, how do get the contents of the char array into m_name with that accessor function? E: Thanks, that gave me something to work with. Oxyclean fucked around with this message at 05:36 on Aug 10, 2011 |
# ? Aug 10, 2011 04:46 |
|
Where to start. GetName returns a reference to a field of type char*. Since it's a reference, the call result is an l-value, and you can assign to it. You are successfully assigning it a pointer that has been produced by "decaying" the array reference to a pointer to its first element, with the result that the value in the field is now a pointer to this local variable. Hooray. You then presumably return from the function and leave the field pointing to a local variable which has gone out of scope. Some other function, quite possibly the operator<< function, then scribbles all over your poor, abandoned local variable. Boo. This works with "test" because the storage for the characters in "test" is allocated globally, meaning it exists forever and always. You need the data to which that field points to live long enough for you to print it. Usually that means allocating it on the heap (and then freeing it later).
|
# ? Aug 10, 2011 04:58 |
|
Visual Studio question... I open up a vcproj file and see: code:
Is there a way to see/set this from within Dev Studio?
|
# ? Aug 10, 2011 18:06 |
|
Are you looking at the right project configuration?
|
# ? Aug 10, 2011 19:07 |
|
I had sworn off C++ (still traumatized over spending hours decrypting the compiler's BS error messages when I was in college), but I just heard about clang. Supposedly, it's a C++ compiler with error messages meant to be understood by other people than those that wrote the compiler. I figured I'd give it a try. I got it working from the command-line by extracting clang and llvm to my mingw folder, but I want an IDE (any IDE) to use it with. I tried Netbeans, but it gave me a "mkdir: Command not found" error. I googled it and followed instructions to add the MinGW bin path to Windows' %PATH% environment variable, but I still get that error (though the text surrounding it is different despite the input being the same). I then tried Qt Creator, which I thought let me use clang++.exe as a compiler, but is still compiling with mingwgcc or whatever in the background. I added a new toolchain where the compiler path points at clang++.exe, then went in my project's options and selected my own toolchain from the dropdown list in place of the default "MinGW as a GCC for Windows target". It seemed simple enough. But I know it's still compiling in GCC because it's giving me GCC error messagess. For example, when I omitted a semi-colon, Qt Creator's compile output said "new types may not be defined in a return type: (perhaps a semicolon is missing after the definition of 'Cat')". If it had been using clang, it would've said "error: expected ';' after class". I also tried Visual Studio 2010, but there are no options to select a different compiler. I should've known Any advice?
|
# ? Aug 10, 2011 19:25 |
MachinTrucChose posted:I also tried Visual Studio 2010, but there are no options to select a different compiler. I should've known Actually VC2010 does allow you to choose a different compiler, but the build rules for it have to be defined as a series of MSBuild files. It should theoretically be possible to write a ruleset to allow you to use CLang. (If you are interested, the relevant files are installed under C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0) Also, in the specific example you gave last, the error produced by GCC reads just fine to me, it tells you what you have actually written and why it is wrong, and gives you a suggestion for what you might have intended.
|
|
# ? Aug 10, 2011 19:46 |
|
nielsm posted:Also, in the specific example you gave last, the error produced by GCC reads just fine to me, it tells you what you have actually written and why it is wrong, and gives you a suggestion for what you might have intended. It's probably template errors that they're worried about.
|
# ? Aug 10, 2011 20:39 |
|
Plorkyeran posted:Are you looking at the right project configuration? Unfortunately I am. And I can't find anything on MSDN about this.
|
# ? Aug 10, 2011 20:46 |
|
When using (TR1) smart pointers, which is a better rule of thumb: use shared_ptrs unless you have a reason not to, or use weak_ptrs unless you have a reason not to? I understand how they both work with respect to ownership, but there are situations where it doesn't seem particularly clear-cut (like passing a smart pointer into a function) and I wonder if there's just some best practices guideline.
|
# ? Aug 10, 2011 20:47 |
|
|
# ? Jun 10, 2024 09:04 |
|
GrumpyDoctor posted:When using (TR1) smart pointers, which is a better rule of thumb: use shared_ptrs unless you have a reason not to, or use weak_ptrs unless you have a reason not to? I understand how they both work with respect to ownership, but there are situations where it doesn't seem particularly clear-cut (like passing a smart pointer into a function) and I wonder if there's just some best practices guideline. The way I understand it, use a shared_ptr unless doing so would cause a cyclic reference, in which case use a weak_ptr -- but I'm actually not sure what you think isn't clear cut about passing a smart pointer into a function and I'm curious to hear you elaborate.
|
# ? Aug 10, 2011 21:11 |