Thread: very simple code, please check to see whats wrong

  1. #1
    Unregistered
    Guest

    very simple code, please check to see whats wrong

    this is code i just wrote(im just learning c). for some reason, this program complies fine, and it runs fine, but when i enter a number for inches, and then press enter it says "Proj3 has caused an error in PROJ3.EXE. Proj3 will now close." i tried restarting my pc, but it still gives me that problem. i dont see anything wrong with the code, so if someone could try and run it on their pc, id appreciate it. btw, im running microsoft visual studio 6.0 enterprise or professional edition(dont remember what its called). thanks alot!


    #include <stdio.h>

    main()
    {

    double inch, cent;

    printf("this program converts inches to centimeters.\n");
    printf("how many inches?");
    scanf("%f", inch);

    cent=inch*2.54;

    printf("%f inches is equal to %f. centimeters.\n", inch, cent);

    }

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Use and ampersand '&' when you scan a number with scanf. If you scan a string than you don't need the ampersand:

    I think %lf rather than %f is appropriate for type 'double' try it.

    scanf("%lf", &inch);

    Code:
    #include <stdio.h> 
    
    int main() 
    { 
    
    	double inch, cent; 
    
    	printf("this program converts inches to centimeters.\n"); 
    	printf("how many inches?"); 
    	scanf("%lf", &inch); 
    
    	cent=inch*2.54; 
    
    	printf("%lf inches is equal to %lf. centimeters.\n", inch, cent); 
    
    	return 0;
    }
    Last edited by Troll_King; 10-09-2001 at 11:47 PM.

  3. #3
    Unregistered
    Guest
    crap, cant believe i missed something that easy. thanks!!!!

  4. #4
    Unregistered
    Guest
    %f is fine for doubles. As a matter of fact, to quote the MSDN:
    Code:
    f = format speficier //These two lines are my pharaphrase,
    type = double        //the MSDN uses a table to display these.
    
    //This is a quote of the definition of the %f type specifier:
    
    Signed value having the form [ – ]dddd.dddd,
    where dddd is one or more decimal digits. The
    number of digits before the decimal point depends
    on the magnitude of the number,  and the
    number of digits after the decimal point depends
    on the requested precision.
    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird error in this simple code! C2248!
    By gross100 in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2005, 01:31 AM
  2. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  3. Check out my code
    By joshdick in forum C++ Programming
    Replies: 9
    Last Post: 09-17-2003, 07:41 PM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM