How to convert character into integer?

This is a discussion on How to convert character into integer? within the C Programming forums, part of the General Programming Boards category; Basically my program specifications require that I NOT use arrays. I want to convert a string into its integer values. ...

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

    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    To read a single character, use the function getchar().

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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
    140
    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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,561
    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, 03: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, 07:32 AM
  5. Convert a character to integer (ex '1' to 1)
    By VanJay011379 in forum C++ Programming
    Replies: 11
    Last Post: 03-18-2003, 12:14 AM

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