Thread: Need help with final touch

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    13

    Need help with final touch

    hey what i tried to do was write a program that asks user to enter integer numbers, keeps track of the largest and smallest numbers entered, ends if 0 is entered. Then prints the number of entries, min, and max.

    This is what i made:

    Code:
     
    #include "stdio.h"
    
    void main() {
    
    	int max;
    	int min;
    	int index = 0;
    	int input;
    
    	
    	printf("Enter integer numbers: ");
    	
    		scanf("%d\n", &input); //initialze the "input" variable
    		index++; 
    		max = input; //initialize "max"
    		min = input; //initialize "min"
    
    		while (input != 0) {
    			
    			scanf("%d", &input);
    
    			index++;
    			
    			if(input>max){
    				max=input;
    			} //end of if 1
    			if(input<min){
    				min=input;
    			} //end of if 2
    
    		} //end of 'while' loop
    		
    		printf("\n The number of integers you entered was:  %d", index);	
    		printf("\n The largest integer you entered was:  %d", max);	
    		printf("\n The smallest integer you entered was:  %d", min);	
    		
    }
    It almost works fine expect one problem, it always returns the min value as 0, because its what i use to terminate the loop. how can i prevent it from doing this? i was thinking i could make the if statement say something like: "if input is less than min but not equal to 0", i just dont know how this would be done.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Detect the end of the loop as you input the value, not after you've "processed it by mistake".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    Quote Originally Posted by Salem View Post
    Detect the end of the loop as you input the value, not after you've "processed it by mistake".
    how?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by MaaaTty
    how?
    Depends, could be as simple as testing for 0 immediately after the scanf and breaking out of the loop at that point... or reordering some of the code in the loop so that the scanf is at the bottom of the while loop code block.
    "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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > how?
    How about an if statement?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    MaaaTtY, have you by chance taken a look at Salem's avatar?
    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. DirectX dll Final Debug and Final Retail
    By hdragon in forum Tech Board
    Replies: 0
    Last Post: 11-15-2005, 09:46 PM
  2. Final Fantasy I
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 03-28-2004, 11:53 PM
  3. reviewing for final exam, need help with this question
    By jlmac2001 in forum C++ Programming
    Replies: 12
    Last Post: 11-26-2003, 08:37 AM
  4. Final year project
    By khpuce in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 10-10-2003, 07:04 AM
  5. Game Components - final year project
    By gazsux in forum Game Programming
    Replies: 17
    Last Post: 06-14-2003, 08:09 PM