Game Programming and Development Tools

C – gaurdianAQ

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
I discovered how easy C is!!!!! It seemed so hard before but after reading a bit of game programming for teens and learning some of the concepts of game design it seems so much easier!
samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
C is like English. Easy to start, hard to master. Not to be discouraging though! Keep with it and you'll do well!

------------------
Sam Washburn

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
how do I do it so that I can handle strings I read a tutorial on how to handle integers but not strings here is the code they use,
#include <stdio.h>

int main()
{
int this_is_a_number;

printf( "Please enter a number: " );
scanf( "%d", &this_is_a_number );
printf( "You entered %d", this_is_a_number );
getchar();
}

how do I use that to handle strings I tryed everything that I could think of
jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
this is just off the top of my head but did you try "%s" instead of "%d" and give it a char pointer?
(someone else can correct me if i'm wrong, its been a while since i've coded C)
gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
that would make a difference? wow I did not know that
gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
I got an error when I used that here is what I have now
#include <stdio.h>

char main()
{
char this_is_a_word;
printf( "Please enter your name: " );
scanf("%s", &this_is_a_word );
printf( "You entered %s", this_is_a_word );
getchar();
}


samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
a char is a character.. not a string.

Strings in plain c are a bit of a pain, since to do it right you have to actually allocate memory for your string. Strings are definitely harder in c than in most other langs.

Check out this tutorial though:

http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/strings.html

------------------
Sam Washburn

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
well actually I have a c++ compiler so maybe you could tell me how to do it there?
spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
if you have a c++ ide then it's most likely that you have a c compiler too typicaly you have 2 compilers and as for strings i know that most people support the c++ string class but i think it's good to know the c style strings too,just practice with them and you'll do good.

------------------
Matthew(22:36-40)"Teacher, which is the greatest commandment in the Law?" Jesus replied: " 'Love the Lord your God with all your heart and with all your soul and with all your mind. This is the first and greatest commandment. And the second is like it: 'Love your neighbor as yourself.All the Law and the Prophets hang on these two commandments."
Whose Son Is the Christ

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
you're on the right track, only you're using a char instead of a char*

i'm really rusty with c (and i've been using java for a while now) but try:
char input [10];
scanf("%s", input );

it'll accept up to 9 characters

[This message has been edited by jestermax (edited January 26, 2007).]

spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
yeah the thing with c style strings is when you say char *c; you are just making a pointer but you haven't initialized it yet,but when you say char c[10]; you have made a pointer to a char array and initialized it to the size of 10 chars,usually if i don't know the size of the array at compile time what i do is allocate memory at runtime like
int size;
char *c;
cin>>size;
c=new char[size];


or something like that but i think it's too early for you to learn dynamic memory allocation.

------------------
Matthew(22:36-40)"Teacher, which is the greatest commandment in the Law?" Jesus replied: " 'Love the Lord your God with all your heart and with all your soul and with all your mind. This is the first and greatest commandment. And the second is like it: 'Love your neighbor as yourself.All the Law and the Prophets hang on these two commandments."
Whose Son Is the Christ

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
ok so how do i use it so that when using the if statement I can check to see whether a certain character has been inputed because the tutorials I have been using have only taught me how to use numbers?
jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
well first of all when i talk about strings with C/C++ i mean an array of characters that is terminated (ending with) a null character ('\0'). the std:string class is considered a "string object".

well you use a scanf to get the input from the user to start it. then you'll have your string populated with the data so you're all set to do some comparisons.
now you can't use the equals operated for it, because it'll just check if the string has the same memory location as the text you want to check for.
so you have to use a comparison function provided by the C library.

heres your include for the header file:
#include <string.h>
note that when using C++ standard libraries, don't put in the ".h".

so heres some code:


if( strcmp( string, "compare" ) == 0 )
printf( "strings are equal" );

if you want some more info on it then just google "strcmp" and you'll find a ton of good info on it (and other string functions)

spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
if you are talking about comparing a single char you can do like:


#include<iostream.h>
int main(){
char c;
cout<<"enter char:\n";
cin>>c;
if(c=='a'){
cout<<"you entered small letter a\n";
}
else{
cout<<"you didn't enter small letter a\n";
}
return 0;
}

if you are talking about comparing a whole string:


#include<iostream.h>
#include<string.h>
int main(){
char pass[32];
cout<<"Enter password\n";

while(1==1){
cin>>pass;
if(strcmp(pass,"Spade_89")==0){
cout<<"You entered the correct password\n";
break;
}
cout<<"You entered incorrect password\n";
}

return 0;
}

That means you use the strcmp function,you pass it 2 strings as an argument if the are equal it returns 0,if the first one is greater than the second it returns 1 if the second one is greater it returns -1.

notice that you also have to include string.h.

------------------
Matthew(22:36-40)"Teacher, which is the greatest commandment in the Law?" Jesus replied: " 'Love the Lord your God with all your heart and with all your soul and with all your mind. This is the first and greatest commandment. And the second is like it: 'Love your neighbor as yourself.All the Law and the Prophets hang on these two commandments."
Whose Son Is the Christ

[This message has been edited by spade89 (edited January 26, 2007).]

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
in your code you have a while loop iterating forever if you didn't guess the right password.

EDIT: his way of getting input is the C++ way which is a good way, but the header should be <string> as opposed to <string.h> (standards)

[This message has been edited by jestermax (edited January 26, 2007).]

spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
lol,we replied at the same time, but if you are serious about c style strings you better practice a lot on pointers first.

------------------
Matthew(22:36-40)"Teacher, which is the greatest commandment in the Law?" Jesus replied: " 'Love the Lord your God with all your heart and with all your soul and with all your mind. This is the first and greatest commandment. And the second is like it: 'Love your neighbor as yourself.All the Law and the Prophets hang on these two commandments."
Whose Son Is the Christ

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
thank you this is just what I wanted to know
samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
Spade, please be careful with the code you post. I'm getting the feeling you just fling it in there and not really check it over. I don't meant to burn ya. I'm only saying this to help the learners.

------------------
Sam Washburn

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
you can do some fun things with pointers but just don't try to be a hero with it. try to avoid pointers because even pros forget to deallocate memory or anything else from a huge list of things that can go wrong.

Like spades said, if you declare a pointer, it's not allowed to anything. So unless you specifically need one, use an object or array of objects.
Example:
char* stringForHoldingJunk;
char stringForHoldingJunk2 [100];
out of these 2, the second one is actually a string.

however you can use it as you would a pointer; you just reference the first location:
stringForHoldingJunk = stringForHoldingJunk2;
that sets the pointer to point to the character array. So from there you can use he same array operations (like the [] operator).

Anyways, just a reminder

spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
i definetly didn't check the code i just wrote it up, i meant to put the cin in the while loop,i was in a kind of hurry when i wrote that code.i should edit it.

------------------
Matthew(22:36-40)"Teacher, which is the greatest commandment in the Law?" Jesus replied: " 'Love the Lord your God with all your heart and with all your soul and with all your mind. This is the first and greatest commandment. And the second is like it: 'Love your neighbor as yourself.All the Law and the Prophets hang on these two commandments."
Whose Son Is the Christ

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
if ( answer == 'a' || 'A' ) {
score = score + 1;
cout<< "Correct! You now how a total score of " << score << "!\n";
}
if ( answer == 'b' || 'B' ) {
cout<< "Incorrect Paladins are not even in Guild Wars!";
}
if ( answer == 'c' || 'C' ) {
cout<< "Incorrect Evan does not have Nightfall therefore he cannot use Dervishes!";
}
if ( answer == 'd' || 'D' ) {
cout<< "Incorrect you can only have 1 primary profession in Guild Wars!";
}
what is wrong with those if statements? cause in the rest of my program it is supposed to be if you press a different button then it gives you a different answer but all the answers keep appearing on the screen instead of just one of them? why is that
jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
A) you have to have a comparison for each case, you can't say:
if( i = 1 || 2 )
because you're not comparing i to 2, you're just saying if "2 == true" which will always translate to true unless it's zero.
B) if you want a list of cases based on the key pressed then either use an "if-then-else if" structure or a switch statement.
gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
huh but I thought it was like this if answer = a or A so should it not work whether it was either one of those rather than showing everything from b c and d
jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
Well in C, i'm pretty sure that the "||" isn't a short circuit one, as "|" is a bitwise operator. So it'll compare say i to 'a' (and return true or false) and since that's not true it'll see if 'A' is true. 'A' is equal to 65 so 65 should be true since it's not 0 (someone can correct me if i'm wrong).

you have a series of "IF" statements so it'll check each one of your cases to see if it's true, so each will evaluate to true and the code blocks will be executed.
try this:

if ( answer == 'a' || answer == 'A' ) {
score = score + 1;
cout<< "Correct! You now how a total score of " << score << "!\n";
}else if ( answer == 'b' || answer == 'B' ) {
cout<< "Incorrect Paladins are not even in Guild Wars!";
}

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
thank you I will try that
SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
Oh, how I WISH you could do what you are asking. But you need to spell out each 'if' separately, how Jestermax shows in his example.

There is something called 'order of precedence'. This defines the order in which things are considered/calculated. Take 2 + 2 * 3, for example. Multiplication has precedence over the addition, so the answer is 8, not 12.

In the statement 'if a equals 1 or 2', the '1 or 2' is considered first. This evalutes to true (1). Then it compares the a to 1. Like Jestermax, the only way to do what you want is to say:

if ( (a == 1) || (a == 2) )

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
oh yeah, as the square did, when in doubt put brackets around your comparisons:
if( (i == 1 ) || (i == 2) ){..}
it just saves a ton of debug time when you try to solve logic errors
SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
Thanks, Jestermax.

I probably overdo my code, but I have found it cleaner and easier to read and understand. I try to separate out precedence so the reader does not have to figure it out. It becomes especially difficult to read statements with multiple &&'s and ||'s when there are no helpful indications of what you are really trying to do.

On the bigger, more confusing statements, I will try to add a comment describing things as well.