Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:39:32+00:00 2026-05-26T20:39:32+00:00

I’m writing a code for a simple text based game, and when I purchase

  • 0

I’m writing a code for a simple text based game, and when I purchase a car, the HP and TOPSPEED are registering for integers different than specified.
I’ve looked over the code myself, perhaps I’m not seeing it, but the Dodge Intrepid should register at 214hp and 140mph top speed, however when I enter the race menu, it registers for 320hp and 160mph top speed, which are the settings of the Mitsubishi 3000GT and the Dodge Stealth of the last beta. I imported the “race” code from the previous beta, being careful to omit any information about the cars used in the previous beta. If you can find my mistake or point anything out, it would be greatly appreciated (I’m including the code with my post). Thanks for your time. (I’m probably overlooking something) (Written in C, compiled and linked with DevC++)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*HP at RACE is malfunctioning, look over code.*/

int rnd(int range);
void seedrnd(void);

int main()
{
      char name[15];
      char ocar;
      char exit;
      char partsA;
      char partsB;
      char partsC;
      char choice;
      char dealer;
      char model;
      int bhp;
      int ohp;
      int tspd;
      int otspd;
      int score;
      int oscore;
      int cash;
      int bonus;
      int parts1;
      int parts2;
      int parts3;
      int car1;
      int car2;
      int car3;
      int car;
      int hp1;
      int hp2;
      int hp3;
      int tspd1;
      int tspd2;
      int tspd3;


      printf("What is your name?\n");
      scanf("%s",name);
      parts1=1;
      parts2=1;
      parts3=1;
      hp1=214;
      tspd1=140;
      hp2=490;
      tspd2=190;
      hp3=320;
      tspd3=160;
      car=0;
      car1=0;
      car2=0;
      car3=0;
      cash=40000;


      while(exit!='-')
      {
      printf("\nMenu:\n1.Garage\n2.Race\n3.Parts Shop\n4.Dealerships\n\n");
      choice=getch();

      if(choice=='1')
      {
           printf("Welcome to %s's garage:\n\n",name);
           if(car1>0)
           {
                 printf("1.Dodge Intrepid\n");
           }
           if(car2>0)
           {
                 printf("2.1988 Vector M12\n");
           }
           if(car3>0)
           {
                 printf("3.Mitsubishi 3000GT\n");
           }
           car=getch();
           if(car=='1')
           {
                   if(car1>0)
                   {
                             printf("You are now driving a Dodge Intrepid\n");
                             car=1;
                   }
                   if(car1<1)
                   {
                             printf("You do not yet own this vehicle\n");
                   }
           }
           if(car=='2')
           {
                   if(car2>0)
                   {
                             printf("You are now driving a 1988 Vector M12\n");
                             car=2;
                   }
                   if(car2<1)
                   {
                             printf("You do not yet own this vehicle\n");
                   }
           }
           if(car=='3')
           {
                   if(car3>0)
                   {
                             printf("You are now driving a Mitsubishi 3000GT\n");
                             car=3;
                   }
                   if(car<1)
                   {
                            printf("You do not yet own this vehicle\n");
                   }
           }
      }
      if(choice=='2')
      {
       if(car=1)
       {
           bhp=hp1;
           tspd=tspd1;
       }
       if(car=2)
       {
           bhp=hp2;
           tspd=tspd2;
       }
       if(car=3)
       {
           bhp=hp3;
           tspd=tspd3;
       }
      /*Begin Race Mechanism*/
      printf("Your selected car has %dhp and %dmph top speed\n\n",bhp,tspd);
      printf("Now choose your opponent:\n");
      printf("1.Rachel (150-250hp 110-130mph)\n");
      printf("2.Kyle (250-350hp 130-170mph)\n");
      printf("3.Darrian (350-450hp 170-210mph)\n");
      printf("4.Chelsea (450-550hp 210-220mph)\n");

      ocar=getch();
      seedrnd();
      if(ocar=='1')
      {
           ohp=rnd(100)+151;
           otspd=rnd(20)+111;
           bonus=500;
      }
      else if(ocar=='2')
      {
           ohp=rnd(100)+251;
           otspd=rnd(40)+131;
           bonus=1000;
      }
      else if(ocar=='3')
      {
           ohp=rnd(100)+351;
           otspd=rnd(40)+171;
           bonus=1500;
      }
      else if(ocar=='4')
      {
           ohp=rnd(100)+451;
           otspd=rnd(10)+211;
           bonus=2000;
      }
      else
      {
          printf("Haha, you're racing Eli\nNo contest here\n");
          ohp=2;
          otspd=25;
      }

      printf("Match-up:%s %dhp %dmph top speed\n",name,bhp,tspd);
      printf("              vs                \n");
      printf("Opponent: %dhp %dmph top speed\n",ohp,otspd);
      getch();

      score=bhp*tspd;
      oscore=ohp*otspd;

      printf("Let the race begin\n");
      sleep(1000);
      printf("3\n");
      sleep(1000);
      printf("2\n");
      sleep(1000);
      printf("1\n");
      sleep(1000);
      printf("!\n\n");
      sleep(2000);
      printf("The race is over, and the winner is!!!\n\n\n\n");
      sleep(3000);
      if(score>oscore)
      {
         printf("YOU!!!\n");
         cash=cash+(bonus);
      }
      else if(score<oscore)
      {
         printf("Your Opponent...\n");
      }
      else
      {
        printf("...neither of you, it was a tie!\n");
        cash=cash+(bonus/2);
      }
      printf("You now have$%d.\n\n",cash);




      }/* Closes choice 2*/
      if(choice=='3')
      {
      /* Parts Shop*/
      printf("Under Construction\n");
      }
      if(choice=='4')
      {
           printf("Press 'y' to exit\n"); 

           printf("Please Select a Dealership\n");
           printf("1.Dodge\n2.Vector\n3.Mitsubishi\n\n");
           while(dealer!='y')
           {
           dealer=getch();
           if(dealer=='1')
           {
              printf("DODGE:\n");
              if(car1<1)
              printf("1.Dodge Intrepid (214hp 140mph) $21,000\n\n");
              model=getch();
                   if(model=='1')
                   {
                        if(cash<21000)
                        {
                             printf("You cannot afford this vehicle\n\n");
                        }
                        if(cash>=21000)
                        {
                             if(car1>0)
                             {
                              printf("You have already purchased this vehicle\n\n");
                             }
                             if(car1<1)
                             {
                                 car1=car1+1;
                                 cash=cash-21000;
                                 printf("Thank You for purchasing this Dodge Intrepid\n\n");
                             }
                        }
                   }
         }
         if(dealer=='2')
         {
            printf("VECTOR:\n");
            if(car2<1)
            printf("1.1988 Vector M12 (490hp 190mph)$180,000\n\n");
            model=getch();
                 if(model=='1')
                 {
                      if(cash<180000)
                      {
                           printf("You cannot afford this vehicle\n\n");
                      }
                      if(cash>=180000)
                      {
                           if(car2>0)
                           {
                           printf("You have already purchased this vehicle\n\n");
                           }
                           if(car2<1)
                           {
                           car2=car2+1;
                           cash=cash-180000;
                           printf("Thank You for purchasing this 1988 Vector M12\n\n");
                           }
                      }
                 }
       }
       if(dealer=='3')
       {
            printf("MITSUBISHI:\n");
            if(car3<1)
            printf("1.Mitsubishi 3000GT (320hp 160mph) $60,000\n\n");
            model=getch();
                 if(model=='1')
                 {
                      if(cash<60000)
                      {
                           printf("You cannot afford this vehicle\n\n");
                      }
                      if(cash>=60000)
                      {
                           if(car3>0)
                           {
                           printf("You have already purchased this vehicle\n\n");
                           }
                           if(car3<1)
                           {
                           car3=car3+1;
                           cash=cash-60000;
                           printf("Thank You for purchasing this Mitsubishi 3000GT\n\n");
                           }
                      }
                 }
       }
       }
       }
       exit=getch();
  }
  return(0);
  }


