Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple C problem.

Options
  • 12-10-2005 5:11pm
    #1
    Closed Accounts Posts: 3,783 ✭✭✭


    I'm relativly new to programming, I've been at it about four weeks. I'm having problem with a very simple c temperature conversion program, infact this happens quite a lot and I don't know what it is (Probably something very very simple). I have written the program and the code is as such.
    #include<stdio.h>
    main()
    {
    [INDENT]float tempf;
    float tempc;
    tempc = (tempf -32.0) * (5.0 / 9.0);
    printf("Temperature converter\n Please enter in a temerature in degrees fahrenheight:\n ");
    scanf("%f",&tempf);
    printf("\nThe temperature you entered converted to degrees celcius is: %f Degrees Celcius.\n",tempc);
    printf("End of Program.");
    getchar();[/INDENT]
    }
    
    There is no problem compiling the program but when I run it once Ienter in a farenheight value the program closes, or else displays the results for a milisecond and then closes. Can anyone help please?


Comments

  • Registered Users Posts: 112 ✭✭Paul_D


    float tempf;
    float tempc;
    printf("Temperature converter\n Please enter in a temerature in degrees fahrenheight:\n ");
    scanf("%f",&tempf);
    tempc = (tempf -32.0) * (5.0 / 9.0);
    printf("\nThe temperature you entered converted to degrees celcius is: %f Degrees Celcius.\n",tempc);
    printf("End of Program.");
    getchar();


  • Registered Users Posts: 26,578 ✭✭✭✭Creamy Goodness


    #include <stdio.h>
    #include <conio.h>
    main()
    {
    //declaring variables
    			float celsius ; 
    			float fahrenheit ; 
    //assigning values to variables
    			fahrenheit = 0 ; 
    //printf function
    		   printf("Enter a temperature in degrees Fahrenheit\n") ; 
    //scanf function
    			scanf("%f", &fahrenheit) ;  
    //conversion
    			celsius = (fahrenheit - 32.0)*(5.0/9.0) ;
    //printf function
    			printf("%f fahrenheit is equal to %f celsius\n", fahrenheit, celsius) ; 
    //scanf function
    		scanf("%f", &fahrenheit) ;
    getchar() ; 
    return 0 ;
    } 
    

    that should do it, return 0 should be enough for it to stay open or if not, insert that second scanf function


  • Closed Accounts Posts: 3,783 ✭✭✭Binomate


    Works now. Thanks for the help.


  • Registered Users Posts: 26,578 ✭✭✭✭Creamy Goodness


    no problem, and by the way you're behind on your C programming labs :D


  • Closed Accounts Posts: 3,783 ✭✭✭Binomate


    Cremo wrote:
    no problem, and by the way you're behind on your C programming labs :D
    Lol, I know. I didn't bother with C programming on Monday and came in an hour and a half late on Tuesday morning to catch up on the 10th stuff, got that all done within half an hour. Then I finished up the 11th stuff here at home today. It boredom ale. :D


  • Advertisement
  • Registered Users Posts: 131 ✭✭theexis


    Cremo wrote:
    that should do it, return 0 should be enough for it to stay open or if not, insert that second scanf function

    Just wanted to clarify something for you here; return 0 has no relevance to a program "staying open"; adding the second scanf is a hack that just worked for you. You should ask yourself why getchar() isn't working (i.e. no line of code should be "magic"!).

    Also, unless you're handing your code to someone who never programmed in their life I'm not sure why you need a comment like "assigning values to variables". More useful would be to make constant and comment the relevance of the numbers involved in the conversion.

    Don't take this as a rant - just a few pointers. :)


Advertisement