Thread: simple program weird result

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    Post simple program weird result

    Hi fellow C programmers,
    New to programming in C. The code below works fine showing everything except for tracks. It shows 0 tracks no matter what i put in. Can you tell me whats wrong?
    Code:
    #include <stdio.h>
    main(){
           char title[20];
           char artist[10];
           int tracks;
           char type[1];
           int price;
           
           printf("Enter CD Title: \n");
           scanf("%s",title);
          
           printf("Enter Artist Name: \n");
           fflush(stdin);
           scanf("%[^\n]",artist);
           
           printf("Enter number of tracks on CD: \n");
           scanf("%d",&tracks);
           
           printf("Enter 'A' for Album or 'S' for single: \n");
           scanf("%s",type);
           
           printf("Enter CD Price: \n");
           scanf("%d",&price);
           
           printf(" CD Information \n");
           printf("-------------------\n");
           printf(" Title: %s , Artist: %s , Tracks: %d , Album/Single: %s , Price: %d \n",title,artist,tracks,type,price);
    
           fflush(stdin);
           getchar();
    }
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fflush(stdin);
    This isn't defined - see the FAQ

    > char type[1];
    You need at least 2 chars to store a string, otherwise you just trash something else.
    Which in your case seems to be the int track;

    Rather than using scanf, just use fgets() to read a whole line, then use sscanf to extract information from the line.
    Then all that need for fflush() and weird scanf usage goes away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can you tell me whats wrong?
    Yes. You've discovered a subtle problem with the ordering of your variables and array overflow. When you get here:
    Code:
    scanf("%s",type);
    You presumably type 'S' or 'A', right? Well, what actually gets written is 'S' or 'A' to type[0] and 0 to type[1]. However, type doesn't have an index 1, only an index 0. Your compiler happens to store the address of tracks where type[1] would be, so 0 gets written there. Try changing the size of type to 2.

    You also have other problems. Please read our programming FAQ for detailed info.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Thanks that helped. I am new to programming so i am learning from VTC video.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-10-2006, 03:03 PM
  2. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 03:46 PM
  3. Help with simple program
    By nik2007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2006, 09:54 AM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  5. I need help on a formula for this simple program.
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2002, 10:01 PM