annoying problem that's a pain to figure out...........
Hi, below is a program that I copied out of the book(word for word) so that I could figure out more about input and output in C and for some wonderful reason it decides to crash on me sometimes :mad: . My question is why(I have no clue whatsoever, since the book is wrong(I'm 95% sure of that)). It's supposed to print out a small table, but that's all that I know about it. Program.....
Code:
// enumerating_items.c:
#include <stdio.h>
#include <stdlib.h>
#define NUMITEMS 4
char *names[4] = {"Bob", "Azmaria", "Rosette", "Chrno"};
int main()
{
int loop;
char temp[4][160];
char name[20];
int age;
long amount;
for(loop = 0; loop < NUMITEMS; loop++)
sprintf(temp[loop], "%s %d %ld", name[loop], rand() % 20 + 18, rand() + 27500L);
printf("%4s | %-20s | %5s | %9s\n", "#", "Name", "Age", "Amount");
printf("-----------------------------------------------------------------------\n");
for(loop = 0; loop < NUMITEMS; loop++)
{
sscanf(temp[loop], "%s %d %ld", &name, &age, &amount);
printf("%4d | %-20s | %5d | %9ld\n", loop + 1, name, age, amount);
}
return 0;
}
Thank you in advance.
P.S.: This is not a homework assignment in any shape or way.
Re: annoying problem that's a pain to figure out...........
Quote:
Originally posted by ats007spdou
sprintf(temp[loop], "%s %d %ld", name[loop], rand() % 20 + 18, rand() + 27500L);
try
sprintf(temp[loop], "%s %d %ld", names[loop], rand() % 20 + 18, rand() + 27500);