Thread: How to convert character into integer?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    How to convert character into integer?

    Basically my program specifications require that I NOT use arrays.
    I want to convert a string into its integer values. For example
    printf("Please enter a word");

    How would i scanf each indivudual character the user inputs?

    Code:
    printf("Please enter a word");
    scanf(
    printf("The total value of the characters are: );

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    To read a single character, use the function getchar().

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by qny View Post
    To read a single character, use the function getchar().
    Not sure if that would work though.

    Let's say user enters This is short.

    How would i store all the values of 'T' 'h' 'i' 's' '(space) 'i' 's' '(space) 's' 'h' 'o' 'r' 't'

    scanf loop?
    Last edited by tmac619619; 11-01-2012 at 03:20 PM.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You don't store values. The description specifically forbids the use of arrays.
    Where would you store the characters? ???

    Read them, one by one, and deal with them, again one by one.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by qny View Post
    You don't store values. The description specifically forbids the use of arrays.
    Where would you store the characters? ???

    Read them, one by one, and deal with them, again one by one.
    I think i almost have it.
    Code:
    int main()
    {
    printf("Line: ");
    char mynum;
    scanf("%c",&mynum);
    
    
    
    
    
    
    
    while(mynum!='.')
    {
    While the input is not '.', how would i add up the values?

    let's say input is 'This'

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    So ... answer me this:
    what is the value of "This"?
    Or what output do you expect for an input of "This"?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    EDIT: This assumes you want to total up the numeric values associated with each character the user inputs:

    A char is just like a tiny little int. We tend to think of them as being letters/punctuation, and express them as 'T', 'h', etc, but they do have numeric values. Take a look at an ASCII chart (link) for an example of one mapping of letters to numbers (there are others, but they're much less common, and you are almost certainly not using them).

    That means mynum is already a number, so you just add it to some other variable that keeps track of the sum. Make that other variable a regular int, so there's room to total up all the chars.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert integer to character
    By Swoorup in forum C++ Programming
    Replies: 20
    Last Post: 11-28-2011, 04:53 AM
  2. Convert integer to char..
    By sniper83 in forum C Programming
    Replies: 7
    Last Post: 07-18-2007, 01:23 PM
  3. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  4. convert to integer...
    By compile in forum C Programming
    Replies: 5
    Last Post: 01-22-2007, 08:32 AM