Thread: scanf problem?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    scanf problem?

    Hey there, ill start off by saying im quite fresh with C, but im trying

    I'm having a problem with some code here:


    Code:
    /* Program:	Course Project - CD Inventory
     * Version: 	1.0
     * Author: 	Joe
     * 
     * This program is used to track its inventory of CD's
     * 
     * Version 1.0
     * 	- Only Reads input from user, and displays input
     * 	- Only Reads in Title/Artist/Number of Tracks/(Album or Single)/Price
     * 	- Does not save user input once program has finished
     * 	- Only stores infomation about ONE CD
     */
    
    #include <stdio.h>
    #include <string.h> 	// string library to access string functions
    
    int main(void)
    {
        char Title[101];	// Store track Title, extra 1 for NULL char
        char Artist[101];	// Store Artist Name, extra 1 for NULL char
        short nTracks;	// Store No. of Tracks				
        short Album; 	// Store Album/Singles as int
        char type;		// For figuring out Albulm or single
        float Price;	// Store price as double to store cents as well
    	
    	
        printf("\n******** CD Inventory v 1.0 ********\n");
    	
        /* The title */
        
        printf("Please enter the Title of the CD: ");
        scanf("%s", Title);
    
        /* The artist */
    
        printf("Please enter the Artist: ");
        scanf("%s", Artist);
    
        /* The tracks */
    	
        printf("Please enter the Number of tracks on the CD: ");
        scanf("%d", &nTracks);
    	
        /* The Price */
    	
        printf("Please enter the Price of the CD: $ ");
        scanf("%f", &Price);
    	
        /* Album or Single */
    	
        printf("Album or Single? Type a for album or s for single: ");
        scanf("%c", &type);
        Album = type == 'a';
    	
        /* Output of CD details */
    
        printf("\n========== CD Details =========\n"); 
        printf("Title: %s\n", Title);
        printf("Artist: %s\n", Artist);
        printf("Tracks: %d\n", nTracks);
        printf("Price: $%.2f\n", Price);
        
        if(Album)
            printf("Album\n");
        else
            printf("Single\n");
    		
        getchar();
        
        return 0;
    }
    Problem being that after the program asks for the price of the CD, it skips the code:

    Code:
    /* Album or Single */
    	
    printf("Album or Single? Type a for albulm or s for single: ");
    scanf("%c", &type);
    Album = type == 'a';
    And i have no idea why, it's driving me crazy, help MUCH appreciated!

  2. #2
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    As many beginners you forgot to use '&'.
    gavra.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Use fgets() to read strings (and remember to strip the newline char), not scanf().

    Try putting a getchar() before the scanf() to read a char. A newline char is probably still left in the input buffer from your previous scanf() calls.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Quote Originally Posted by MacGyver View Post
    Use fgets() to read strings (and remember to strip the newline char), not scanf().

    Try putting a getchar() before the scanf() to read a char. A newline char is probably still left in the input buffer from your previous scanf() calls.
    Ahhh ok i see, thanks for that, the getchar() worked perfectly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 01:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-05-2005, 12:42 AM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 01:48 PM