Hello everyone, I'm new to C and was hoping ya'll could review my code. It is a simple workout program that prints a list of exercises into a .txt file.
The major bug is in main.c line 18 and the generateProgram function in boxing_functions.c line 57. I'm still learning pointers and have no one to ask for help other than forums lol. Thanks in advance for any and all help.
main.c
#include "boxing_hdr.h"
void main(void){
int minutes, numSets;
char fileName;
printf("This program generates a boxing workout in a .txt file.\n");
printf("How many minutes do you want to workout?");
scanf("%d", &minutes);
// This divides the time you entered into 3 minutes blocks and
// returns how many sets you can workout for
numSets = Time(minutes);
// Prints outs the program to the new text
// file and returns the name of the .txt file
fileName = generateProgram(numSets);
printf("Generation done, the workout is located in the \
file named %s. Have fun and hit it hard\n", fileName);
}
boxing_functions.c
#include "boxing_hdr.h"
// This divides the time you entered into 3 minutes blocks and
// returns how many sets you can workout for
int Time(int minutes){
int sets;
sets = minutes / 4;
return sets;
}
// This function return the current time and date
// This is what the program will be named
char * Date(void){
struct tm *local;
time_t t;
t = time(NULL);
local = localtime(&t);
return asctime(local);
}
// This function performs the actual shuffling of the array
void shuffle(int *array, size_t n){
if (n > 1)
{
size_t i;
for (i = 0; i < n -1; i++)
{
size_t j = i + rand() / (RAND_MAX / (n-i) + i);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
// This Function initializes the array shuffles the data (by calling shuffle())
void initializeExercises()
{
int indices[length(data)];
int i;
// Initialize the indices
for ( i = 0; i < length(data); ++i)
{
indices[i] = i;
}
// Shuffle the indices array
shuffle(indices, length(data));
}
// This function generates and prints the program, returning the file name
char generateProgram(int sets){
int fd, openFlags, i;
int indices[length(data)];
mode_t filePerms;
char fileName[25];
// Set permissions for the file and open it
openFlags = O_CREAT | O_WRONLY;
filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
S_IROTH | S_IWOTH;
fd = open(Date(), openFlags, filePerms);
if(fd == -1)
exit(EXIT_FAILURE);
initializeExercises();
// Write to the file
for ( i = 0; i < sets; i++)
{
write(fd, data[indices[i]], strlen(data[indices[i]]));
write(fd, "\n", 1);
}
// Close the file
if(close(fd) == -1)
exit(EXIT_FAILURE);
strcpy(fileName, Date());
return fileName;
}
Here is the hdr.h file
#ifndef BOXING_HDR
#define BOXING_HDR
#define length(x) sizeof(x)/sizeof*(x)
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
static const char *data[] =
{
"Run 1 mile",
"Jump Rope 3 Rounds",
"Shadow Box",
"heavy bag",
"push ups",
};
int Time(int minutes);
char * Date(void);
char generateProgram(int sets);
void initializeExercises();
void shuffle(int *array, size_t n);
#endif