Need some help with C

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
spiroth10
Mental DCEmu
Mental DCEmu
Posts: 304
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Mon Sep 01, 2003 4:57 pm
Has thanked: 0
Been thanked: 0

Need some help with C

Post by spiroth10 »

okay, I'm trying to write a text based adventure game (please, if youve never coded in C before dont go and tell me text based games are easy to program, they're not). well, I have some variables for HP values, and when you get to a battle I have each command set (and I know the commands if statements are OK) to take away a certain amount of one of the variables data (hence, making the enemy lose HP) and I have a while loop that executes the same function if HP is <0. so, I try my game, and it stops, no matter what you choose and goes to the function that it should go it if you win the fight. the code I have there is

if(ogrehp = 0)
{
ogredead = 1
}
if(ogredead = 1)
{
story2();
}

so, I figure the compiler didnt recognise the loop the way I had it, so I set
up two identicle functions and had a loop switch back and forth between each other -- which didn't work either :P. it seems that the hp value is dropping itself to 0 automatically even if you dont touch the variable at all (there's a choice to defend yourself, and it STILL kills the enemy :P lol) and then it exits to the story2() function.

if you need/want to see my source code, I can post it -- just be aware, im not nearly done with it, and there's a lot of stuff that isnt finished and some stuff I need to remove because they do absolutely nothing (there's at least 1 or 2 functions like that) but there shouldnt be any errors (I compiled it with DJGPP in windows). thanks.


----
edit
-----

the ='s wer a typo in my post, not the code... please read my last reply and ignore this. thanks.
Last edited by spiroth10 on Sat Jul 03, 2004 11:32 pm, edited 1 time in total.
Tvspelsfreak
Team Screamcast
Team Screamcast
Posts: 144
Joined: Tue Dec 23, 2003 6:04 pm
Location: Umeå, Sweden
Has thanked: 0
Been thanked: 0
Contact:

Post by Tvspelsfreak »

You have to have double "="s in your if statements:

if(ogrehp == 0)
{
ogredead = 1
}
if(ogredead == 1)
{
story2();
}
https://github.com/tvspelsfreak/texconv - Converts images into any texture format supported on the DC.
OneThirty8
Damn Dirty Ape
Damn Dirty Ape
Posts: 5031
Joined: Thu Nov 07, 2002 11:11 pm
Location: Saugerties, NY
Has thanked: 0
Been thanked: 0

Re: Need some help with C

Post by OneThirty8 »

Code: Select all

if(ogrehp = 0)
{
    ogredead = 1
}
if(ogredead = 1)
{
   story2();
}
The way you have your if statement, it's going to make ogrehp = 0 and then compare, and you want to just compare ogrehp and see whether it's equal to 0 - also, you forgot the semicolon after ogredead = 1. Try this:

Code: Select all

if(ogrehp == 0)
{
    ogredead = 1;
}
if(ogredead == 1)
{
   story2();
}
Not sure if that's where your problem is because I don't know how the rest of your code works, but that will fix the problems I can see.
*edit - Tvspelsfreak posted while I was posting - but at least we said the same thing. :D

*edit 2 - after reading your post more carefully, that's definitely the problem you described. I read the first part, then your code, and skimmed the rest the first time.
Last edited by OneThirty8 on Sat Jul 03, 2004 11:03 pm, edited 1 time in total.
Rev. Layle
Insane DCEmu
Insane DCEmu
Posts: 190
Joined: Sun Jun 27, 2004 8:35 pm
Location: stillwater, ok
Has thanked: 0
Been thanked: 0
Contact:

Post by Rev. Layle »

yup a single = will do an assignment, even in an if statement, and you generally get no compiler warning and definately no compiler error.... and the if statement will pass as long the the assignment is a value other than zero

so, the lesson learned, as tvspelsfreak says, is use "==" for equality comparasions
spiroth10
Mental DCEmu
Mental DCEmu
Posts: 304
Joined: Mon Sep 01, 2003 4:57 pm
Has thanked: 0
Been thanked: 0

Post by spiroth10 »

err no, thats not the problem, I just forgot to add those in my post, theyre there in the code...

to simplify the matter, I'll just post my code... just know that I know it's a mess...

-----------
the code
_________________

#include <stdio.h>
#include <string.h>

