|
-
April 12th, 2004, 11:53 AM
#1
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 . 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.
Last edited by ats007spdou; April 12th, 2004 at 11:55 AM.
Favorite music:
Rammstein
E nomine
Prodigy
"Beer, the solution and the cause of all of our problems" -- Homer Simpson
-
April 12th, 2004, 12:03 PM
#2
The line :
Code:
sprintf(temp[loop], "%s %d %ld", name[loop], rand() % 20 + 18, rand() + 27500L);
should be :
Code:
sprintf(temp[loop], "%s %d %ld", names[loop], rand() % 20 + 18, rand() + 27500L);
"name" should be "names"
-
April 12th, 2004, 12:04 PM
#3
Re: annoying problem that's a pain to figure out...........
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);
-
April 12th, 2004, 01:26 PM
#4
I am such a tool, thanks........
Favorite music:
Rammstein
E nomine
Prodigy
"Beer, the solution and the cause of all of our problems" -- Homer Simpson
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|