convering char to int problems

This is a discussion on convering char to int problems within the C Programming forums, part of the General Programming Boards category; I'm writing a practice program to read in two characters (the first character coming first alphabetically) and then print out ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    66

    convering char to int problems

    I'm writing a practice program to read in two characters (the first character coming first alphabetically) and then print out the letters between the two entered( including the inputs) in alphabetical order. what my problem is... well i dont really know what i am doing wrong but im getting crazy outputs. but im not using the ascii numbers for the letters just A=0, B=1, C=2, etc. can anyone help me with this issue? thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    char   startchar;
    char   endchar;
    int      i = 0;
    char   s;
    char   e;
    
    
    void charbtwn(char s, char e);
    
    int main(void) {
    
        scanf("%c %c", &startchar, &endchar);
        charbtwn(startchar,endchar);
        
    system("pause");
    return(0);
    }
    
    void charbtwn(char s, char e) {
        
        for(i=s; i<=e; i++) {
            putchar('A' + i);
        }         
    }

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Change
    Code:
     putchar('A' + i);
    to
    Code:
     putchar( i);
    I am not sure what the ASCII for 'A' is but I am pretty sure (certain) it is not 0.
    Probably 48 or 74 at a guess

    Actually it is 65

    SO I presume you are getting some of these
    http://www.asciitable.com/extend.gif

    The lowest code would be 130 (65+65) when you type in an 'A' and 187 when you type in a 'z'.

    Could be a useful way of encrypting emails though
    Last edited by esbo; 04-01-2009 at 12:01 AM.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    stop using system("pause"); use getchar() instead, and if you have to clean out the junk scanf has in the buffer still just use something like this:
    Code:
    #define CLEAR_BUFFER (while (getchar() !='\n') )
    get into the habit of writing good clean C code and know why you're doing it too.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    get rid of global variables - define them in the smallest scope possible...

    for example i is used only in the charbtwn - so it should be local for this function

    also its type should be char - because you assign char to it and compare it to another char
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Quote Originally Posted by esbo View Post
    Change
    Code:
     putchar('A' + i);
    to
    Code:
     putchar( i);
    I am not sure what the ASCII for 'A' is but I am pretty sure (certain) it is not 0.
    Probably 48 or 74 at a guess

    Actually it is 65

    SO I presume you are getting some of these
    http://www.asciitable.com/extend.gif

    The lowest code would be 130 (65+65) when you type in an 'A' and 187 when you type in a 'z'.

    Could be a useful way of encrypting emails though


    when the code is just
    Code:
    puchar('A' + 2)
    the out put will be 'C'
    'A' + 1 will be 'B'
    'A' + 5 will print 'F'
    etc.

    this is the logic i am using to write the program.

    in the for loop i am starting "i" at the character the user inputs for 'startchar' then incrementing it by 1 untill i=endchar.

    could this problem be because i am using scanf instead of getchar()?

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    sorry esbo, you were right. i just had to take out that <'A' +> . thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  2. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  3. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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