vendredi 7 avril 2017

Why doesn't my program print out the values I want?

I've been scratching my head over this one for a while now.

I'm trying to write a program that randomly generates the score in 6 hockey games and assigns the results to certain values. However, the points awarded to the winning team (3 for a victory, and 1 for a tie) doesn't seem to be stored correctly in the post in the post I'm trying to add them to (serie[i].poang), since the last part of the program just prints out the scores of each team as "0" when I run the program. My best guess is that something is off with the if-else statements that assign the score, but I can't find anything myself.

The variables are written in Swedish, so go ahead and ask if anything about that is unclear. Thanks on beforehand!

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

#define ESC 27


struct lag {
char namn[20];
int gjorda;
int inslappta;
int poang;
};

int main(void) {
struct lag temp, serie[] = {
    { "Bryn\204s", 0, 0, 0 },
    { "Djurg\206rden", 0, 0, 0 },
    { "Fr\224lunda", 0, 0, 0 },
    { "F\204rjestad", 0, 0, 0 },
    { "HV 71     ", 0, 0, 0 },
    { "Link\224ping", 0, 0, 0 },
    { "Lule\206     ", 0, 0, 0 },
    { "MODO    ", 0, 0, 0 },
    { "R\224gle     ", 0, 0, 0 },
    { "Skellefte\206", 0, 0, 0 },
    { "S\224dert\204lje", 0, 0, 0 },
    { "Timr\206     ", 0, 0, 0 }
};
int i, j, hemma, borta;


srand((unsigned)time(NULL));


do {
    system("cls");
    for (i = 0; i <= 10; i += 2)
    {
        hemma = rand() % 8;
        borta = rand() % 8;
        serie[i].gjorda = serie[i].gjorda + hemma;
        serie[i].inslappta = serie[i].inslappta + borta;
        serie[i + 1].gjorda = serie[i + 1].gjorda + borta;
        serie[i + 1].inslappta = serie[i + 1].inslappta + hemma;

        printf("%s - %s \t \t \t \t %d - %d \n", serie[i].namn, serie[i + 1].namn, hemma, borta);
    }

    if (hemma > borta)
        serie[i].poang = serie[i].poang + 3;

    else if (hemma == borta)
    {
        serie[i].poang = serie[i].poang + 1;
        serie[i + 1].poang = serie[i + 1].poang + 1;
    }

    else if (hemma < borta)
        serie[i + 1].poang = serie[i + 1].poang + 3;


    for (i = 0; i < 11; i++)
        for (j = i + 1; j < 12; j++)
            if (serie[j].poang < serie[i].poang)
            {
                temp.poang = serie[i].poang;
                serie[i].poang = serie[j].poang;
                serie[j].poang = temp.poang;

                temp.gjorda = serie[i].gjorda;
                serie[i].gjorda = serie[j].gjorda;
                serie[j].gjorda = temp.gjorda;

                temp.inslappta = serie[i].inslappta;
                serie[i].inslappta = serie[j].inslappta;
                serie[j].inslappta = temp.inslappta;

                strcpy(temp.namn, serie[i].namn);
                strcpy(serie[i].namn, serie[j].namn);
                strcpy(serie[j].namn, temp.namn);
            }

    for (i = 0; i < 11; i++)
        for (j = i + 1; j < 12; j++)
            if (serie[j].poang == serie[i].poang)
                if ((serie[j].gjorda - serie[j].inslappta) < (serie[i].gjorda - serie[i].inslappta))
                {
                    temp.poang = serie[i].poang;
                    serie[i].poang = serie[j].poang;
                    serie[j].poang = temp.poang;

                    temp.gjorda = serie[i].gjorda;
                    serie[i].gjorda = serie[j].gjorda;
                    serie[j].gjorda = temp.gjorda;

                    temp.inslappta = serie[i].inslappta;
                    serie[i].inslappta = serie[j].inslappta;
                    serie[j].inslappta = temp.inslappta;

                    strcpy(temp.namn, serie[i].namn);
                    strcpy(serie[i].namn, serie[j].namn);
                    strcpy(serie[j].namn, temp.namn);
                }

    for (i = 0; i <= 11; i++)
    {
        printf(" %s \t \t %d - %d \t \t %d \n", serie[i].namn, serie[i].gjorda, serie[i].inslappta, serie[i].poang);
    }
} while (_getch() != ESC);




return 0;

}

Aucun commentaire:

Enregistrer un commentaire