Thread: scanf problems in visual studio 2010

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    scanf problems in visual studio 2010

    Hi guys I started to practice C programming a few days ago, and I was asked by the tutorial video to write a program for a CD database. My problem is my code executes the next two printf statements if I enter words separated by a space, is it because of my scanf syntax? Any help is greatly appreciated.

    Code:
    #include <iostream>
    
    
    int main()
    {
        char title[40], artist[40], as[2]; 
        int price, tracks; 
    
    
        printf ("CD DATABASE\n");
        printf ("PLEASE ENTER THE TITLE OF THE CD\n");
        scanf ("%s", &title);
        printf ("PLEASE ENTER THE NAME OF THE ARTIST\n");
        scanf ("%s", &artist);
        printf ("PLEASE ENTER THE NUBER OF TRACKS\n");
        scanf ("%d", &tracks);
        printf ("PLEASE TYPE (a) FOR ALBUM AND (s) FOR SINGLE\n");
        scanf ("%s", &as); 
        printf ("PLEASE ENTER THE PRICE OF THE CD\n");
        scanf ("%d", &price);
    
    
        printf ("=====CD INFORMATION=====\n");
        printf ("CD TITLE: %s\n", title);
        printf ("ARTIST: %s\n", artist);
        printf ("NUMBER OF TRACKS: %d\n", tracks);
        
        if (strcmp(as,"a") == 0)
        {
            printf ("TYPE: ALBUM \n");
        }
        else
        {
            printf ("TYPE: SINGLE \n");
        }
    
    
        printf ("PRICE: %d\n", price);
        printf ("=====CD INFORMATION=====\n");
        fflush (stdin);
        getchar ();
    }
    Last edited by kills101; 11-14-2012 at 08:13 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    scanf requires you to pass the address of the variable, thus
    Code:
    int var;
    scanf("%d",&var);
    Code:
    char varStr[10];
    scanf("%s",varStr);

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    The %s specifier stops scanning a string at the first whitespace character. You can use the fgets function to read a whole line instead (but note that it stores the newline character at the end if it was present in the input and if there is enough space in the buffer). Alternatively you can use the %[ scanf specifier to read a string up to but not including the newline.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1 <iostream> is a C++ header. For the functions you've used in your program above you should include the <stdio.h> and <string.h> headers instead.




    #2
    Code:
    fflush (stdin);
    Read: FAQ > Explanations of... > Why fflush(stdin) is wrong
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Thanks guys

    Thanks for the suggestion guys I finally found the solution to my problem, I tried to insert the fflush before every scanf and changed the header files to stdio instead of iostream.

    Code:
    #include <stdio.h>
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
        char title[40], artist[40], as[2]; 
        float price;
    	int tracks; 
    
    
        printf ("CD DATABASE\n");
        printf ("PLEASE ENTER THE TITLE OF THE CD\n");
        scanf ("%[^\n]", &title);
        printf ("PLEASE ENTER THE NAME OF THE ARTIST\n");
    	fflush(stdin);
        scanf ("%[^\n]", &artist);
        printf ("PLEASE ENTER THE NUBER OF TRACKS\n");
    	fflush(stdin);
        scanf ("%d", &tracks);
        printf ("PLEASE TYPE (a) FOR ALBUM AND (s) FOR SINGLE\n");
    	fflush(stdin);
        scanf ("%[^\n]", &as); 
        printf ("PLEASE ENTER THE PRICE OF THE CD\n");
    	fflush(stdin);
        scanf ("%f", &price);
    
    
        printf ("=====CD INFORMATION=====\n");
        printf ("CD TITLE: %s\n", title);
        printf ("ARTIST: %s\n", artist);
        printf ("NUMBER OF TRACKS: %d\n", tracks);
        
        if (strcmp(as,"a") == 0)
        {
            printf ("TYPE: ALBUM \n");
        }
        else
        {
            printf ("TYPE: SINGLE \n");
        }
    
    
        printf ("PRICE: %.2f\n", price);
        printf ("=====CD INFORMATION=====\n");
        fflush (stdin);
        getchar ();
    }

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Even after you read the link to "why fflush(stdin) is wrong"?...
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio 2010 .exe help
    By omGeeK in forum Game Programming
    Replies: 4
    Last Post: 03-20-2011, 01:36 PM
  2. Visual Studio 2010 - so where is it now?
    By Devils Child in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2010, 12:51 PM
  3. Visual Studio 2010 Help
    By dnelsalty in forum C++ Programming
    Replies: 15
    Last Post: 08-30-2009, 06:07 PM
  4. Windows 7 RC, Visual Studio 2010 Beta, Office 2010 beta, anyone in?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-20-2009, 01:57 PM