Page 3 of 12
#81
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?


#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;
}
#82
Quote 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?


#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:

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:

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.
ggg1 ggg3

.
#84
Quote 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?


#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
Last edited by ironman1478 at Mar 2, 2012,
#87
Quote 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.
ggg1 ggg3

.
#88
Quote 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
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.
#89
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
Quote 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:
/**
 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
#91
Quote 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


#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



#include "Sample.h"

#include <iostream>

using namespace std;

void sampleFunction()
{
    cout << "Hello\n";
}

void Sample::foo()
{
    one = CONSTANT;
}



Hope that helps.
#92
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
#94
Quote 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_)' /
\ `-' /
| |---| |
[_] [_]
#95
Quote 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):

#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:

#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

#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:

#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:
#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
#96
Quote by LostLegion
What's the best free 3D program?

Blender.
______________________________________________________________________
Last edited by Gyroscope : Tomorrow at 01:00 PM.
#97
Quote 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
#98
Quote by yaaarp
I just had an aha moment. Thanks guys.


Glad to be of help
#99
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 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 at Mar 4, 2012,
#100
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.
Last edited by NothingRocks at Mar 6, 2012,
#104
I'm currently working on getting the hang of making 2D games in C++ with the win32 API. My biggest achievement so far is that I've made myself an engine that supports pretty much everything I need.

Once I feel that I'm ready to take it to the next level I want to get into DirectX.
You who build these altars now

To sacrifice these children
You must not do it anymore
#105
So I'm getting back into programming C++ and downloaded Visual Studio Express 2010. I made this simple program, but whenever I run it I get the error message

#include <iostream> 
using namespace std;  
int main() { 	
cout<<"Hello World!"<<endl; 	
return 0; 
}


1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup


And I have no idea what that means. Anybody know what's wrong?
#106
Quote by CoreysMonster
So I'm getting back into programming C++ and downloaded Visual Studio Express 2010. I made this simple program, but whenever I run it I get the error message

#include <iostream> 
using namespace std;
int main() {
cout<<"Hello World!"<<endl;
return 0;
}


1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup


And I have no idea what that means. Anybody know what's wrong?
Just tried it out in Code::Blocks and it worked fine

I searched and found this: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/b83e4884-9dec-4d67-ab92-90d5b6c8c51c/
ggg1 ggg3

.
#107
Quote by CoreysMonster
So I'm getting back into programming C++ and downloaded Visual Studio Express 2010. I made this simple program, but whenever I run it I get the error message

#include <iostream> 
using namespace std;
int main() {
cout<<"Hello World!"<<endl;
return 0;
}


1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup


And I have no idea what that means. Anybody know what's wrong?

nothings wrong with it. i tried it in visual studio c++ 2010 and it work. try compiling it under a win32 console application with "empty project" unchecked and uncheck "precompiled header"
Last edited by ironman1478 at Mar 7, 2012,
#108
I've been using rpg maker for around 8 years now...it's a lot of fun. The old versions may snes like games, but I would recommend anyone to try it.
Quote by iantheman
I laughed at someone for breaking his g-string, and got sigged


Quote by Veil Of Osiris

You just made me spit out my Kool-Aid all over my keyboard.


sorry
#109
Quote by ironman1478
nothings wrong with it. i tried it in visual studio c++ 2010 and it work. try compiling it under a win32 console application with "empty project" unchecked and uncheck "precompiled header"

that was the problem, I had created it as a win32 application, not a win32 console application. Thanks!
#110
What compilers do you guys use? I've been using Dev-cpp for a while now and it's my favorite by far.
You who build these altars now

To sacrifice these children
You must not do it anymore
#111
Quote by the bartender
What compilers do you guys use? I've been using Dev-cpp for a while now and it's my favorite by far.

i either use g++ in a unix shell or i use whatever compiler visual studio 2010 uses (got studio for free b/c of school).
#112
Quote by the bartender
What compilers do you guys use? I've been using Dev-cpp for a while now and it's my favorite by far.
GNU GCC Compiler
ggg1 ggg3

.
#113
Quote by ironman1478
nothings wrong with it. i tried it in visual studio c++ 2010 and it work. try compiling it under a win32 console application with "empty project" unchecked and uncheck "precompiled header"


Make empty project, should be very visible in the list under Visual C++. Then make a Main.cpp and you're set. Even works with Win32 programming and whetever external libraries you want to use, like OpenGL, SDL, SFML, ....
#115
Quote by ironman1478
i either use g++ in a unix shell or i use whatever compiler visual studio 2010 uses (got studio for free b/c of school).

Isn't Visual Studio 2010 free anyways? Or is the Express version not the full version?
#117
Quote by CoreysMonster
Isn't Visual Studio 2010 free anyways? Or is the Express version not the full version?
Express version is the free version IIRC
ggg1 ggg3

.
#119
Quote by Vendetta V
did anybody see my post? i'm really looking forward for such an opportunity?

You're offering music services to a bunch of other musicians, you'll have to take into account that most of us prefer to make our own
#120
Quote by the bartender
What compilers do you guys use? I've been using Dev-cpp for a while now and it's my favorite by far.



I use NetBeans as my IDE and such for about everything. Unless I've using Java, which I've started using Eclipse for.