用C编程,整数分配错误,找不到错误

0 c integer

我正在为一个简单的基于文本的游戏编写代码,当我购买汽车时,HP和TOPSPEED正在注册不同于指定的整数.我自己查看了代码,也许我没有看到它,但道奇Intrepid应该以214hp和140mph的速度注册,但是当我进入比赛菜单时,它注册320hp和160mph的最高速度,这是最后测试版的三菱3000GT和道奇Stealth的设置.我从之前的测试版导入了"竞赛"代码,小心省略了之前测试版中使用的汽车的任何信息.如果你能找到我的错误或指出任何问题,我将非常感激(我的帖子中包含了代码).谢谢你的时间.(我可能忽略了一些东西)(用C编写,用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));
}
Run Code Online (Sandbox Code Playgroud)

小智 6

   if(car=1)
   {
       bhp=hp1;
       tspd=tspd1;
   }
Run Code Online (Sandbox Code Playgroud)

你可能意味着if( car == 1 ) {...这只是设置car为1,并"返回" 1if.然后你为car == 2和做同样的事情car == 3.

=是赋值运算符,==而是比较运算符!第一个为变量赋值,而后者在相同时返回true(注意,char*您需要strcmp()).

一般来说,您应该将程序分解为小函数,并检查不满意的情况(例如,如果用户输入的内容不可接受会发生什么?)

你应该注意的另一件事是这个

scanf("%s",name);
Run Code Online (Sandbox Code Playgroud)

如果有人输入超过15个字符怎么办?你将在你不拥有的内存中开始正确,这将调用未定义的行为.一个简单的诀窍是做

scanf("%14s", name);
Run Code Online (Sandbox Code Playgroud)

这会将输入限制为14个字符(并且将保留nul终结符的第15个字符).在这里阅读更多内容:http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

正如其他人在评论中指出的那样,考虑使用结构来让您的生活更轻松.我没有详细阅读该程序以了解游戏的确切结构,但您可以为用户提供一个结构:

struct player
{
    char name[15];
    car* owned_cars; //Linked List!
}
Run Code Online (Sandbox Code Playgroud)

在哪里car定义如下:

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!
}
Run Code Online (Sandbox Code Playgroud)

也!如果你创建一个LinkedList,通常会发生的一个错误是他们忘记初始化thieir指针NULL.所以:

car* new_car = malloc( sizeof(car) );
new_car->next_car = NULL;
Run Code Online (Sandbox Code Playgroud)

如果你不这样做,next_car将包含一个随机/垃圾编号!所以,当你试图通过列表

while(new_car->next_car != NULL)
{
    //....
}
Run Code Online (Sandbox Code Playgroud)

您将访问您不拥有的内存.

祝好运并玩得开心点!