Do I have a scanf problem?

This is a discussion on Do I have a scanf problem? within the C Programming forums, part of the General Programming Boards category; I am attempting to write a simple program that takes a user entered value and multiply it by a constant ...

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.

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:

Code:

int n;
scanf("%d", &n);

Same with:

Code:

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:

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.