0

i have a little problem splitting the source file into smaller source files and headers. Below I am attaching the code that I managed to write, unfortunately there are some errors and warnings. Should variables that are constants be written in the header? My main.c

#include <stdio.h>
#include <stdlib.h>
#include "story.h"

void dodawanie(int x, int y, struct d* dzialania)
{
    ok++;
    int w = x+y;
    dzialania = realloc(dzialania, ok*sizeof(struct d));
        dzialania[ok-1].a = x;
        dzialania[ok-1].b = y;
        dzialania[ok-1].wynik = w;
        dzialania[ok-1].znak = '+';
}
void odejmowanie(int x, int y, struct d* dzialania)
{
    ok++;
    int w = x-y;
    dzialania = realloc(dzialania, ok*sizeof(struct d));
    dzialania[ok-1].a = x;
    dzialania[ok-1].b = y;
    dzialania[ok-1].wynik = w;
    dzialania[ok-1].znak = '-';
}
int main()
{
    int A, B;
    int dz;
    struct d *dzi = malloc (ok* sizeof(struct d));
    for(;;)
    {
        printf(" KALKULATOR \n");
        printf(" 1. dodwanie \n");
        printf(" 2. odejmowanie \n");
        printf(" 3. historia \n");
        printf(" co chcesz policzyc: ");
        scanf("%d", &dz);
        switch (dz) {
            case 1:
            {
                printf("Podaj A: ");
                scanf("%d", &A);
                printf(" Podaj B: ");
                scanf("%d", &B);
                dodawanie(A, B, dzi);

            }break;
            case 2:
            {
                printf("Podaj A: ");
                scanf("%d", &A);
                printf(" Podaj B: ");
                scanf("%d", &B);
                odejmowanie(A, B, dzi);
            }break;
            case 3:
            {
                int h;
                printf(" HISTORIA \n");
                printf(" 1. wypisz. \n");
                printf(" 2. zapisz do pliku. \n");
                printf(" co chcesz zrobic z historia: ");
                scanf("%d", &h);
                switch (h)
                {
                    case 10: {
                        wypisz(dzi);
                    }
                    break;
                    case 11: {
                        usun(dzi);
                    }
                        break;
                    case 12: {
                        zapiszdo(dzi);
                    } break;
                    case 13: {
                        wyswietlz();
                    } break;
                    case 14: {
                        usunzpliku();
                    } break;
                    default: {
                        printf(" nie prawidlowe dzialanie ");
                    }
                }
            }break;
            default:
            {
                printf(" Brak dzialania ");
            }break;
        }
    }
}

here is story.c

#include <stdio.h>
#include <stdlib.h>
#include "story.h"

void wypisz(struct d* tabs)
{
    if(ok==0)
    {
        printf("historia pusta!");
    }
    else
    {
        for (int i = 0; i < ok; i++) {
            printf("%d", tabs[i].a);
            printf("%c", tabs[i].znak);
            printf("%d", tabs[i].b);
            printf("=");
            printf("%d", tabs[i].wynik);
            printf("\n");
        }
    }
}
void usunzpliku()
{
    FILE *plik=fopen("siema.txt", "w");
    if(plik)
    {
        fputc('\0', plik);
        printf("WYCZYSZCZONO HISTORIE Z PLIKU\n");
        fclose(plik);
    }
    else
    {
        printf("Nie udalo sie wczytac pliku.");
    }

}
void usun(struct d* tabs)
{
    if(ok==0)
    {
        printf("historia pusta!");
    }
    else
    {
        ok=0;
        tabs = realloc(tabs, ok*sizeof(struct d));
    }
}
void zapiszdo(struct d* tabs)
{
    FILE *plik=fopen("siema.txt", "a");

    if(ok==0)
    {
        printf("historia pusta!");
    }
    else
    {
        if(plik)
        {
            for (int i = 0; i < ok; i++) {
                fprintf(plik,"%d", tabs[i].a);
                fprintf(plik,"%c", tabs[i].znak);
                fprintf(plik,"%d", tabs[i].b);
                fprintf(plik, "=");
                fprintf(plik,"%d", tabs[i].wynik);
                fprintf(plik,"\n");
            }
            fclose(plik);
        }
        else
        {
            printf("Nie udalo sie wczytac pliku.");
        }
    }
}
void wyswietlz()
{
    FILE *plik=fopen("siema.txt", "r");
    if(plik)
    {
        int c;
        c=getc(plik);
        printf("####    HISTORIA   ####\n");
        while(c!=EOF)
        {
            printf("%c", c);
            c=getc(plik);
        }
        printf("####    KONIEC    ####");
    }
    else
    {
        printf("Nie udalo sie wczytac pliku.");
    }
    fclose(plik);
}

story.h

#ifndef UNTITLED3_STORY_H
#define UNTITLED3_STORY_H
struct d
{
    int a;
    int b;
    int wynik;
    char znak;
};
int ok=0;
void wypisz(struct d* tabs);
void usunzpliku();
void usun(struct d* tabs);
void zapiszdo(struct d* tabs);
void wyswietlz();
#endif //UNTITLED3_STORY_H

And error

[1/1] Linking C executable untitled3.exe
FAILED: untitled3.exe 
cmd.exe /C "cd . && "C:\Program Files\JetBrains\CLion 2021.3.2\bin\mingw\bin\gcc.exe" -g  CMakeFiles/untitled3.dir/main.c.obj CMakeFiles/untitled3.dir/story.c.obj -o untitled3.exe -Wl,--out-implib,libuntitled3.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2021.3.2\bin\mingw\bin/ld.exe: CMakeFiles/untitled3.dir/story.c.obj:C:/Users/Mateusz/CLionProjects/untitled3/story.h:10: multiple definition of `ok'; CMakeFiles/untitled3.dir/main.c.obj:C:/Users/Mateusz/CLionProjects/untitled3/story.h:10: first defined here
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

thx for help me!

  • 2
    Don't define variables in header files like `int ok=0;`. You can use `extern int ok;` and then define it in one source file. – Retired Ninja Feb 09 '22 at 21:02
  • @RetiredNinja can u explain me why? or how it work? becouse i have to part to another one – NewRobot30 Feb 09 '22 at 21:08
  • 1
    You should read the accepted answer for this question: [How do I use extern to share variables between source files?](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files) It's not a direct duplicate of your question, of which there are many, but it is full of useful advice. – Retired Ninja Feb 09 '22 at 21:08
  • @RetiredNinja i tried in story.h `code extern int ok;` and than in main.c `code ok=0;` it works but i have warrning: `code warning: data definition has no type or storage class warning: type defaults to 'int' in declaratio ` – NewRobot30 Feb 09 '22 at 21:43
  • Header: `extern int ok;` One source file: `int ok=0;` – Retired Ninja Feb 09 '22 at 23:37

0 Answers0