Thread: Advice for the new boy!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    15

    Advice for the new boy!

    Hello, I have written this program, forgive me , this is my first program after " hello world".

    Code:
    # include <stdio.h> /*  */
    #include <stdlib.h> /*  */
    
    
    int main(void)/* */
    
    {
    
    	int sum, char1, char2, char3, char4;/*  */
    	
    	printf( "Please enter four characters:\n"); /**/
    
    	char1 = getchar();/**/
    	char2 = getchar();/**/
    	char3 = getchar();/**/
    	char4 = getchar();/**/
    	
        printf("The first character you entered is: %c,", char1 );/**/
        printf(" the ASCII value is %d.\n", char1 );
        printf("The second character you entered is: %c,", char2 );
        printf(" the ASCII value is %d.\n", char2 );
        printf("The third character you entered is: %c,", char3 );
        printf(" the ASCII value is %d.\n", char3 );
        printf("The fourth character you entered is: %c,", char4 );
        printf(" the ASCIIV value is %d.\n", char4 );
    
        sum = char1 + char2 + char3 +char4;
        printf("The sum of the ASCII values = %d\n",sum);
        
        system("pause");/**/
        
      
    
    }
    How can i make the program display an error message if the user enters more than four characters?
    Also any feedback on how I can improve the program?
    Last edited by Salem; 10-13-2010 at 11:47 AM. Reason: Your one free edit for adding code tags - read the intro threads to find out how to do it

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Aren't new boys supposed to read this.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    you can improve your program by replacing the 4 char variables with a character array
    you can improve the advice you get here by using [code] tags when posting code
    to check if they entered more than 4 characters, read in a 5th, think about what it should equal, and then check if it does equal that
    also, learn to properly document your code, it will save you hours of trouble when you start writing more complicated programs
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  2. Advice for breaking into the industry
    By Trennto in forum Game Programming
    Replies: 9
    Last Post: 07-03-2008, 07:47 PM
  3. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  4. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM