CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2003
    Location
    Regensburg, Bavaria, Germany
    Posts
    147

    Unhappy 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

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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"

  3. #3
    Join Date
    Apr 2002
    Posts
    61

    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);

  4. #4
    Join Date
    Apr 2003
    Location
    Regensburg, Bavaria, Germany
    Posts
    147
    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
  •  





Click Here to Expand Forum to Full Width

Featured