|
|
#81 |
|
UG's Indigo River
Join Date: Jun 2009
Location: Mobile, Alabama
|
I was wondering if I could get some help with strings. Every time I run this code it runs both if statements. What am I doing wrong here?
Code:
__________________
Song of the Week Metropolitan Jazz Affair - Bird of spring Awakia Facebook Page Anime List Music Youtube |
|
|
|
|
|
#82 | |
|
why
Join Date: Apr 2010
|
Quote:
In programming, there are many data types. The ones we are concerned with now are Primitive types and Class types. Primitive types are characterized by the smaller amount of storage space they take in relation to the class type. Examples of Primitive data types are int, char, float, double, boolean, etc. Primitive data types can be manipulated with a lot of operators (ie +, -, *, /, ++, --, ==, etc). Class types, however, take a larger amount of storage space and can only be manipulated with very few operators. Strings are class types. So, you cannot compare strings with ==. The code will compile just fine, but it won't work. So, in order to compare strings, you need to use the following method: Code:
This method returns an int. The int returned depends on the contrast between the two strings. Basically, if the two strings are exactly alike, the int returned is 0. If the two strings are different by one character, the int returned is 1. 2 differences returns 2, etc. So for your first if statement, this is what you should do: Code:
Note that the method is case-sensitive. That means a capital/small letter difference can mean the difference between the strings matching or not. Hope I've helped. ![]() |
|
|
|
|
|
|
#83 |
|
UG's Indigo River
Join Date: Jun 2009
Location: Mobile, Alabama
|
It's not working. It still runs both statements regardless of what I input.
__________________
Song of the Week Metropolitan Jazz Affair - Bird of spring Awakia Facebook Page Anime List Music Youtube |
|
|
|
|
|
#84 | ||
|
Registered User
Join Date: Dec 2007
|
Quote:
i fixed it for u also u had some weird brackets also, u have to bring in the string class with #include<string>. u can do it the .compare way, which is good because there are functions that allow you to ignore case, but in this instance, you can use == to compare strings
__________________
Quote:
Last edited by ironman1478 : 03-02-2012 at 07:22 PM. |
||
|
|
|
|
|
#85 |
|
UG's Indigo River
Join Date: Jun 2009
Location: Mobile, Alabama
|
There we go, that did the trick. Thank you both for helping the code-tard.
__________________
Song of the Week Metropolitan Jazz Affair - Bird of spring Awakia Facebook Page Anime List Music Youtube Last edited by NothingRocks : 03-02-2012 at 07:28 PM. |
|
|
|
|
|
#86 |
|
Registered User
Join Date: May 2006
|
Here a link to one of my free games on the Android market:
https://market.android.com/details?...ames.helmknight and heres a video for it: Would like to know what you'll think of it. Thanks. |
|
|
|
|
|
#87 | |
|
why
Join Date: Apr 2010
|
Quote:
|
|
|
|
|
|
|
#88 | |
|
1984
Join Date: Aug 2005
Location: Adelaide, Australia
|
Quote:
just one thing to point out, when writing if statements you can basically make as complicated a logic statement as you like using brackets, equals (==), nots (!=), greater and less thans (> <), ands (&&) && ors (||). ie Code:
not that that example does anything useful but sometimes complex if statements are very useful.
__________________
$100AUD Super Strat Build
Kasabian - Fire fan video/uni work. Watch it! Jackson Dinky with TB-11 + SH-1 Pickups & Gotoh Floyd Rose Washburn D10S Dunlop 535Q Crybaby Fulltone OCD MXR 6 Band EQ Laney VC15 Hayden 2x12 |
|
|
|
|
|
|
#89 |
|
Told you not to trust me
Join Date: Sep 2008
Location: Lighthouse
|
Can anyone explain how headers work in c++ ?
Can you define the entire function in the header or can you only pass on the prototype from one .cpp file to another .cpp file? I've been told you can define the function in the header file but every time I try the compiler complains. When I include a library.h are the functions in there or are they in their own .cpp files? Also can I pass on other things from .h files like classes and enums? I have a basic knowledge of programming and I'm comfortable with java but c/c++ is frustrating me so much.
__________________
Day man Fighter of the Night man Champion of the sun You’re a master of karate and friendship…for everyone |
|
|
|
|
|
#90 | |
|
1984
Join Date: Aug 2005
Location: Adelaide, Australia
|
Quote:
C++ is a pain and the arse, I'm not entirely up to scratch with it but in terms of defining functions in the header treat it kind of like writing an interface in java. At the same time though the syntax is very fussy. Honestly I couldn't do it off the top of my head, every time I need to do something in C++ (which is rarely) I work out how to make it work and then use that as a template for all my other header files. Here's an example of some messy openGL code of mine Table.h: Code:
Table.cpp Code:
So as you can see you just define the variables and methods I wish to use in Table.cpp in the header file. Everything before the public: tag is basically the equivalent of making it private in java.
__________________
$100AUD Super Strat Build
Kasabian - Fire fan video/uni work. Watch it! Jackson Dinky with TB-11 + SH-1 Pickups & Gotoh Floyd Rose Washburn D10S Dunlop 535Q Crybaby Fulltone OCD MXR 6 Band EQ Laney VC15 Hayden 2x12 |
|
|
|
|
|
|
#91 | |
|
Registered User
Join Date: May 2006
|
Quote:
Generally you only define prototypes in a header although you can also implement them in the headers. Template functions and classes can actually only be implemented in headers. When you include a library.h file which is a part of a pre compiled library generally it has a corresponding .lib file(or some similar file) which includes the definitions of the prototypes in the header. If it is not pre compiled then the implementions are a part of a cpp file generally. You can include other things from a header file like constants, enums, struct definitions in addition to the class definitions and the function prototypes. You should generally make it a habit of having something known as "header guards" in your headers which allow you to avoid a multiple declaration error when you end up "#include"ing a header multiple times. Here's a simple example: Sample.h Code:
Basically the #ifndef, #define and the #endif are what consist of a header guard. You could do a search on them if you want to really understand them properly. Heres how you'd implement the stuff defined in Sample.h. Sample.cpp Code:
Hope that helps. |
|
|
|
|
|
|
#92 |
|
Told you not to trust me
Join Date: Sep 2008
Location: Lighthouse
|
That clears up a lot of problems but now I'm having problems understanding why you would actually use headers... Are they the only way to access code out of your .cpp?
Are they used as a place to build empty functions (like a java interface) or do I use them to access the function from one .cpp to another? If the table class was in another .cpp file, what would I write in the header to obtain all functions and constructors in another .cpp file In other words, let's say I have 2 files: someObject.cpp containing the class for some object and all it's functions (that already have bodies) and another.cpp where I'd want to create an instance of this object and use it's functions What would i write in the header file included in another.cpp to get everything from the class? Thanks alot guys, I've struggling with this for a while.
__________________
Day man Fighter of the Night man Champion of the sun You’re a master of karate and friendship…for everyone |
|
|
|
|
|
#93 |
|
the ghetto pimpass nigga
Join Date: Oct 2010
Location: Louth, Ireland
|
What's the best free 3D program?
__________________
REGGAE Have a Peavey VYPYR? Like Assassin's Creed? Xbox - GamingFailure porn
LIVERPOOL F.C. Hollywood Undead
![]() |
|
|
|
|
|
#94 | |
|
.... shabba!
Join Date: Oct 2007
Location: That hill in Scotland
|
Quote:
If you're a student/know someone with a student email who wouldn't mind loaning you it, you can get 3DS Max for free, legally. Or you can use... other methods. It's the only one I've used, so sorry I can't be of more help.
__________________
,--.-'-,--. \ /-~-\ / / )' a a `( \ ( ( ,---. ) ) THIS WAS MEANT TO BE A PIG \ `(_o_o_)' / \ `-' / | |---| | [_] [_] |
|
|
|
|
|
|
#95 | |
|
Registered User
Join Date: May 2006
|
Quote:
C++ .h files aren't really like Java's interfaces. They are totally different in the way they work and what they are meant to do. In C++ a .h and .cpp pair is whats analogous to a .java file in Java if you really want to compare the two languages. One purpose of headers is that they allow you to keep separate parts of your program in separate files based on their functionality. This might seem like a disadvantage if you are actually working on a small program but when you are working on something thats big and complex it really does help a lot to manage the code. Another advantage of headers is they work like a kind of a "menu card" of the functionality. So if you have written a class or a set of functions and you are giving it to one of your co-workers to use it, they'd just have a look at the .h file and get an idea of what the class/functions do without having to worry about looking at the implementation which is stored in the .cpp. I am not sure if headers are the only way to access code in other files. I haven't tried it but I guess something like #include "somefile.cpp" should work in some cases. But I wouldn't recommend doing that though. Stick to using .h files for that. I'll try and answer the last part of your question with an example: Lets say I have the following file(object.cpp): Code:
Code:
Code:
Code:
AnotherObject.h: Code:
|
|
|
|
|
|
|
#96 | |
|
UG Senior Member
Join Date: Jul 2006
Location: SLUT
|
Quote:
Blender.
__________________
__________________________________________________ ____________________ Last edited by Gyroscope : Tomorrow at 01:00 PM. |
|
|
|
|
|
|
#97 | |
|
Told you not to trust me
Join Date: Sep 2008
Location: Lighthouse
|
Quote:
I just had an aha moment. Thanks guys.
__________________
Day man Fighter of the Night man Champion of the sun You’re a master of karate and friendship…for everyone |
|
|
|
|
|
|
#98 | |
|
Registered User
Join Date: May 2006
|
Quote:
Glad to be of help ![]() |
|
|
|
|
|
|
#99 | |
|
Registered User
Join Date: Sep 2008
Location: Norway
|
I played around with Game Maker as early as at age 12, even made a 3D platformer in it at age 14/15, though it was not that robust and I had some workarounds that I didn't even understand why worked.
I picked up Unity3D in april/may last year as it was relevant to the college degree I had as the first preference at the time and learned the basics of it and C# scripting, because I've knew some of it. I found another degree that was more programming oriented and less design than the one I originally had as first preference, and picked up C++ with the exact tutorials that the OP links to to be somewhat prepared. College began and the programming was very easy, as I knew almost two semesters worth of programming already then. I'm on the second semester and the programming still is easy, but it means I can skip the class on mondays and focus on the game programming related math and the unrelated networking course that is in the degree -- knowing about TCP/UDP i do understand, but setting up a Cisco router... The game design's boredom-challenge-frustration scale seem to apply to programming assignments too, doing a fill-in-the-blank assignments among the professor's horrible, horrible code makes me procrastinate them out of scorn. Like 1-based arrays and compressed code. I have made some game object structure upon OpenGL, but i don't really like it. I want to try aggregation to avoid the "Diamond of Death"-problem (class B and C inherits from A and D wants to inherit from both B and C = fail). Quote:
You had an if-statement ending with a ';', which means, if that do nothing. The block below is entered anyways independenlty of the if-statement you had. If you use char-arrays for strings, comparing stra == strb will check if they both are in the same place in memory, which they arent. You'll understand that shit when you learn about pointers. std::string (or just string if you use the "std" namespace), is, like you see in the code that solved your problem, the way to do it nicely, but if you had to char-arrays. char a[] = "Hello"; char b[] = "Hello"; you could write. if(strcmp(a, b) == 0) { ...do your stuff } If you take input into char-arrays, do make them large enough, as writing beyond them can get shit to **** up, i.e. writing to memory the program doesn't own and you might get access violations. Your program is too small to begin thinking about saving memory. so... char input[128]; cin >> input; if(strcmp(input, "Hello") == 0) { // user wrote "Hello" } strcmp's return value is returning the difference, good for alphabetically sorting stuff, but 0 means exact match, which is what you was looking for (case sensitive). Last edited by GisleAune : 03-04-2012 at 04:41 PM. |
|
|
|
|
|
|
#100 |
|
UG's Indigo River
Join Date: Jun 2009
Location: Mobile, Alabama
|
So, I'm thinking about making an RPG. I have another coder and a writer, but I know it wouldn't be enough. So, if anyone here is up for joining the team let me know. I'm accepting spriters, coders, writers, artists, and maybe musicians/composers.
__________________
Song of the Week Metropolitan Jazz Affair - Bird of spring Awakia Facebook Page Anime List Music Youtube Last edited by NothingRocks : 03-06-2012 at 11:50 PM. |
|
|
|
![]() |
| Thread Tools | Rate This Thread | |
|
|