Thread: Simple Display Problem?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,817
    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-05-2009, 11:05 PM
  2. Problem either on display method or structure..
    By DarrenY in forum C Programming
    Replies: 2
    Last Post: 09-04-2005, 08:16 AM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. problem with A simple modeless messagebox
    By hanhao in forum C++ Programming
    Replies: 8
    Last Post: 07-05-2005, 11:18 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM