Go Back   UG Community @ Ultimate-Guitar.Com > UG Community > The Pit
User Name  
Password
Search:

Reply
Old 03-02-2012, 06:19 PM   #81
NothingRocks
:^)
 
NothingRocks's Avatar
 
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:
#include <iostream> int main() { using namespace std; string answer; cout << "As you walk along the grassy field you're ambushed by a kobold! \n\n"; cout << "What do you wish to do? \n\n"; getline (cin,answer); cout << endl; { if (answer == "attack", "Attack"); { cout << "You attack the kobold! \n\n"; } if (answer == "defend", "Defend") { cout << "You brace yourself against the kobold's attack \n\n"; } } system("pause"); return 0; }
__________________
四百二十燃やせこのオカマやろう!

Awakia Facebook Page

Anime List
Music
Youtube
NothingRocks is online now   Reply With Quote
Old 03-02-2012, 06:43 PM   #82
behind_you
(° ل͜ °)
 
behind_you's Avatar
 
Join Date: Apr 2010
Quote:
Originally Posted by NothingRocks
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:
#include <iostream> int main() { using namespace std; string answer; cout << "As you walk along the grassy field you're ambushed by a kobold! \n\n"; cout << "What do you wish to do? \n\n"; getline (cin,answer); cout << endl; { if (answer == "attack", "Attack"); { cout << "You attack the kobold! \n\n"; } if (answer == "defend", "Defend") { cout << "You brace yourself against the kobold's attack \n\n"; } } system("pause"); return 0; }
You cannot compare strings this way. I'll explain (If I'm mistaken anywhere, someone can correct me).

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:
String1.compare(String string2); //Where 'string1' is the name of the first string variable you want to compare


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:
if (answer.compare("attack") == 0); //This is true if the value of the string variable 'answer' is exactly the same as the word 'attack'


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.
__________________
ʕʔ ALL BEARS GO TO HEAVEN ʕʔ
behind_you is offline   Reply With Quote
Old 03-02-2012, 07:09 PM   #83
NothingRocks
:^)
 
NothingRocks's Avatar
 
Join Date: Jun 2009
Location: Mobile, Alabama
It's not working. It still runs both statements regardless of what I input.
__________________
四百二十燃やせこのオカマやろう!

Awakia Facebook Page

Anime List
Music
Youtube
NothingRocks is online now   Reply With Quote
Old 03-02-2012, 07:17 PM   #84
ironman1478
Registered User
 
ironman1478's Avatar
 
Join Date: Dec 2007
Quote:
Originally Posted by NothingRocks
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:
#include <iostream> #include <string> int main() { using namespace std; string answer; cout << "As you walk along the grassy field you're ambushed by a kobold! \n\n"; cout << "What do you wish to do? \n\n"; getline(cin,answer); //u had a space in the getline function. i dont think u can do that/ cout << endl; if (answer == "attack" || answer =="Attack" ) { cout << "You attack the kobold! \n\n"; } else if (answer == "defend" || answer == "Defend") { cout << "You brace yourself against the kobold's attack \n\n"; } system("pause"); return 0; }

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:
Originally Posted by Meths
If there were 12th century popes alive today they'd be telling Rick Santorum to get out of the fucking dark ages already.

Last edited by ironman1478 : 03-02-2012 at 07:22 PM.
ironman1478 is offline   Reply With Quote
Old 03-02-2012, 07:27 PM   #85
NothingRocks
:^)
 
NothingRocks's Avatar
 
Join Date: Jun 2009
Location: Mobile, Alabama
There we go, that did the trick. Thank you both for helping the code-tard.
__________________
四百二十燃やせこのオカマやろう!

Awakia Facebook Page

Anime List
Music
Youtube

Last edited by NothingRocks : 03-02-2012 at 07:28 PM.
NothingRocks is online now   Reply With Quote
Old 03-02-2012, 07:47 PM   #86
RRRone
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.
RRRone is offline   Reply With Quote
Old 03-02-2012, 07:50 PM   #87
behind_you
(° ل͜ °)
 
behind_you's Avatar
 
Join Date: Apr 2010
Quote:
Originally Posted by ironman1478
u have to bring in the string class with #include<string>
Yeah, I completely forgot about that. Been a while since I've messed with C++. Spend most of my time with Java now.
__________________
ʕʔ ALL BEARS GO TO HEAVEN ʕʔ
behind_you is offline   Reply With Quote
Old 03-02-2012, 08:48 PM   #88
Diamond Dave
1984
 
Diamond Dave's Avatar
 
Join Date: Aug 2005
Location: Adelaide, Australia
Quote:
Originally Posted by NothingRocks
There we go, that did the trick. Thank you both for helping the code-tard.

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:
if((a==b && c!=b)||(a==c && c>b)) then


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
Diamond Dave is offline   Reply With Quote
Old 03-02-2012, 09:34 PM   #89
yaaarp
Told you not to trust me
 
yaaarp's Avatar
 
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
yaaarp is offline   Reply With Quote
Old 03-02-2012, 10:45 PM   #90
Diamond Dave
1984
 
Diamond Dave's Avatar
 
Join Date: Aug 2005
Location: Adelaide, Australia
Quote:
Originally Posted by yaaarp
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.

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:
/** This class draws a table by scaling and moving blocks. The table feet are the plane y = 0 */ class Table { GLfloat tableX, tableY, tableZ; GLfloat legX, legY, legZ; public: Table( GLfloat width, GLfloat length ); void render(); };


Table.cpp
Code:
#ifdef MacOSX #include <OpenGL/gl.h> #else #include <GL/gl.h> #endif #include "Table.h" // Cube vertex data GLfloat Vertices[8][3] = { { -1.0, -1.0, 1.0 }, { 1.0, -1.0, 1.0 }, { 1.0, 1.0, 1.0 }, { -1.0, 1.0, 1.0 }, { -1.0, -1.0, -1.0 }, { 1.0, -1.0, -1.0 }, { 1.0, 1.0, -1.0 }, { -1.0, 1.0, -1.0 } }; // Helper function to draw a quad given indices to vertex array static void drawQuad(int a, int b, int c, int d) { glVertex3fv(Vertices[a]); glVertex3fv(Vertices[b]); glVertex3fv(Vertices[c]); glVertex3fv(Vertices[d]); } /** drawBlock simply draws a cube from -1 to 1 in all directions. We change what it produces using transformations. */ static void drawBlock() { glBegin(GL_QUADS); // "Near" face, z = 1 glColor3f(0.6, 0.6, 0.6); drawQuad(0, 1, 2, 3); // "Right" face, x = 1 glColor3f(0.4, 0.4, 0.4); drawQuad(1, 5, 6, 2); // "Far" face, z = -1 glColor3f(0.2, 0.2, 0.2); drawQuad(5, 4, 7, 6); // "Left" face, x = -1 glColor3f(0.4, 0.4, 0.4); drawQuad(4, 0, 3, 7); // "Top" face, y = 1 glColor3f(0.8, 0.8, 0.8); drawQuad(3, 2, 6, 7); // "Bottom" face, y = -1 glColor3f(0.1, 0.1, 0.1); drawQuad(4, 5, 1, 0); glEnd(); } /** Constructor - calculates the table's dimensions based on the desired height and width. */ Table::Table( GLfloat width, GLfloat length ) { tableX = width; tableY = 0.05; tableZ = length; legX = width / 64; legY = 0.5; legZ = width / 64; } /** Draw the table as a set of scaled blocks. Notice push and pop to make things easier. */ void Table::render() { // Now draw the scene... // First, move down twice the leg height // This makes the feet of the table the plane y = 0 glTranslatef( 0.0, 2*legY, 0.0 ); // Draw the table top glPushMatrix(); glScalef( tableX, tableY, tableZ ); drawBlock(); glPopMatrix(); // Now the 4 legs... glPushMatrix(); glTranslatef( tableX-8*legX, -tableY-legY, tableZ-8*legZ ); glScalef( legX, legY, legZ ); drawBlock(); glPopMatrix(); glPushMatrix(); glTranslatef( -tableX+8*legX, -tableY-legY, tableZ-8*legZ ); glScalef( legX, legY, legZ ); drawBlock(); glPopMatrix(); glPushMatrix(); glTranslatef( -tableX+8*legX, -tableY-legY, -tableZ+8*legZ ); glScalef( legX, legY, legZ ); drawBlock(); glPopMatrix(); glPushMatrix(); glTranslatef( tableX-8*legX, -tableY-legY, -tableZ+8*legZ ); glScalef( legX, legY, legZ ); drawBlock(); glPopMatrix(); // Undo the translation at the start of the function glTranslatef( 0.0, -2*legY, 0.0 ); }


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
Diamond Dave is offline   Reply With Quote
Old 03-03-2012, 06:24 AM   #91
RRRone
Registered User
 
