Thread: Text Adventure Needs A Small Fix.

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    2

    Text Adventure Needs A Small Fix.

    Hey, I've been working on writing a text adventure in c as a bit of a side project from a basic programming course I took last year. We worked in the cs50 environment and everything worke fine until I tried moving it out into just plain c. Because of this move, the game is no longer functional due to the fact that C does not actually contain a string as a variable. As far as I can tell, this lack of the string variable is the only thing I'm missing.

    To help decipher my code, here is a summary of the mechanics of the game:

    1.)console prints initial scenario containing choices to move to the next scene

    2.) A string is declared and its content determined my user input

    3.) the user input is compared to the strings relatinbg to the choices provided by the initial and subsequent scenarios. (I didn't add a spell check because I never though about it at the time and am too lazy to go back through and fix it.)

    4.)lastly, based on the input the user gives, a new scenario is printed and the process repeats.

    Heres the code for the game:
    Code:
    #include <cs50.h>
    #include <string.h>
    #include <stdio.h>
    int main (void)
    {
        char* choice1;
        printf("\nYou find yourself at the steps of a mysterious mansion.\n There is a forest to the left,\n the house is in front of you,\n there is an old barn to the right, \n and there is a cave behind you.\n Do you:\n go left,\n go right,\n or turn around?\n");
        
        choice1=GetString(void);
        if ( strcmp( choice1, "go right") ==0)
        {printf("\nYou wander into an old, mysterious barn and\nyou hear a soft creaking in the loft followed by heavy footsteps.\n Do you:\nhide in the hay,\n go to the noise,\n or look up?\n");}
        
        string choiceb1;
        choiceb1=GetString(void);
        if (strcmp(choiceb1, "look up") ==0)
        {printf("\nA shadowy figure descends from the roof\n and lands behind you.\n it quickly knocks you unconcious and\n feeds you to the murder cows.\nGAME OVER\n");}
        if (strcmp(choiceb1, "go to the noise") ==0)
        {printf("\nA figure jumps out behind you and snaps your neck.\n GAME OVER\n");}
        if (strcmp( choiceb1, "hide in the hay") ==0)
        {printf("\nYou dive into the hay and as you feel around,\n you find an old, rusty revolver.\n Do you:\n keep it\n or leave it?\n");}
        
        string choiceb2;
        choiceb2=GetString();
        if (strcmp(choiceb2, "leave it") ==0)
        {printf("\nYou toss the gun aside,\n and as it hits the ground it misfires and kills you.\n GAME OVER\n");}
        if (strcmp(choiceb2, "keep it") ==0)
        {printf("\nAs you further inspect the weapon,\n you see it only has one bullet.\n As you notice this, a dark figure descends from the loft and goes back up.\n Do you:\n leave\n or follow him?\n");}
        
        string choiceb3;
        choiceb3=GetString();
        if (strcmp(choiceb3, "leave") ==0)
        {printf("\nYou step on a twig on the way out,\n the figure turns around,\n jumps on your chest,\n and dismembers you.\nGAME OVER");}
        if (strcmp(choiceb3, "follow him") ==0)
        {printf("\nYou follow the figure and watch as it feeds the murder cows.\n He could be friendly and helpful,\n or hostile and kill you.\n Do you take the shot?\n<yes/no>\n");}
        
        string choiceb4;
        choiceb4=GetString();
        if (strcmp(choiceb4, "no") ==0)
        {printf("\nAs you watch,\n the figure catches you out of the corner of its eye.\n Before you realise this,\n it jumps on you\n and rips your face off.\n GAME OVER\n");}
        if (strcmp(choiceb4, "yes") ==0)
        {printf("\nYou shoot the figure and as he falls,\n he drops a sling.\n Do you take it?\n <yes/no>\n");}
        
        string choiceb5;
        choiceb5=GetString();
        if (strcmp(choiceb5, "no") ==0)
        {printf("\nAs you leave the barn,\n the murder cows escape and devour you.\n GAME OVER\n");}
        if (strcmp(choiceb5, "yes") ==0)
        {printf("\nYou take the sling and save it for later use.\n You are now back at the house Do you: go left\n or turn around?\n");}
        
        string choicebh;
        choicebh=GetString();
        if ( strcmp( choicebh, "turn around") ==0)
        {printf("\nYou turn around and enter the cave\n Do you:\n search the cave\n or go deeper?\n ");}
        
        string choicebc1;
        choicebc1=GetString();
        if (strcmp(choicebc1, "go deeper") ==0)
        {printf("\nYou are confronted by a prancer.\n You have nothing to defend yourself with.\n As you stand there defenceless and paralyzed by fear,\n the prancer devours you.\nGAME OVER\n");}
        if (strcmp(choicebc1, "search the cave") ==0)
        {printf("\nYou search the cave and pick up a few rocks before proceeding\n As you venture deeper into the cave,\n you come across a prancer.\n Do you:\n go for his head\n or go for his arms?\n");}
            
        string choicebc2;
        choicebc2=GetString();
        if (strcmp(choicebc2, "go for his arms") ==0)
        {printf("\nYou charge at his arms and break one,\n but as soon as you do,\n he grabs you with his other three,\nand he dismembers you.\n GAME OVER\n");}
        if (strcmp(choicebc2, "go for his head") ==0)
        {printf("\nYou throw a pebble at the prancer's arm to distract it,\n then hurl a large rock at it's face.\n It is now stunned. Do you:\n sneak by\n leave\n or attack it?\n");}
            
        string choicebc3;
        choicebc3=GetString();
        if (strcmp(choicebc3, "sneak by") ==0)
        {printf("\nAs you try to sneak by,\n the prancer regains conciousness and grabs you.\n Soon after, he dismembers you and eats you.\n GAME OVER\n");}
        if (strcmp(choicebc3, "leave") ==0)
        {printf("\nThe prancer regains its bearings,\n and it catches you on your way out.\n It then quickly snaps your neck.\n GAME OVER\n");}
        if (strcmp(choicebc3, "attack it") ==0)
        {printf("\nWhile it is stunned, you wall jump and snap its neck.\n As you venture deeper into the cave,\n you can hear the dead body spassaming on the floor.\n You then meet an old man who gives you a sword.\n You are back at the house.\n Yor last path is the forest.\n Type 'go left' to enter the forest.\n");}
        
        string choicebcf;
        choicebcf=GetString();
        if ( strcmp( choicebcf, "go left") ==0)
        {printf("\nYou enter a dark and mysterious forest\n and travel across the road until you come upon a fork in the road.\n Do you:\n go left\n or go right?\n");}
                
        string choicebcf1;
        choicebcf1=GetString();
        if ( strcmp(choicebcf1, "go right") ==0)
        {printf("\nYou travel along the road and cross paths with 'The Abyss'.\n Do you: sling a rock at it\n or slash its arms?\n");}
                
        string choicebcf2;
        choicebcf2=GetString();
        if (strcmp(choicebcf2, "sling a rock at it") ==0)
        {printf("\nYou sling a rock at 'The Abyss' and it quickly envelops the stone and ejects it\n in your direction.\n You were not expecting this, so as you are caught off guard,\n the rock smashes your face, killing you.\nGAME OVER\n");}
        if (strcmp(choicebcf2, "slash its arms") ==0)
        {printf("\nYou lunge towards 'The Abyss',\n and as you do, you make a swift slice which cuts off the arms of 'The Abyss'.\n After this, seing you have the upper hand,\n you procede hacking and slicing until the foul abomination is vanquished.\n You continue on the road until you find a chest, but it is locked.\n Because of this, you go along the left path until you meet a prowler.\n Do you:\n sling a rock\n or engage it with the sword?\n");} 
                
        string choicebcf3;
        choicebcf3=GetString();
        if (strcmp(choicebcf3, "sling a rock") ==0)
        {printf("\nYou sling a rock at the prowler with great effort,\n but alas, it was to no avail.\n The rock dinks off of the prowler's thick armor-like flesh,\n and then the prowler stabs you with its arm.\n GAME OVER\n");}
        if (strcmp(choicebcf3, "engage it with the sword") ==0)
        {printf("\nThe prowler is caught off guard by your bold move to charge forward and attack.\n During that brief moment, you land a graceful blow to the prowler's neck.\n This, naturally, removes the prowler's head from its body.\n You take a look at the head and figure it could be helpful later.\n Do you: keep it\n or leave it?\n");}
                
        string choicebcf4;
        choicebcf4=GetString();
        if (strcmp(choicebcf4, "keep it") ==0)
        {printf("\nAs you reach down to grab the disembodied head it bites your hand\n and continues to chomp its way down your arm until it bites your neck, killing you.\n GAME OVER\n");}
        if (strcmp(choicebcf4, "leave it") ==0)
        {printf("\nYou leave the head where it is and journey on until you find a crate.\n You break it open and find a key.\n You then take that key and open the locked chest to find a skull shaped key.\n You then take that key and put it into the doors at the mansion.\n The door unlocks and you open the door and stare inside.\n Von Carlo welcomes you and commends you for your bravery.\n All is well, but he says you must make a choice.\n You must chose to: become the new caretaker\n or leave.\n Which do you do?\n");}
           
        string choicembcf;
        choicembcf=GetString();
        if (strcmp(choicembcf, "leave") ==0)
        {printf("\nYou leave the mansion, and Von Carlo shoots you on your way out.\n As you fall to the floor, he cackles loudly then walks away muttering,\n 'If only he were wise. If only he were wise'\n GAME OVER\n");}
        if (strcmp(choicembcf, "become the new caretaker") ==0)
        {printf("\nVon Carlo vanishes and you are left with nothing but the deed to the mansion.\n You live out the rest of your days waiting for the next adventurer\n to take on the challenge of VON CARLO'S MANSION.\n YOU WIN!\n");}
        
        if (strcmp(choicebcf1, "go left") ==0)
        {printf("\nYou go along the left path until you meet a prowler.\n Do you:\n sling a rock\n or engage it with the sword?\n");} 
                
        string choicebcfl1;
        choicebcfl1=GetString();
        if (strcmp(choicebcfl1, "sling a rock") ==0)
        {printf("\nYou sling a rock at the prowler with great effort,\n but alas, it was to no avail.\n The rock dinks off of the prowler's thick armor-like flesh,\n and then the prowler stabs you with its arm.\n GAME OVER\n");}
        if (strcmp(choicebcfl1, "engage it with the sword") ==0)
        {printf("\nThe prowler is caught off guard by your bold move to charge forward and attack.\n During that brief moment, you land a graceful blow to the prowler's neck.\n This, naturally, removes the prowler's head from its body.\n You take a look at the head and figure it could be helpful later.\n Do you: keep it\n or leave it?\n");}
                
        string choicebcfl2;
        choicebcfl2=GetString();
        if (strcmp(choicebcfl2, "keep it") ==0)
        {printf("\nAs you reach down to grab the disembodied head it bites your hand\n and continues to chomp its way down your arm until it bites your neck, killing you.\n GAME OVER\n");}
        if (strcmp(choicebcfl2, "leave it") ==0)
        {printf("\nYou leave the head where it is and journey on until you find a crate.\n You break it open and find a key.\n You then turn back and go along the right path where you encounter 'TheAbyss'\n Do you: sling a rock at it\n or slash its arms?\n");}
                
        string choicebcfl3;
        choicebcfl3=GetString();
        if (strcmp(choicebcfl3, "sling a rock at it") ==0)
        {printf("\nYou sling a rock at 'The Abyss' and it quickly envelops the stone and ejects it\n in your direction.\n You were not expecting this, so as you are caught off guard,\n the rock smashes your face, killing you.\nGAME OVER\n");}
        if (strcmp(choicebcfl3, "slash its arms") ==0)
        {printf("\nYou lunge towards 'The Abyss',\n and as you do, you make a swift slice which cuts off the arms of 'The Abyss'.\n After this, seing you have the upper hand,\n you procede hacking and slicing until the foul abomination is vanquished.\n You continue on the road until you find a chest, and unlock it with your key.\n You then take that key and insert it into the mansion doors.\nThe door unlocks and you open the door and stare inside.\n Von Carlo welcomes you and commends you for your bravery.\n All is well, but he says you must make a choice.\n You must chose to:\n become the new caretaker\n or leave.\n Which do you do?\n");}
                
        string choicembcfl;
        choicembcfl=GetString();
        if (strcmp(choicembcfl, "leave") ==0)
        {printf("\nYou leave the mansion, and Von Carlo shoots you on your way out.\n As you fall to the floor, he cackles loudly then walks away muttering,\n 'If only he were wise. If only he were wise'\n GAME OVER\n");}
        if (strcmp(choicembcfl, "become the new caretaker") ==0)
        {printf("\nVon Carlo vanishes and you are left with nothing but the deed to the mansion.\n You live out the rest of your days waiting for the next adventurer\n to take on the challenge of VON CARLO'S MANSION.\n YOU WIN!\n");}
              
        
        if (strcmp(choicebh, "go left") ==0)
        {printf("\nYou go left and enter the forest.\n A mutant rabid raccoon breaks your legs and bites you.\n You sit there paralyzed and die from the rabies you just received from the raccoon.\n");}      
                
                
            if ( strcmp( choice1, "turn around") ==0)
            {printf("\nYou turn around and enter the cave\n Do you:\n search the cave\n or go deeper?\n");}
        
            string choicec1;
            choicec1=GetString();
            if (strcmp(choicec1, "go deeper") ==0)
            {printf("\nYou are confronted by a prancer.\n You have nothing to defend yourself with.\n As you stand there defenceless and paralyzed by fear,\n the prancer devours you.\nGAME OVER\n");}
            if (strcmp(choicec1, "search the cave") ==0)
            {printf("\nYou search the cave and pick up a few rocks before proceeding\n As you venture deeper into the cave,\n you come across a prancer.\n Do you:\n go for his head\n or go for his arms?\n");}
            
            string choicec2;
            choicec2=GetString();
            if (strcmp(choicec2, "go for his arms") ==0)
            {printf("\nYou charge at his arms and break one,\n but as soon as you do,\n he grabs you with his other three,\nand he dismembers you.\n GAME OVER\n");}
            if (strcmp(choicec2, "go for his head") ==0)
            {printf("\nYou throw a pebble at the prancer's arm to distract it,\n then hurl a large rock at it's face.\n It is now stunned. Do you:\n sneak by\n leave\n or attack it?\n");}
            
            string choicec3;
            choicec3=GetString();
            if (strcmp(choicec3, "sneak by") ==0)
            {printf("\nAs you try to sneak by,\n the prancer regains conciousness and grabs you.\n Soon after, he dismembers you and eats you.\n GAME OVER\n");}
            if (strcmp(choicec3, "leave") ==0)
            {printf("\nThe prancer regains its bearings,\n and it catches you on your way out.\n It then quickly snaps your neck.\n GAME OVER\n");}
            if (strcmp(choicec3, "attack it") ==0)
            {printf("\nWhile it is stunned, you wall jump and snap its neck.\n As you venture deeper into the cave,\n you can hear the dead body spasming on the floor.\n You then meet an old man who gives you a sword.\n After this, you return to the house.\n Do you:\n go left\n or go right?\n");}
            
            string choicech;
            choicech=GetString();
            if (strcmp(choicech, "go left") ==0)
            {printf("\nYou go left and enter the forest.\n A mutant rabid raccoon breaks your legs and bites you.\n You sit there paralyzed and die from the rabies you just received from the raccoon.\nGAME OVER\n");}
            if (strcmp(choicech, "go right") ==0)
            {printf("\nYou wander into an old, mysterious barn and\nyou hear a soft creaking in the loft followed by heavy footsteps.\n Do you:\nhide in the hay,\n go to the noise,\n or look up?\n");}
        
            string choicecb1;
            choicecb1=GetString();
            if (strcmp(choicecb1, "look up") ==0)
            {printf("\nA shadowy figure descends from the roof\n and lands behind you.\n it quickly knocks you unconcious and\n feeds you to the murder cows.\nGAME OVER\n");}
            if (strcmp(choicecb1, "go to the noise") ==0)
            {printf("\nA figure jumps out behind you and snaps your neck.\n GAME OVER\n");}
            if (strcmp( choicecb1, "hide in the hay") ==0)
            {printf("\nYou dive into the hay and as you feel around,\n you find an old, rusty revolver.\n Do you:\n keep it\n or leave it?\n");}
        
            string choicecb2;
            choicecb2=GetString();
            if (strcmp(choicecb2, "leave it") ==0)
            {printf("\nYou toss the gun aside,\n and as it hits the ground it misfires and kills you.\n GAME OVER\n");}
            if (strcmp(choicecb2, "keep it") ==0)
            {printf("\nAs you further inspect the weapon,\n you see it only has one bullet.\n As you notice this, a dark figure descends from the loft and goes back up.\n Do you:\n leave\n or follow him?\n");}
        
            string choicecb3;
            choicecb3=GetString();
            if (strcmp(choicecb3, "leave") ==0)
            {printf("\nYou step on a twig on the way out,\n the figure turns around,\n jumps on your chest,\n and dismembers you.\nGAME OVER");}
            if (strcmp(choicecb3, "follow him") ==0)
            {printf("\nYou follow the figure and watch as it feeds the murder cows.\n He could be friendly and helpful,\n or hostile and kill you.\n Do you take the shot?\n<yes/no>\n");}
        
            string choicecb4;
            choicecb4=GetString();
            if (strcmp(choicecb4, "no") ==0)
            {printf("\nAs you watch,\n the figure catches you out of the corner of its eye.\n Before you realise this,\n it jumps on you\n and rips your face off.\n GAME OVER\n");}
            if (strcmp(choicecb4, "yes") ==0)
            {printf("\nYou shoot the figure and as he falls,\n he drops a sling.\n Do you take it?\n <yes/no>\n");}
        
            string choicecb5;
            choicecb5=GetString();
            if (strcmp(choicecb5, "no") ==0)
            {printf("\nAs you leave the barn,\n the murder cows escape and devour you.\n GAME OVER\n");}
            if (strcmp(choicecb5, "yes") ==0)
            {printf("\nYou take the sling and save it for later use.\n You are now back at the house.\n Your last path is the forest.\n Type 'go left' to go to the forest.\n");}
            
            string choicecbf;
            choicecbf=GetString();
            if (strcmp(choicecbf, "go left") ==0)
            {printf("\nYou enter a dark and mysterious forest\n and travel across the road until you come upon a fork in the road.\n Do you:\n go left\n or go right?\n");}
                
            string choicecbf1;
            choicecbf1=GetString();
            if ( strcmp(choicecbf1, "go right") ==0)
            {printf("\nYou travel along the road and cross paths with 'The Abyss'.\n Do you: sling a rock at it\n or slash its arms?\n");}
                
            string choicecbf2;
            choicecbf2=GetString();
            if (strcmp(choicecbf2, "sling a rock at it") ==0)
            {printf("\nYou sling a rock at 'The Abyss' and it quickly envelops the stone and ejects it\n in your direction.\n You were not expecting this, so as you are caught off guard,\n the rock smashes your face, killing you.\nGAME OVER\n");}
            if (strcmp(choicecbf2, "slash its arms") ==0)
            {printf("\nYou lunge towards 'The Abyss',\n and as you do, you make a swift slice which cuts off the arms of 'The Abyss'.\n After this, seing you have the upper hand,\n you procede hacking and slicing until the foul abomination is vanquished.\n You continue on the road until you find a chest, but it is locked.\n Because of this, you go along the left path until you meet a prowler.\n Do you:\n sling a rock\n or engage it with the sword?\n");} 
                
            string choicecbf3;
            choicecbf3=GetString();
            if (strcmp(choicecbf3, "sling a rock") ==0)
            {printf("\nYou sling a rock at the prowler with great effort,\n but alas, it was to no avail.\n The rock dinks off of the prowler's thick armor-like flesh,\n and then the prowler stabs you with its arm.\n GAME OVER\n");}
            if (strcmp(choicecbf3, "engage it with the sword") ==0)
            {printf("\nThe prowler is caught off guard by your bold move to charge forward and attack.\n During that brief moment, you land a graceful blow to the prowler's neck.\n This, naturally, removes the prowler's head from its body.\n You take a look at the head and figure it could be helpful later.\n Do you: keep it\n or leave it?\n");}
                
            string choicecbf4;
            choicecbf4=GetString();
            if (strcmp(choicecbf4, "keep it") ==0)
            {printf("\nAs you reach down to grab the disembodied head it bites your hand\n and continues to chomp its way down your arm until it bites your neck, killing you.\n GAME OVER\n");}
            if (strcmp(choicecbf4, "leave it") ==0)
            {printf("\nYou leave the head where it is and journey on until you find a crate.\n You break it open and find a key.\n You then take that key and open the locked chest to find a skull shaped key.\n You then take that key and put it into the doors at the mansion.\n The door unlocks and you open the door and stare inside.\n Von Carlo welcomes you and commends you for your bravery.\n All is well, but he says you must make a choice.\n You must chose to: become the new caretaker\n or leave.\n Which do you do?\n");}
                
            string choicemcbf;
            choicemcbf=GetString();
            if (strcmp(choicemcbf, "leave") ==0)
            {printf("\nYou leave the mansion, and Von Carlo shoots you on your way out.\n As you fall to the floor, he cackles loudly then walks away muttering,\n 'If only he were wise. If only he were wise'\n GAME OVER\n");}
            if (strcmp(choicemcbf, "become the new caretaker") ==0)
            {printf("\nVon Carlo vanishes and you are left with nothing but the deed to the mansion.\n You live out the rest of your days waiting for the next adventurer\n to take on the challenge of VON CARLO'S MANSION.\n YOU WIN!\n");}
                
            if (strcmp(choicecbf1, "go left") ==0)
            {printf("\nYou go along the left path until you meet a prowler.\n Do you:\n sling a rock\n or engage it with the sword?\n");} 
                
            string choicecbfl1;
            choicecbfl1=GetString();
            if (strcmp(choicecbfl1, "sling a rock") ==0)
            {printf("\nYou sling a rock at the prowler with great effort,\n but alas, it was to no avail.\n The rock dinks off of the prowler's thick armor-like flesh,\n and then the prowler stabs you with its arm.\n GAME OVER\n");}
            if (strcmp(choicecbfl1, "engage it with the sword") ==0)
            {printf("\nThe prowler is caught off guard by your bold move to charge forward and attack.\n During that brief moment, you land a graceful blow to the prowler's neck.\n This, naturally, removes the prowler's head from its body.\n You take a look at the head and figure it could be helpful later.\n Do you: keep it\n or leave it?\n");}
                
            string choicecbfl2;
            choicecbfl2=GetString();
            if (strcmp(choicecbfl2, "keep it") ==0)
            {printf("\nAs you reach down to grab the disembodied head it bites your hand\n and continues to chomp its way down your arm until it bites your neck, killing you.\n GAME OVER\n");}
            if (strcmp(choicecbfl2, "leave it") ==0)
            {printf("\nYou leave the head where it is and journey on until you find a crate.\n You break it open and find a key.\n You then turn back and go along the right path where you encounter 'The Abyss'\n Do you: sling a rock at it\n or slash its arms?\n");}
                
            string choicecbfl3;
            choicecbfl3=GetString();
            if (strcmp(choicecbfl3, "sling a rock at it") ==0)
            {printf("\nYou sling a rock at 'The Abyss' and it quickly envelops the stone and ejects it\n in your direction.\n You were not expecting this, so as you are caught off guard,\n the rock smashes your face, killing you.\nGAME OVER\n");}
            if (strcmp(choicecbfl3, "slash its arms") ==0)
            {printf("\nYou lunge towards 'The Abyss',\n and as you do, you make a swift slice which cuts off the arms of 'The Abyss'.\n After this, seing you have the upper hand,\n you procede hacking and slicing until the foul abomination is vanquished.\n You continue on the road until you find a chest, and unlock it with your key.\n You then take that key and insert it into the mansion doors.\nThe door unlocks and you open the door and stare inside.\n Von Carlo welcomes you and commends you for your bravery.\n All is well, but he says you must make a choice.\n You must chose to:\n become the new caretaker\n or leave.\n Which do you do?\n");}
                
            string choicemcbfl;
            choicemcbfl=GetString();
            if (strcmp(choicemcbfl, "leave") ==0)
            {printf("\nYou leave the mansion, and Von Carlo shoots you on your way out.\n As you fall to the floor, he cackles loudly then walks away muttering,\n 'If only he were wise. If only he were wise'\n GAME OVER\n");}
            if (strcmp(choicemcbfl, "become the new caretaker") ==0)
            {printf("\nVon Carlo vanishes and you are left with nothing but the deed to the mansion.\n You live out the rest of your days waiting for the next adventurer\n to take on the challenge of VON CARLO'S MANSION.\n YOU WIN!\n");}
                
            
                if ( strcmp(choice1, "go left") ==0)
                {printf("\nYou enter a dark and mysterious forest\n and travel across the road until you come upon a fork in the road.\n Do you:\n go left\n or go right?\n");}
                
                string choicef1;
                choicef1=GetString();
                if ( strcmp(choicef1, "go right") ==0)
                {printf("\nYou travel along the road and cross paths with 'The Abyss'.\n You have nothing to defend yourself with and stand\n in the presence of 'The Abyss' paralyzed by fear.\n It slowly moves towards you then envelops you into its dark mass of tentacles.\n It keeps you there until you suffocate.\n It then releases you,\n pecks your eyes out,\n and eats you.\nGAME OVER\n");}
                
                
                if (strcmp(choicef1, "go left") ==0)
                {printf("\nYou go along the left path until you meet a prowler.\n You have nothing to defend yourself with and stand there paralyzed by fear.\n The powler then sprints at you,\n and in two swift motions,\n it stabs you with one arm,\n and dices you into many pieces with the other.\n GAME OVER\n");} 
    }
    Any help would be appreciated, Thanks.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    "string" is just "char *" so you could add this typedef:
    Code:
    typedef char * string;
    However, it's rather pointless (and even somewhat obfuscational). Real programmers just use char *.

    GetString is either mallocing space and returning a pointer, or passing back a pointer to a static char array. The latter is simplest and something like this might work for the code you posted (actually, it won't quite work but I believe that's due to errors in your code structure) :
    Code:
    char *GetString() {
        static char str[256];
        if (fgets(str, sizeof str, stdin)) {
            char *p = strchr(str, '\n');
            if (p)
                *p = '\n';
            return str;
        }
        return NULL;
    }
    Also, don't put "void" in the parens of the GetString function call. You've done that in a couple of them.

    And you don't need a separate variable for every string you get.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    For reference:

    http://dkui3cmikz357.cloudfront.net/...y-c-3.0/cs50.h
    http://dkui3cmikz357.cloudfront.net/...y-c-3.0/cs50.c

    Notice that GetString() is actually allocating memory using malloc() and therefore it should be free()ed (that is if you decide to continue using the cs50 "library")
    Last edited by Hodor; 01-08-2014 at 09:04 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Assuming it is possible to limit the lengths of strings within the GetString() function, I'd use a struct containing a C-style string.

    Code:
    typedef struct
    {
         char value[256];    /*  or whatever length is needed */
    } MyString;
    
    MyString GetString()
    {
         MyString retval;
         strcpy(retval.value, "Hello");
         return retval;
    }
    and then, in main(), specify your choices to be of type MyString, and use <variablename>.value to obtain the strings and pass to strcmp() or whatever else is required.

    The advantage of this is that you can make use of more than one string at a time, without forcing the caller to worry about memory deallocation.

    The original code is abysmal (unnecessary copy/paste) but I'll leave that alone.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    2
    Alrighty, thanks for the help. I'll reply later and see if any of this fixes the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my text adventure
    By ckeener in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2005, 08:33 AM
  2. text rpg adventure
    By BuRtAiNiAn FlY in forum Game Programming
    Replies: 13
    Last Post: 07-20-2002, 02:25 PM
  3. text adventure
    By pwprogrammer14 in forum C++ Programming
    Replies: 29
    Last Post: 05-26-2002, 08:02 PM
  4. Text Adventure
    By ArtGeek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 11:23 PM
  5. text adventure
    By BuRtAiNiAn FlY in forum C Programming
    Replies: 1
    Last Post: 09-05-2001, 09:39 PM