Simple Display Problem?

This is a discussion on Simple Display Problem? within the C Programming forums, part of the General Programming Boards category; Howdy. I recently started using the Dev-C++ compiler, and ran into a bit of a problem. I'm new to the ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    MA
    Posts
    5

    Simple Display Problem?

    Howdy. I recently started using the Dev-C++ compiler, and ran into a bit of a problem. I'm new to the whole C programming thing, so bear with me

    I made a simple Celsius to Fahrenheit conversion program, but I'm having an issue getting the out put to remain on the screen. It will flash briefly, and then exit when the program is finished. Any advice on how to keep the display showing on my screen once the program has finished running?

    Code:

    Code:
    #include <stdio.h>
    
    main(){
           
    float fahr, cels;
    int lower, upper, step;
    
    lower = 0;
    upper = 20;
    step = 1;
    
    cels = lower;
    while( cels <= upper){
           fahr = (9/5)*(cels + 32);
           printf("%f\t %6.1f\n", cels,fahr);
           cels = cels + step;
           }
           
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Read the FAQ on How do I... Stop my Windows Console from disappearing everytime I run my program?

    By the way, use int main(void), not just main(). You should to return 0 at the end of main if this is not C99.
    Last edited by laserlight; 01-03-2008 at 02:34 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Quote Originally Posted by portable View Post
    Code:
    #include <stdio.h>
    
    int main(){
    	float fahr, cels;
    	int lower, upper, step;
    
    	lower = 0;
    	upper = 20;
    	step = 1;
    
    	cels = lower;
    	while( cels <= upper){
    		fahr = (9/5)*(cels + 32);
    		printf("&#37;f\t %6.1f\n", cels,fahr);
    		cels = cels + step;
    	}
    	return 0;
    }
    You should probably learn to indent properly too. Each block should be indented once as I show in my demonstration above.
    Makes readable code, makes everyone happy when you post code they have to search for mistakes on your behalf.
    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.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    fahr = (9/5)*(cels + 32);
    There are issues with that.

    First, since both 9 and 5 are integers, the division is done using integer division rules and 9/5 becomes 1... so you're basically saying fahr = 1*(cels+32) in that loop. You need to use floating-point division so make either (both) of those values floating-point numbers and you'll be doing much better.

    Second, the overall calculation is incorrect. It should be:
    Code:
    fahr = ((9.0/5.0)*cels + 32);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Surely this is a case where a for-loop is appropriate:

    Code:
    	cels = lower;
    	while( cels <= upper){
    		fahr = (9/5)*(cels + 32);
    		printf("%f\t %6.1f\n", cels,fahr);
    		cels = cels + step;
            }
    Like this:
    Code:
    	for(cels = lower; cels <= upper; cels += step) {
    		fahr = (9/5)*(cels + 32);
    		printf("%f\t %6.1f\n", cels,fahr);
            }
    Note: I have just modified the original code to show the for-loop, so any other problems in the code will still be there in the second code-snippet too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-06-2009, 12:05 AM
  2. Problem either on display method or structure..
    By DarrenY in forum C Programming
    Replies: 2
    Last Post: 09-04-2005, 09:16 AM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 01:09 AM
  4. problem with A simple modeless messagebox
    By hanhao in forum C++ Programming
    Replies: 8
    Last Post: 07-06-2005, 12:18 AM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM

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