Avatar billede pitzen Nybegynder
11. maj 2005 - 17:35 Der er 2 kommentarer og
1 løsning

Pointer-kæde problem

Hej eksperter!

Jeg skal aflevere denne programmeringsopgave imorgen, men der er et problem!

Når jeg har kørt spillet eén gang og vender tilbage til main() skriver jeg mit navn igen og så er topscorerliste nulstillet..

Jeg kan ikke finde fejlen... Men prøv at køre programmet og se selv!

Håber i kan hjælpe!

pf. tak.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void menu();
void spil();
void nyt_point();
void vis_listen();

struct point_struct
{
  char point[40], navn[40];
  struct point_struct *naeste;
};

struct point_struct *foerste, *ny;

int opvx=1, opvy=1, nedhy=7, nedhx=9, scoreone, scoretwo, x, samletscore, counter=0, score;


char svar_box, valg='j', menuvalg, valg;


void et(int opvx, int opvy, int nedhx, int nedhy, int x)
{
  gotoxy((nedhx/2+1)+x,nedhy/2+1);
  putchar('O');
}



void to(int opvx, int opvy, int nedhx, int nedhy, int x)
{
  gotoxy(opvx+2+x, nedhy-1);
  putchar('O');
  gotoxy(nedhx-2+x, opvy+1);
  putchar('O');
}


void tre(int opvx, int opvy, int nedhx, int nedhy, int x)
{
  gotoxy((opvx+2)+x, nedhy-1);
  putchar('O');
  gotoxy(nedhx-2+x, opvy+1);
  putchar('O');
  gotoxy((nedhx/2+1)+x,nedhy/2+1);
  putchar('O');
}


void fire(int opvx, int opvy, int nedhx, int nedhy, int x)
{
  gotoxy(opvx+2+x, nedhy-1);
  putchar('O');
  gotoxy(opvx+2+x, opvy+1);
  putchar('O');
  gotoxy(nedhx-2+x, opvy+1);
  putchar('O');
  gotoxy(nedhx-2+x, nedhy+-1);
  putchar('O');
}


void fem(int opvx, int opvy, int nedhx, int nedhy, int x)
{
  gotoxy(opvx+2+x, nedhy-1);
  putchar('O');
  gotoxy(opvx+2+x, opvy+1);
  putchar('O');
  gotoxy(nedhx-2+x, opvy+1);
  putchar('O');
  gotoxy(nedhx-2+x, nedhy+-1);
  putchar('O');
  gotoxy((nedhx/2+1)+x,nedhy/2+1);
  putchar('O');
}


void seks(int opvx, int opvy, int nedhx, int nedhy, int x)
{
  gotoxy(opvx+2+x, nedhy-1);
  putchar('O');
  gotoxy(opvx+2+x, opvy+1);
  putchar('O');
  gotoxy(nedhx-2+x, opvy+1);
  putchar('O');
  gotoxy(nedhx-2+x, nedhy+-1);
  putchar('O');
  gotoxy(opvx+2+x,nedhy/2+1);
  putchar('O');
  gotoxy(nedhx-2+x,nedhy/2+1);
  putchar('O');
}




void omrids(int opvx, int opvy, int nedhx, int nedhy, int x)
{
  int i;

  clrscr();
  gotoxy(opvx+x, opvy);
  putchar('É');
  for(i = 1; i < (nedhx-opvx); i++)
    putchar('Í');
  putchar('»');
  gotoxy(opvx+x, (opvy+1));
  for(i = 1; i < (nedhy-opvy); i++)
    {
      putchar('º');
      gotoxy(opvx+x, opvy+1+i);
    }
  gotoxy(nedhx+x, (opvy+1));
  for(i = 1; i < (nedhx-opvy); i++)
    {
      putchar('º');
      gotoxy(nedhx+x, opvy+i);
    }
  gotoxy(opvx+x, nedhy);
  putchar('È');
  for(i = 1; i < (nedhx-opvx); i++)
    putchar('Í');
  putchar('¼');

  gotoxy(opvx+10+x, opvy);
  putchar('É');
  for(i = 1; i < (nedhx-opvx); i++)
    putchar('Í');
  putchar('»');
  gotoxy(opvx+10+x, (opvy+1));
  for(i = 1; i < (nedhy-opvy); i++)
    {
      putchar('º');
      gotoxy(opvx+10+x, opvy+1+i);
    }
  gotoxy(nedhx+10+x, (opvy+1));
  for(i = 1; i < (nedhx-opvy); i++)
    {
      putchar('º');
      gotoxy(nedhx+10+x, opvy+i);
    }
  gotoxy(opvx+10+x, nedhy);
  putchar('È');
  for(i = 1; i < (nedhx-opvx); i++)
    putchar('Í');
  putchar('¼');
}

void main()
{
  do
  {
      foerste = NULL;
      clrscr();
      gotoxy(25,1);
      printf("--==Terningspillet==--");
      gotoxy(24,3);
      printf("Indtast navn p† spiller:");
      gotoxy(24,4);
      ny = malloc(sizeof(struct point_struct));
      scanf("%s", ny->navn);
      menu();
      switch(menuvalg)
      {
    case '1': spil();break;
    // case '2': regler();break;
    case '3': vis_listen();break;
      }
      flushall();
      valg = getch();
  }while(valg!='n');
}

void menu()
{
  clrscr();
 
 
  gotoxy(25,4);
  printf("HOVEDMENU");
  gotoxy(25,6);
  printf("(1) Start spil");
  gotoxy(25,8);
  printf("(2) Se regler");
  gotoxy(25,10);
  printf("(3) Se Topscore liste");
  menuvalg = getch();
}



