Thread: Needed help.

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    Needed help.

    What's wrong with this code? (I'm trying for the program to print the highest number)

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    	int num,num2,num3;
    
    	printf("Please enter the first number: ");
    	scanf("%d", &num);
    	printf("Please enter the second number: ");
    	scanf("%d", &num2);
    	printf("Please enter the third number: ");
    	scanf("%d", &num3);
    
    	if (num > num2 && num > num3)
    		printf("The highest number is: %dn", num);
    	if (num2 > num && num2 >num3)
    		printf("The highest number is %dn", num2);
    	if (num3 > num && num3 > num 2)
    		printf("The highest number is %dn", num3);
    
    	system("PAUSE");
    }

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Well, what is the problem, list your input, what the program outputs and the expected output.

    Also, use getchar() instead of system("pause").

    You declare main as an int (as per standard), but you don't return any values.
    Last edited by Wraithan; 01-19-2007 at 06:40 PM.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Denisius
    What's wrong with this code?
    There are a number of points that could be made - Wraithan having already mentioned a couple - but I would suggest that in order to make your program work, look very closely at your variable names all through your program. Check for consistency.

    Now then, as for the other things; If you want to be a good C programmer, you are going to have to get familiar with the standard library. Seasoned C coders will tell you that scanf is not the best way to get your integer input. By using scanf, there are just too many problems introduced for what it is worth. You would do well to look into using an fgets + sscanf combo, or perhaps a fgets + strtol combination. All of these functions, especially strtol, provide good error checking so you can control what happens with the user input. Jack Klein offers a good example of strtol in action.

    Read your documentation regarding escape sequences. A newline is represented in this manner:

    Code:
    '\n'
    You can pare down your code a little as well:

    Code:
    if (num > num2 && num > num3) {
            printf("The highest number is: %d\n", num);
        } else if (num2 > num && num2 > num3) {
            printf("The highest number is %d\n", num2);
        } else { /* Neither num1 or num2 is highest
    	      * therefore num3 must be highest.
    	      */
            printf("The highest number is %d\n", num3);
        }
    Finally, turn the warnings up on your compiler. This is very helpful when you have a very tiny / sometimes hard to see bug as the one in your program. It will at least tell you what line to look at, so you don't waste your time poring over all of the code.

    ~/
    Last edited by kermit; 01-19-2007 at 07:50 PM.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    I think i have found the problem, thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM