-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollDice_and_fileIO.c
49 lines (37 loc) · 1.22 KB
/
rollDice_and_fileIO.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Copyright© Spencer Hiscox, 2023
All Rights Reserved
*/
#include "header.h"
int rollDice(void) {
return ((rand() % 6) + 1);
}
void print2File(diceRoll currentState, filePath output) {
FILE* fid;
fopen_s(&fid, output.filePath, "a");
fprintf_s(fid, (currentState.rollNumber == 1) ? "Roll Number,Die 1,Die 2,Sum\n%i,%hu,%hu,%hu\n"
: "%i,%hu,%hu,%hu\n", currentState.rollNumber, currentState.die1, currentState.die2, currentState.rollTotal);
fclose(fid);
return;
}
diceRoll cOutcome(unsigned short int userInput, unsigned short int* tally, diceRoll currentStatus, filePath output) {
output = fileNameX(output, userInput);
while (currentStatus.rollNumber < userInput) {
currentStatus.die1 = rollDice();
currentStatus.die2 = rollDice();
currentStatus.rollTotal = currentStatus.die1 + currentStatus.die2;
tally[currentStatus.rollTotal - 2] += 1;
currentStatus.rollNumber++;
print2File(currentStatus, output);
}
if (currentStatus.rollNumber != userInput) {
printf("counting error in cOutcome, number of iterations does not match user input.\n");
}
printStats(tally);
return currentStatus;
}
diceRoll initState(void) {
diceRoll init;
init.die1 = init.die2 = init.rollTotal = init.rollNumber = 0;
return init;
}