Why this prog crashes?

This is a discussion on Why this prog crashes? within the C Programming forums, part of the General Programming Boards category; tconverter.c Code: #include <stdio.h> int choice; float cels(float x); float fahr(float x); float kelv(float x); int main() { float in, ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Question Why this prog crashes?

    tconverter.c
    Code:
    #include <stdio.h>
    int choice;
    
    float cels(float x);
    float fahr(float x);
    float kelv(float x);
    int main()
    {
    	float in, out;
    	
    	while (true){
    	in = out = 0;
    	choice = 0;
    	printf("Wich type of c0nversions do you want to make?\n");
    	
    	/* The Choice function */
    	printf("1)Celsius -> Fahrenheit\n");
    	printf("2)Celsius -> Kelvin\n");
    	printf("3)Fahrenheit -> Celsius\n");
    	printf("4)Fahrenheit -> Kelvin\n");
    	printf("5)Kelvin -> Celsius\n");
    	printf("6)Kelvin -> Fahrenheit\n");
    	
    	scanf("%d", choice);
    	printf("Value: ");
    	scanf("%f", in);
    	
    	switch (choice) {
    		case 1:
    			out = cels(in);
    			break;
    		case 2:
    			out = cels(in);
    			break;
    		case 3:
    			out = fahr(in);
    			break;
    		case 4:
    			out = fahr(in);
    			break;
    		case 5:
    			out = kelv(in);
    			break;
    		case 6:
    			out = kelv(in);
    			break;
    	}
    	
    	printf("Output: %f\n", out);
    	}
    }
    
    float cels(float x)
    {
    	float f, k;
    	
    	if (choice == 1){
    		f = x * 1,8 + 32;
    		return f;
    	}
    	if (choice == 2){
    		k = x + 273.15;
    		return k;
    	}
    }
    
    float fahr(float x)
    {
    	float c, k;
    	
    	if (choice == 3){
    		c = (x - 32) / 1.8;
    		return c;
    	}
    	if (choice == 4){
    		k = (x + 459.67) / 1.8;
    		return k;
    	}
    }
    
    float kelv(float x)
    {
    	float c, f;
    	
    	if (choice == 5){
    		c = x - 273.15;
    		return x;
    	}
    	if (choice == 6){
    		f = x * 1.8 - 459.67;
    		return f;
    	}
    }
    When I run this prog, it crashes by the first input, why? (BTW, my first serious prog

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Code:
    scanf("%d", choice);
    The second argument to scanf requires an address. You get the address of the variable by using the address-of operator, &.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Thanks mate, didn't see it.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    While you're at it, you should indent the while loop.
    The second scanf statement also requires fixing, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prog crashes upon read of system file
    By Queatrix in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2007, 11:28 PM
  2. Program crashes and memory leaks
    By ulillillia in forum Tech Board
    Replies: 1
    Last Post: 05-15-2007, 11:54 PM
  3. dont know why prog crashes
    By mattyans in forum C Programming
    Replies: 22
    Last Post: 05-24-2002, 07:16 PM
  4. Exe crashes with Stack Dump
    By Shaman in forum C++ Programming
    Replies: 4
    Last Post: 10-19-2001, 03:24 PM
  5. Program crashes and I can't figure out why
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-19-2001, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21