Join Date: May 2006
Quote:
Originally Posted by yaaarp
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.



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:
#ifndef _SAMPLE_H #define _SAMPLE_H const int CONSTANT = 10; enum SampleEnum { ONE = 0, TWO, THREE }; void sampleFunction(); class Sample { private: int one; public: void foo(); }; #endif


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:
#include "Sample.h" #include <iostream> using namespace std; void sampleFunction() { cout << "Hello\n"; } void Sample::foo() { one = CONSTANT; }


Hope that helps.
RRRone is offline   Reply With Quote
Old 03-03-2012, 11:19 AM   #92
yaaarp
Told you not to trust me
 
yaaarp's Avatar
 
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
yaaarp is offline   Reply With Quote
Old 03-03-2012, 11:30 AM   #93
LostLegion
resurrected
 
LostLegion's Avatar
 
Join Date: Oct 2010
What's the best free 3D program?
__________________
LIVERPOOL F.C.


LostLegion is online now   Reply With Quote
Old 03-03-2012, 01:10 PM   #94
EMGs_rule
.... shabba!
 
EMGs_rule's Avatar
 
Join Date: Oct 2007
Location: That hill in Scotland
Quote:
Originally Posted by LostLegion
What's the best free 3D program?


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_)' /
\ `-' /
| |---| |
[_] [_]
EMGs_rule is offline   Reply With Quote
Old 03-03-2012, 06:41 PM   #95
RRRone
Registered User
 
Join Date: May 2006
Quote:
Originally Posted by yaaarp
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.

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:
#include <iostream> using namespace std; void doSomething() { cout << "Doing something..." << endl; } class Object { private: int id; public: Object() {} void setId(int i) { id = i; } void printId { cout << id << endl; } };
Now I'd like to use the object.cpp stuff in my main.cpp file. For that, I'd first create an object.h file like so:

Code:
#ifndef _OBJECT_H #define _OBJECT_H void doSomething(); class Object { private: int id; public: Object(); void setId(int i); void printId(); }; #endif
Then I'd change my object.cpp file to look like this

Code:
#include "object.h" #include <iostream> using namespace std; void doSomething() { cout << "Doing something..." << endl; } Object::Object() { } void Object::setId(int i) { id = i; } void Object::printId() { cout << id << endl; }
Now I can use the object class and the function in my main.cpp file like so:

Code:
#include "object.h" int main() { Object object; object.setId(10); object.printId(); doSomething(); return 0; }
Now, if I want to use the Object class in another class(AnotherObject) of mine, I'd do it like this

AnotherObject.h:
Code:
#ifndef _ANOTHER_OBJECT_H #define _ANOTHER_OBJECT_H #include "object.h" class AnotherObject { Object object; public: void doSomeStuff(); }; #endif
You have to #include "object.h" in any .h or .cpp file where you wish to use the functions and classes defined by it
RRRone is offline   Reply With Quote
Old 03-03-2012, 09:56 PM   #96
Gyroscope
UG Senior Member
 
Gyroscope's Avatar
 
Join Date: Jul 2006
Location: SLUT
Quote:
Originally Posted by LostLegion
What's the best free 3D program?

Blender.
__________________
__________________________________________________ ____________________
Last edited by Gyroscope : Tomorrow at 01:00 PM.
Gyroscope is offline   Reply With Quote
Old 03-04-2012, 01:15 PM   #97
yaaarp
Told you not to trust me
 
yaaarp's Avatar
 
Join Date: Sep 2008
Location: Lighthouse
Quote:
Originally Posted by RRRone
Awesome explanation


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
yaaarp is offline   Reply With Quote
Old 03-04-2012, 04:10 PM   #98
RRRone
Registered User
 
Join Date: May 2006
Quote:
Originally Posted by yaaarp
I just had an aha moment. Thanks guys.


Glad to be of help
RRRone is offline   Reply With Quote
Old 03-04-2012, 04:24 PM   #99
GisleAune
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:
Originally Posted by NothingRocks
There we go, that did the trick. Thank you both for helping the code-tard.

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.
GisleAune is offline   Reply With Quote
Old 03-06-2012, 11:49 PM   #100
NothingRocks
:^)
 
NothingRocks's Avatar
 
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.
__________________
四百二十燃やせこのオカマやろう!

Awakia Facebook Page

Anime List
Music
Youtube

Last edited by NothingRocks : 03-06-2012 at 11:50 PM.
NothingRocks is online now   Reply With Quote
Reply


Thread Tools Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT -4. The time now is 04:59 AM.

Forum Archives / About / Terms of Use / Advertise / Contact / Ultimate-Guitar.Com © 2014
Powered by: vBulletin Version 3.0.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.