int rnd(int range)
{
    int i;

    i=rand()%range;
    return(i);
}

void seedrnd(void)
{
     srand((unsigned)time(NULL));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:39:33+00:00Added an answer on May 26, 2026 at 8:39 pm
       if(car=1)
       {
           bhp=hp1;
           tspd=tspd1;
       }
    

    You probably mean if( car == 1 ) { … this just sets car to 1, and "returns" 1 to the if. Then you do the same for car == 2 and car == 3.

    = is the Assignment operator, while == is the comparison operator! The first assigns a value to a variable, while the latter returns true if the are the same (note, for char* you need strcmp()).

    Generally you should break your program into small functions and also check for the unhappy case (e.g. what happens if user inputs something not acceptable?)

    Another thing that you should pay some attention to is this

    scanf("%s",name);
    

    what if someone types in more than 15 characters? You will start righting in memory you do not own and this invokes Undefined Behaviour. One easy trick for that is to do

    scanf("%14s", name);
    

    This will restrict the input to 14 characters (and will keep the 15th for the nul terminator). Read more here: http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

    As others have noted in the comments, consider using structs to make your life easier. I haven’t read the program in too detail to understand the exact structure of your game, but you could have for example a struct for the user:

    struct player
    {
        char name[15];
        car* owned_cars; //Linked List!
    }
    

    Where car is defined like that:

    struct car
    {
        int max_speed;
        int colour; //or even better an enum here
        int seats;
        //... etc.
    
        car* next_car; //Make the Linked List's next node here!
    }
    

    Also! If you make a LinkedList, one mistake that usually happens is that they forgot to initialize thieir pointers to NULL. So :

    car* new_car = malloc( sizeof(car) );
    new_car->next_car = NULL;
    

    If you don’t do that, next_car will contain a random/garbage number! So when you try to go through the List

    while(new_car->next_car != NULL)
    {
        //....
    }
    

    You will access memory you do not own.

    Good luck and have fun!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a text area in my form which accepts all possible characters from
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.