Originally Posted by
unsolicited
Am i posting correctly? Is it better if i post the entire source code? i was trying to keep my post more concise.
Concise is good. Sometimes the entire source helps. I took your code and ran it and here's what I got after a few small changes. It runs for me without trouble. Notice I added a few checks for errors. Not strictly necessary, but if something goes wrong it helps to know where it happens.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define error(LINE_NO, LINE_STR) error_("%s:%d: Error occured while processing input line %d:\n%s", \
__FILE__, __LINE__, (LINE_NO), LINE_STR)
static inline void error_(const char *fmt, const char *file, int line, int line_no, const char *line_str)
{
fprintf(stderr, fmt, file, line, line_no, line_str);
exit(1);
}
struct cardz {
char value[255];
char color[255];
int point_value;
} card[108];
const size_t card_sz = 108;
int main()
{
void init_cardz();
void get_cardz(FILE *f_in);
void print_cardz();
init_cardz();
get_cardz(stdin);
print_cardz();
}
void init_cardz()
{
for (size_t i = 0; i < card_sz; i++) {
strncpy(card[i].value, "", 255);
strncpy(card[i].color, "", 255);
card[i].point_value = 0;
}
}
void get_cardz(FILE *f_in)
{
char line[255];
for (size_t i = 0;
strncpy(line, "", sizeof(line)),
fgets (line, sizeof(line), f_in) != NULL;
i++
) {
// Check if too many lines
if (i >= card_sz) {
error(i+1, line);
}
// Check if line is too long
if (line[sizeof(line)-1] != '\0') {
error(i+1, line);
}
// Process line
char dummy;
int retval = sscanf (line, "%s %s %d %c", card[i].color,
card[i].value,
&card[i].point_value,
&dummy);
// Check if wrong number of parameters
if (retval != 3) {
error(i+1, line);
}
}
}
void print_cardz()
{
bool card_empty(struct cardz card);
for (size_t i = 0; i < card_sz; i++) {
// stop at first empty card
if (card_empty(card[i])) {
break;
}
printf("Card %d: %s, %s, %d\n", i, card[i].value, card[i].color, card[i].point_value);
}
}
bool card_empty(struct cardz card)
{
return strcmp(card.value, "") == 0 &&
strcmp(card.color, "") == 0 &&
card.point_value == 0;
}