int charhp = 500;
int ogrehp = 150;
int goblinhp = 200;
int kinghp = 300;
int demonhp = 350;
int godhp = 400;
int charattack;
int ogredead;
int goblindead;
int kingdead;
int demondead;
int goddead;
int chardead;
int dowhat;
char whatdo[1000] = "1. attack 2. wait until the goblin attacks, then dodge him and stab him in the back 3. defend";
int ogbatt = 0;

ogrecontinue()
{
while(ogrehp < 0) ogre_batt();
printf("\nThe ogre is still alive! what do I do?\n%s",&whatdo);
scanf("%i",&dowhat);
if(dowhat == 1)
{
printf("\nYou slash the ogre with your dagger. The ogre hits you back! Ouch!\n");
charhp - 20;
ogrehp - 50;
}
if(dowhat == 2)
{
printf("\nWOW! Your attack causes mortal damage to the ogre, instantly killing it!!!\n");
ogrehp - 150;
ogredead = 1;
story2();
}
if(dowhat == 3)
{
printf("\nIt's useless! the ogre breaks through you guard and hits you\n");
charhp - 20;
}
if(charhp == 0)
{
printf("\nYou Died... GAME OVER!\n");
}
if(ogrehp == 0)
{
ogredead = 1;
}
if(ogredead == 1)
{
ogbatt = 1;
story2();
}

if(ogbatt == 0)
{
printf("\nThe ogre is still alive! what do I do?\n%s",&whatdo);
}
}
ogre_batt()
{
while(ogrehp < 0) ogrecontinue();
printf("\nThe ogre is still alive! what do I do?\n%s",&whatdo);
scanf("%i",&dowhat);
if(dowhat == 1)
{
printf("\nYou slash the ogre with your dagger. The ogre hits you back! Ouch!\n");
charhp - 20;
ogrehp - 50;
}
if(dowhat == 2)
{
printf("\nWOW! Your attack causes mortal damage to the ogre, instantly killing it!!!\n");
ogrehp - 150;
ogredead = 1;
story2();
}
if(dowhat == 3)
{
printf("\nIt's useless! the ogre breaks through you guard and hits you\n");
charhp - 20;
}
if(charhp == 0)
{
printf("\nYou Died... GAME OVER!\n");
}
if(ogrehp == 0)
{
ogredead == 1;
}
if(ogredead == 1)
{
ogbatt == 1;
story2();
}

if(ogbatt == 0)
{
printf("\nThe ogre is still alive! what do I do?\n%s",&whatdo);
}
}
story2()
{
printf("not done yet in this demo version\n");
}
int funcs_declare()
{
charhp = 500;
ogrehp = 150;
goblinhp = 200;
kinghp = 300;
demonhp = 350;
godhp = 400;
charattack = 50;
if(charhp == 0)
{
chardead = 1;
}
if(ogrehp == 0)
{
ogredead = 1;
}
if(goblinhp == 0)
{
goblindead = 1;
}
if(kinghp == 0)
{
kingdead = 1;
}
if(demonhp == 0)
{
demondead = 1;
}
if(godhp == 0)
{
goddead = 1;
}
if(chardead == 1)
{
printf("\nSorry, you died...\n Game Over, Try Again\n");
}
}

main()
{
funcs_declare();
printf("It is cold and dark everywhere... Not a sole can be seen, at\nleast alive, for more than 50 miles. the only thing visible in the distance is\ndoom castle, home of the dark god and the orb of power. I am here to steal the orb\n and usher in a new age of light for those reighned under the tyranny of the dark god...\n");
printf("Finally, I have made it inside the castle. slowly I walk through the dark\ncorridors with my trusty map in hand...\n......WAIT!!! whats that noise? I see it now, coming straight at me! An ogre is attacking me!\n");
ogre_batt();
}

_______________

yeah, I know it's a mess... and Im sure I'm not doing somehting right here
Image
Rev. Layle
Insane DCEmu
Insane DCEmu
Posts: 190
Joined: Sun Jun 27, 2004 8:35 pm
Location: stillwater, ok
Has thanked: 0
Been thanked: 0
Contact:

Post by Rev. Layle »

well code like:

charhp - 20;
ogrehp - 50;

does abosolutely nothing.... is evaluates the variable minus whatever and discards the result, in fact, some compilers may just simply ignore it

to reduce the amount you have to have to do:
charhp = charhp - 20;

OR (the preferred shortcut way):
charhp -= 20; // -= is the decrement operator, likewise += is an increment operator, can be used with many other opearands, i.e. *= or /= or >>= etc...

-----------------
if(ogredead == 1)
{
ogbatt == 1;
story2();
}