void spil()
{
  clrscr();
  printf("Hej %s!\n\n", ny->navn);
  printf("Tast ENTER for at sl† med terningen.\n");
  getch();
  samletscore = 0;
  counter = 0;
  while(samletscore != 100)
  {
      if(samletscore < 100)
      {
    counter++;
    clrscr();

    x = 30;

    omrids(opvx, opvy, nedhx, nedhy, x);      // Tegner 2 terning-omrids.

    randomize();
    scoreone = (rand() % 6)+1;
    scoretwo = (rand() % 6)+1;
    scoretwo = (rand() % 6)+1;

    x=30;

    if(scoreone == 1)
        et(opvx, opvy, nedhx, nedhy, x);
    if(scoreone == 2)
        to(opvx, opvy, nedhx, nedhy, x);
    if(scoreone == 3)
        tre(opvx, opvy, nedhx, nedhy, x);
    if(scoreone == 4)
        fire(opvx, opvy, nedhx, nedhy, x);
    if(scoreone == 5)
        fem(opvx, opvy, nedhx, nedhy, x);
    if(scoreone == 6)
        seks(opvx, opvy, nedhx, nedhy, x);

    x=40;

    if(scoretwo == 1)
        et(opvx, opvy, nedhx, nedhy, x);
    if(scoretwo == 2)
        to(opvx, opvy, nedhx, nedhy, x);
    if(scoretwo == 3)
        tre(opvx, opvy, nedhx, nedhy, x);
    if(scoretwo == 4)
        fire(opvx, opvy, nedhx, nedhy, x);
    if(scoretwo == 5)
        fem(opvx, opvy, nedhx, nedhy, x);
    if(scoretwo == 6)
        seks(opvx, opvy, nedhx, nedhy, x);

    gotoxy(1,1);
    printf("1. terning: %d", scoreone);
    printf("\n\n2. terning: %d", scoretwo);

    samletscore = samletscore + scoreone + scoretwo;
    printf("\n\nSamlet antal ›jne: %d", samletscore);
    printf("\nAntal slag: %d", counter);


    valg=getch();
      }

      if(samletscore > 100)
      {
    counter++;
    clrscr();
    printf("Hej %s\n\n", ny->navn);
    randomize();
    scoreone = (rand() % 6) +1;
    scoretwo = (rand() % 6) +1;
    x=30;
    omrids(opvx, opvy, nedhx, nedhy, x);      // Tegner 2 terning-omrids.

    x=30;

    if(scoreone == 1)
        et(opvx, opvy, nedhx, nedhy, x);
    if(scoreone == 2)
        to(opvx, opvy, nedhx, nedhy, x);
    if(scoreone == 3)
        tre(opvx, opvy, nedhx, nedhy, x);
    if(scoreone == 4)
        fire(opvx, opvy, nedhx, nedhy, x);
    if(scoreone == 5)
        fem(opvx, opvy, nedhx, nedhy, x);
    if(scoreone == 6)
        seks(opvx, opvy, nedhx, nedhy, x);

    x=40;

    if(scoretwo == 1)
        et(opvx, opvy, nedhx, nedhy, x);
    if(scoretwo == 2)
        to(opvx, opvy, nedhx, nedhy, x);
    if(scoretwo == 3)
        tre(opvx, opvy, nedhx, nedhy, x);
    if(scoretwo == 4)
        fire(opvx, opvy, nedhx, nedhy, x);
    if(scoretwo == 5)
        fem(opvx, opvy, nedhx, nedhy, x);
    if(scoretwo == 6)
        seks(opvx, opvy, nedhx, nedhy, x);

    gotoxy(1,1);
    printf("1. terning: %d", scoreone);
    printf("\n\n2. terning: %d", scoretwo);

    samletscore = samletscore - scoreone - scoretwo;
    printf("\n\nSamlet antal ›jne: %d", samletscore);
    printf("\nAntal slag: %d", counter);

    valg=getch();
      }

  }
  clrscr();
  printf("Tillykke %s ! Du har klaret spillet p† %d fors›g.", ny->navn, counter);
  score = 10000/counter;
  printf("\nDu fik %d point!", score);
  getch();
  nyt_point();
  vis_listen();
}

void nyt_point(void)
{
  struct point_struct *denne = foerste;

  clrscr();
//  ny = malloc(sizeof(struct point_struct));
//  printf("\nSkriv nyt point: ");
//  scanf("%s", ny->point);
//  printf("\nSkriv navn: ");
//  scanf("%s", ny->navn);  '
  itoa(score, ny->point, 10);
  ny->naeste = NULL;
  if(foerste == NULL)
      foerste = ny;
  else if(atoi(ny->point) > atoi(denne->point))
  {
      foerste = ny;
      foerste->naeste = denne;
  }
  else
  {
      while(denne->naeste != NULL)
      {
    if(atoi(denne->naeste->point) < atoi(ny->point))
        break;
    denne = denne->naeste;
      }
      ny->naeste = denne->naeste;
      denne->naeste = ny;
  }
}

void vis_listen(void)
{
  struct point_struct *denne = foerste;

  clrscr();
  printf("--==TOPSCORE-LISTE==--");
  while(denne != NULL)
    {
      printf("\n%s scorede %s point",denne->navn, denne->point);
      denne = denne->naeste;
    }
}
Avatar billede arne_v Ekspert
11. maj 2005 - 18:27 #1
Det er jo en del kode.

Men skal:

void main()
{
  do
  {
      foerste = NULL;

ikke være:

void main()
{
  foerste = NULL;
  do
  {

for at bevare top scorer listen ?
Avatar billede pitzen Nybegynder
11. maj 2005 - 18:36 #2
Perfekt! Så var den der :D

Jeg vidste det var en bagatel!

Skriv et svar og du får pointne
tak
Avatar billede arne_v Ekspert
11. maj 2005 - 19:06 #3
ok
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester