Thread: Do I have a scanf problem?

  1. #1
    Registered User
    Join Date
    Nov 2004

    Do I have a scanf problem?

    I am attempting to write a simple program that takes a user entered value and multiply it by a constant to get a new answer and show it on the screen. To try to debug the process, I have the three values show up on the screen as separate printf statements prior to the formatted answer so that I can see if somehow the data is messed up. It appears that by initializing my input value (Org_Curr_Amt) to 0.00 and output value (New_Curr_Amt) to 0.00, I have gotten rid of the bad data that was appearing. Now it looks like the program is not reading my scanf statement properly to load Org_Curr_Amt. I will include my code to have you look at it to try to find what is wrong.

    	char  Menu_Opt[4]; 		/* Menu Option                 */
    	float Org_Curr_Amt = 0.00;	/* Original Currency Amount    */
    	float New_Curr_Amt = 0.00;	/* New Currency Amount         */
    	char  Currency_Desc[24][100] = {"American Dollar (USD)", "Australian Dollar (AUD)",
    	  "Brazilian Real (BRL)", "British Pound (GBP)", "Canadian Dollar (CAD)",
    	  "Chinese Yuan (CNY)", "Danish Krone (DKK)", "Euro (EUR)", "Hong Kong Dollar (HKD)", 
    	  "Indian Rupee (INR)", "Japanese Yen (JPY)", "Malaysian Ringgit (MYR)", "Mexican Peso (MXN)",
    	  "New Zealand Dollar (NZD)", "Norwegian Kroner (NOK)", "Singapore Dollar (SGD)", 
    	  "South African Rand (ZAR)", "South Korean Won (KRW)", "Sri Lanka Rupee (LKR)", 
    	  "Swedish Krona (SEK)", "Swiss Franc (CHF)", "Taiwan Dollar (TVD)", "Thai Baht (THB)",
    	  "Venezuelan Bolivar (YEB)"};
    float USD_to_AUD = 1.28436;  /* Australian Dollar (AUD)     */
    printf("Currency Conversion\n"); 
    		printf("United States Dollars Conversion Menu\n\n");
    		printf("A01.  USD to AUD\n");
    		printf("A02.  USD to BRL        Y02.  Transfer to AUD Conversion Menu\n");
    		printf("A03.  USD to GBP        Y03.  Transfer to BRL Conversion Menu\n");
    		printf("A04.  USD to CAD        Y04.  Transfer to GBP Conversion Menu\n");
    		printf("A05.  USD to CNY        Y05.  Transfer to CAD Conversion Menu\n");
    		printf("A06.  USD to DKK        Y06.  Transfer to CNY Conversion Menu\n");
    		printf("A07.  USD to EUR        Y07.  Transfer to DKK Conversion Menu\n");
    		printf("A08.  USD to HKD        Y08.  Transfer to EUR Conversion Menu\n");
    		printf("A09.  USD to INR        Y09.  Transfer to HKD Conversion Menu\n");
    		printf("A10.  USD to JPY        Y10.  Transfer to INR Conversion Menu\n");
    		printf("A11.  USD to MYR        Y11.  Transfer to JPY Conversion Menu\n");
    		printf("A12.  USD to MXN        Y12.  Transfer to MYR Conversion Menu\n");
    		printf("A13.  USD to NZD        Y13.  Transfer to MXN Conversion Menu\n");
    		printf("A14.  USD to NOK        Y14.  Transfer to NZD Conversion Menu\n");
    		printf("A15.  USD to SGD        Y15.  Transfer to NOK Conversion Menu\n");
    		printf("A16.  USD to ZAR        Y16.  Transfer to SGD Conversion Menu\n");
    		printf("A17.  USD to KRW        Y17.  Transfer to ZAR Conversion Menu\n");
    		printf("A18.  USD to LKR        Y18.  Transfer to KRW Conversion Menu\n");
    		printf("A19.  USD to SEK        Y19.  Transfer to LKR Conversion Menu\n");
    		printf("A20.  USD to CHF        Y20.  Transfer to SEK Conversion Menu\n");
    		printf("A21.  USD to TVD        Y21.  Transfer to CHF Conversion Menu\n");
    		printf("A22.  USD to THB        Y22.  Transfer to TVD Conversion Menu\n");
    		printf("A23.  USD to YEB        Y23.  Transfer to THB Conversion Menu\n");
    		printf("                        Y24.  Transfer to YEB Conversion Menu\n\n");
    		printf("Y99.  Exit Program\n");
    		printf("Option: "); 
    		scanf("%s", Menu_Opt);
    		if( strcmp(Menu_Opt,"A01") == 0) 
    			printf("\nPlease enter your value of ");
    			printf("%s ", Currency_Desc[0]);
    			scanf("%d\n", Org_Curr_Amt);
    			New_Curr_Amt = Org_Curr_Amt * USD_to_AUD;
    			printf("%f\n", Org_Curr_Amt);
    			printf("%f\n", USD_to_AUD);
    			printf("%f\n", New_Curr_Amt); 
    			printf("\nYour Equivalent "); 
    			printf("%s", Currency_Desc[1]); 
    			printf("is ");
    			printf("%f\n", New_Curr_Amt);
    Last edited by Salem; 11-26-2004 at 03:52 PM. Reason: Fixing code tags :(

  2. #2

    The second parameter of scanf, argument(s), are pointer to objects or structures to be filled with data read as specified by format string. There must be the same number of these parameters than the number of format tags.

    Important: These arguments must be pointers: if you want to store the result of a scanf operation on a standard variable you should precede it with the reference operator, i.e. an ampersand sign (&). For example:
    int n;
    scanf("%d", &n);
    Same with:
    scanf("%d\n", &Org_Curr_Amt);
    Though this syntax is wrong. Your variable is a floating-type variable, and using the wrong format type can cause input problems. The correct format modifier is %f or %g. Also, when calling scanf you do not need to include the '\n' in your string. For example, this should perform correctly:
    scanf("%g", &Org_Curr_Amt);
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Nov 2004

    Do I have a scanf problem?

    changing the program per the above instructions got it to work. Thanks for your help!

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, 05:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 12:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-04-2005, 11:42 PM
  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, 12:48 PM