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!