again ogbatt == 1, does nothing, it will perform a comparasion operation and discard the result. if you want to assign a value, use ONE equal sign only

------------------------

printf("\nThe ogre is still alive! what do I do?\n%s",&whatdo);

i dont think that will print out string in whatdo at all, maybe garbage. IIRC, whatdo is an array, which by default, whatdo is already a pointer to the beginning of a array of chars (i.e. a string)... take the & out when you do this operation (alternatively, you can do &(whatdo[0]), but that is redundant)
OneThirty8
Damn Dirty Ape
Damn Dirty Ape
Posts: 5031
Joined: Thu Nov 07, 2002 11:11 pm
Location: Saugerties, NY
Has thanked: 0
Been thanked: 0

Post by OneThirty8 »

First, just a suggestion, but when posting code, it's a good idea to use the code tags so that you don't lose your formatting. ( [ code ] without spaces, and [ /code ] without the spaces)

Now, to your code...
Upon first glance, I'd say that in this function:

Code: Select all

ogrecontinue()
I believe this code here is a problem.

Code: Select all

  while(ogrehp < 0) ogre_batt(); 
You already have a 'while' that calls that function in your ogre_batt() loop. Basically, you're going to get to that part, and if the condition is met to call ogre_continue, then ogre_batt is going to be called. Then, in that function, you're checking for the same condition and if that's true, it's going to call ogre_batt. It'll just get stuck in an endless (and useless) loop if ogrehp < 0.

*edit - a couple of things about this bit here: (look for my comments)

Code: Select all

int funcs_declare() 
 { 
      charhp = 500; 
      ogrehp = 150; 
      goblinhp = 200; 
      kinghp = 300; 
      demonhp = 350; 
      godhp = 400; 
      charattack = 50; 
      if(charhp == 0)  // This can't be true - above, you set this to 500.
      { 
            chardead = 1; 
      } 
      if(ogrehp == 0)  // This won't be true - above, it's set to 150.
      { 
            ogredead = 1; 
      } 
      if(goblinhp == 0) //Again - you said goblinhp = 500.
      { 
            goblindead = 1; 
      } 
      if(kinghp == 0) //... will always be 300...
      { 
            kingdead = 1; 
      } 
      if(demonhp == 0) // is always 350
      { 
            demondead = 1; 
      } 
      if(godhp == 0) // is 400.
      { 
            goddead = 1; 
      } 
      if(chardead == 1) // this could possibly be true.
      { 
           printf("\nSorry, you died...\n Game Over, Try Again\n"); 
      }  
/* you'll probably get a compiler warning here - 'int' functions need a return value.  Either return 0 (or whatever you want), or make this whole function void. */
 } 
  

What I would do is make one thing to set the various values to what you want them to start at, and then another function that will check the values after they've changed - basically, just split off the top part from the part where I started making comments.
spiroth10
Mental DCEmu
Mental DCEmu
Posts: 304
Joined: Mon Sep 01, 2003 4:57 pm
Has thanked: 0
Been thanked: 0

Post by spiroth10 »

no... once ogrehp becomes 0 or a negative # the loop ends... it could take a million turns if you just defend for, like, 20 times. remember, each function is an exact replica of the other, and each has a

Code: Select all

   if(ogrehp == 0)
      {
            ogredead = 1
       }
    if(ogredead == 1)
      {
              story2();
       }
now, if i'm correct that should end the loop after the ogrehp becomes 0. the way I coded it, there's no way to go below 0 and you either have to make charhp 0 or ogrehp 0 (or kill or be killed for the gamer) before the battle loop ends... although there's really no worry about that, as if it DOES become an endless loop, I'll discard that while loop anyway... I just put it there because the user may not have killed the thing by the next turn, and I figured that the ogre_batt() function was over and I needed to continue the loop afterwards. It's a primitive battle system, but hey, it's the first REAL app that I've made (other than a calculator... hey thats a big accomplishment, considering I did the the day I started to learn C, and some visual basic apps) thanks for all the help!

edit:
BTW, that int function is the function I said I had to do away with... I have everything set up at the top of the code with the variables now, if you check, and I'm just going to do each battle by hand, because it'd just get too confusing to read if I use only variables.

edit again:
now that I look at it, I had some useful stuff there, maybe I will use it after all...
Rev. Layle
Insane DCEmu
Insane DCEmu
Posts: 190
Joined: Sun Jun 27, 2004 8:35 pm
Location: stillwater, ok
Has thanked: 0
Been thanked: 0
Contact:

Post by Rev. Layle »

yes but "while(ogrehp < 0) ogre_batt();"

only loops while ogrehp is 0 or LESS than zero

perhaps you mean "while(ogrehp > 0) ogre_batt();" instead?
spiroth10
Mental DCEmu
Mental DCEmu
Posts: 304
Joined: Mon Sep 01, 2003 4:57 pm
Has thanked: 0
Been thanked: 0

Post by spiroth10 »

hey... now that you mention it... I MIXED UP THE SIGNS :P ROFLOL :oops: :oops: :o
oops... :roll: well, Id better fix that, too. now, onto the rest of the game... if I ever finish it...
Image
OneThirty8
Damn Dirty Ape
Damn Dirty Ape
Posts: 5031
Joined: Thu Nov 07, 2002 11:11 pm
Location: Saugerties, NY
Has thanked: 0
Been thanked: 0

Post by OneThirty8 »

This is one function:

Code: Select all

int funcs_declare() 
 { 
      charhp = 500; 
      ogrehp = 150; 
      goblinhp = 200; 
      kinghp = 300; 
      demonhp = 350; 
      godhp = 400; 
      charattack = 50; 
      if(charhp == 0) 
      { 
            chardead = 1; 
      } 
      if(ogrehp == 0) 
      { 
            ogredead = 1; 
      } 
      if(goblinhp == 0) 
      { 
            goblindead = 1; 
      } 
      if(kinghp == 0) 
      { 
            kingdead = 1; 
      } 
      if(demonhp == 0) 
      { 
            demondead = 1; 
      } 
      if(godhp == 0) 
      { 
            goddead = 1; 
      } 
      if(chardead == 1) 
      { 
           printf("\nSorry, you died...\n Game Over, Try Again\n"); 
      }  
 } 
Each time it's called it will go through it once.

Code: Select all

int funcs_declare() 
 { 
      charhp = 500; 
      ogrehp = 150; 
      goblinhp = 200; 
      kinghp = 300; 
      demonhp = 350; 
      godhp = 400; 
      charattack = 50; 
above, you've set all the integers to have values other than 0. Below, it will check each to see if it's 0, which it can't be because each time this loop is called, it will set those integers to 500, 150, 200, 300, 350, and 400. You'll want to end one function here, and call that each time you want to reset those values. Then, start a new function here to check the values every time you want to check them, and include the following code.

Code: Select all

      if(charhp == 0) 
      { 
            chardead = 1; 
      } 
      if(ogrehp == 0) 
      { 
            ogredead = 1; 
      } 
      if(goblinhp == 0) 
      { 
            goblindead = 1; 
      } 
      if(kinghp == 0) 
      { 
            kingdead = 1; 
      } 
      if(demonhp == 0) 
      { 
            demondead = 1; 
      } 
      if(godhp == 0) 
      { 
            goddead = 1; 
      } 
      if(chardead == 1) 
      { 
           printf("\nSorry, you died...\n Game Over, Try Again\n"); 
      }  
 }
Ian Micheal
Soul Sold for DCEmu
Soul Sold for DCEmu
Posts: 4865
Joined: Fri Jul 11, 2003 9:56 pm
Has thanked: 2 times
Been thanked: 4 times

Post by Ian Micheal »

Here is the complete fixed version could still be bugs only spent 10 mins on it works now.

Code: Select all


#include <stdio.h> 
#include <string.h> 

int charhp = 500; 
int ogrehp = 150; 
int goblinhp = 200; 
int kinghp = 300; 
int demonhp = 350; 
int godhp = 400; 
int charattack; 
int ogredead; 
int goblindead; 
int kingdead; 
int demondead; 
int goddead; 
int chardead; 
int dowhat; 
char whatdo[1000] = "1. attack 2. wait until the goblin attacks, then dodge him and stab him in the back 3. defend"; 
int ogbatt = 0; 

ogrecontinue() 
{ 
while(ogrehp < 0) ogre_batt();
printf("\nThe ogre is still alive! what do I do?\n%s",&whatdo); 
scanf("%i",&dowhat); 
if(dowhat == 1) 
{ 
printf("\nYou slash the ogre with your dagger. The ogre hits you back! Ouch!\n"); 
charhp = charhp - 20;
ogrehp = ogrehp - 50; 
} 
if(dowhat == 2) 
{ 
printf("\nWOW! Your attack causes mortal damage to the ogre, instantly killing it!!!\n"); 
ogrehp = ogrehp - 150; 
ogredead = 1; 
story2(); 
} 
if(dowhat == 3) 
{ 
printf("\nIt's useless! the ogre breaks through you guard and hits you\n"); 
charhp = charhp - 20; 
} 
if(charhp == 0) 
{ 
printf("\nYou Died... GAME OVER!\n"); 
} 
if(ogrehp == 0) 
{ 
ogredead = 1; 
} 
if(ogredead == 1) 
{ 
ogbatt = 1; 
story2(); 
} 

if(ogbatt == 0) 
{ 
printf("\nThe ogre is still alive! what do I do?\n%s",&whatdo); 
} 
} 
ogre_batt() 
{ 
while(ogrehp > 0) ogrecontinue(); 
printf("\nThe ogre is still alive! what do I do?\n%s",&whatdo); 
scanf("%i",&dowhat); 
if(dowhat == 1) 
{ 
printf("\nYou slash the ogre with your dagger. The ogre hits you back! Ouch!\n"); 
charhp = charhp  - 20; 
ogrehp = ogrehp - 50; 
} 
if(dowhat == 2) 
{ 
printf("\nWOW! Your attack causes mortal damage to the ogre, instantly killing it!!!\n"); 
ogrehp = ogrehp - 150; 
ogredead = ogredead = 1; 
story2(); 
} 
if(dowhat == 3) 
{ 
printf("\nIt's useless! the ogre breaks through you guard and hits you\n"); 
charhp - 20; 
} 
if(charhp == 0) 
{ 
printf("\nYou Died... GAME OVER!\n"); 
} 
if(ogrehp == 0) 
{ 
ogredead == 1; 
} 
if(ogredead == 1) 
{ 
ogbatt == 1; 
story2(); 
} 

if(ogbatt == 0) 
{ 
printf("\nThe ogre is still alive! what do I do?\n%s"); 
} 
} 
story2() 
{ 
printf("not done yet in this demo version\n"); 
} 
 
int funcs_declare() 
{ 
 
      if(charhp == 0)  
      { 
            chardead = 1; 
      } 
      if(ogrehp == 0)  
      { 
            ogredead = 1; 
      } 
      if(goblinhp == 0)  
      { 
            goblindead = 1; 
      } 
      if(kinghp == 0) 
      { 
            kingdead = 1; 
      } 
      if(demonhp == 0) 
      { 
            demondead = 1; 
      } 
      if(godhp == 0)  
      { 
            goddead = 1; 
      } 
      if(chardead == 1) 
      { 
           printf("\nSorry, you died...\n Game Over, Try Again\n"); 

      }  
/* you'll probably get a compiler warning here - 'int' functions need a return value.  Either return 0 (or whatever you want), or make this whole function void. */ 
} 

main() 
{ 
funcs_declare(); 
printf("It is cold and dark everywhere... Not a sole can be seen, at\nleast alive, for more than 50 miles. the only thing visible in the distance is\ndoom castle, home of the dark god and the orb of power. I am here to steal the orb\n and usher in a new age of light for those reighned under the tyranny of the dark god...\n"); 
printf("Finally, I have made it inside the castle. slowly I walk through the dark\ncorridors with my trusty map in hand...\n......WAIT!!! whats that noise? I see it now, coming straight at me! An ogre is attacking me!\n"); 
ogre_batt(); 
} 
Dreamcast forever!!!
Ian Micheal
Soul Sold for DCEmu
Soul Sold for DCEmu
Posts: 4865
Joined: Fri Jul 11, 2003 9:56 pm
Has thanked: 2 times
Been thanked: 4 times

Post by Ian Micheal »

|Intresting if some one could port this to dreamcast for him using the nice example kamjin did char one.

I dont think i can do it but i know it would be a good step just would not know were to start replacing scanf and char example.

but just a thought.
Dreamcast forever!!!
Kamjin
DC Developer
DC Developer
Posts: 216
Joined: Wed Dec 17, 2003 5:27 am
Has thanked: 0
Been thanked: 0

Post by Kamjin »

That Scanf can be so iritating at times.. I've got too much older
code that depends on scanf, and fscanf.. always trying to work
around it..
but I think we should leave him figure that out on on his own :wink: ,
I purposely left out a text parser in the demo code..
but a good hint for spiroth.. think how it would be done in basic.
and litterally mimic it in really simple C..
Post Reply