ASCII values help

This is a discussion on ASCII values help within the C Programming forums, part of the General Programming Boards category; Hi, i am currently trying to write a program that can read 4 input characters (ie what a user selects) ...

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

    ASCII values help

    Hi, i am currently trying to write a program that can read 4 input characters (ie what a user selects) and then convert the character into its ASCII decimal number. I then need the sum of these characters eg if AAAA was inputted (65+65+65+65) then the output should show 260.

    Atm the this is what i have:
    Code:
    #include <stdio.h>
    int main( void )
    {
    	     
    char input_letter;				
    				              
    input_letter = getchar(); 			
    	  
    printf("%d", input_letter ) ;
    }
    i know i'll probably need some kind of array to indetify the other 3 characters, but where im new to the language, i was wondering if anyone knows some solutions

    Grant Searle.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You don't need an array per say. You can just loop around what you are doing now and add a variable to sum the values up. Are you familiar with loops? If not, I suggest checking the FAQ of this site.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    #include <stdio.h>
    
    int main( void )
    {
    #define LETTERNUM 4	     
      char input_letter[LETTERNUM+1];
      int i, sum = 0;
      
      fgets( input_letter, sizeof input_letter, stdin );
      if( strchr( input_letter, '\n' ) ) *strchr( input_letter, '\n' ) = 0;
      if( strlen(input_letter) != LETTERNUM )
      {
        puts("wrong letter num");
        return 1;
      }
    
      for( i=0; i<LETTERNUM; ++i )
        sum += (unsigned char) input_letter[i];
    
      printf("\nsum = %d", sum );
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Great idea's, but it could be done very simply:

    Code:
    #include <stdio.h>
    int main( void )
    {
      char input_letter;				
      int sum = 0;
    				              
      input_letter = getchar(); 			
      sum += input_letter;    //+= is the shorthand for sum = sum+ input_letter
    
      input_letter = getchar();             
      sum += input_letter;
      	  
      input_letter = getchar(); 			
      sum += input_letter;
    
      input_letter = getchar(); 			
      sum += input_letter;
    
      printf("%d", sum ) ;
      return 0;
    }
    That's as simple as it gets, I think. Boring, also!

    Great having you in the forum though, Grant_Searle!

    Now, what about putting the above, into a for loop:
    Code:
    int i;
    
    for(i=0;i<4;i++)  {
      input_letter=getchar();
      sum += input_letter;
    }
    
    //Now what should we do?? :p

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You must input 4 times a Enter, sum is wrong for extended ASCII values.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    hey guys,
    firstly thanks for all your feedback, after some reading of the FAQ's, suggestions from Adak and a bit of common sense i have written working code.
    Yes, it is boring haha but im sure i can elaborate once i understand the language a bit better.
    I will look more into loops and arrays for future programs

    Once again, thankyou.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  3. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 03:04 AM
  4. adding ASCII
    By watshamacalit in forum C Programming
    Replies: 0
    Last Post: 12-26-2002, 04:25 PM
  5. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 09:59 PM

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