Thread: Char to Ascii

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    43

    Char to Ascii

    I need to create a program that converts a Character to Ascii, it is easy enough but I am having trouble with loops.

    If the person inputs the # key the program must close, I can't seem to figure out how to do this

    Code:
    #include <stdio.h>
    
    int main()
    
    {
        int x;
        char letter;
    
    
        printf("Enter a Letter\n");
        printf("Press '#' at any time to quit\n");
    
    while("char != #")
        scanf("%c", &letter);
        printf("Ascii Value= %d", letter);
    
    
    }
    I assumed that would work but it did not

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First off, "char != #" is a string literal, so you might as well be passing "foobarbaz" - the result is the same. Drop the quotes. Second, (barring locale-specific issues) the char is already going to be in ASCII, conveniently enough, so no conversion is necessary.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Sebastiani View Post
    First off, "char != #" is a string literal, so you might as well be passing "foobarbaz" - the result is the same. Drop the quotes. Second, (barring locale-specific issues) the char is already going to be in ASCII, conveniently enough, so no conversion is necessary.
    After dropping the quotes, you'll also need to put a real expression in there. char is a data type, not a variable, and # should be in single quotes: '#'
    You need to initialize the variable you use in place of the "char" you currently have...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    No offence here, but I'm always amazed what kind of constructs newbies come up with. How the heck do people even get to such a point. Really, one day we should collect ALL those constructs and write a programming language that supports them all. That's gotta be the easiest to learn language ever.
    Even though the same line could mean about a dozen different things..

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by EVOEx View Post
    No offence here, but I'm always amazed what kind of constructs newbies come up with. How the heck do people even get to such a point. Really, one day we should collect ALL those constructs and write a programming language that supports them all. That's gotta be the easiest to learn language ever.
    Even though the same line could mean about a dozen different things..
    Isn't that called Perl?
    No wait, it's the other way around. A dozen different lines can mean the same thing.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    I am confused

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by DJ_Steve View Post
    I am confused
    Whats the confusion now? You already got the solution to your problem in the above posts....

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe the biggest problem is the "make this happen" approach, which computers do not understand. You must be specific and tell it how to accomplish "this". The computer can't read and interpret. It's kind of like mathematics.
    Have you read some C book or tutorial?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    just look up some example code of while loops it will become clear immediately what is wrong, your program will excute the loop if you change the bit of code between the brackets after 'while'
    but you also need to look at the construction of a loop because you are missing the curly braces

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rogster001 View Post
    but you also need to look at the construction of a loop because you are missing the curly braces
    They are optional. If omitted, the first statement after the loop is what will be looped.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    iyes but he wants it to continue i imagine, i mean he has more than one statement to handle

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think it's necessary with a loop at all, since the loop seems to be supposed to check if it's ASCII or not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    thanks for the help everyone! I'm slowly getting there

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
    
        char letter;
    
        printf("Enter a Letter\n");
        printf("Press '#' at any time to quit\n");
    
        scanf("%c%*c", &letter);
    
        while(letter != '#')
    
            {
            printf("Ascii Value= %d", letter);
            break;
            }
    
        printf("\n\n");
        printf("Enter a Letter\n");
        printf("Press '#' at any time to quit\n");
    
        scanf("%c%*c", &letter);
    
        return(0);
    }
    At the moment this works well but I need to...

    1) If the user inputs an upper case letter, the program needs to convert it to lower case and vise versa. I know the code is letter = tolower(letter); but how do I do it at the same time for upper case?

    2) I also can't get the program to loop for some reason

    Thanks
    Last edited by DJ_Steve; 08-27-2009 at 11:02 PM.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by DJ_Steve View Post
    \At the moment this works well but I need to...

    1) If the user inputs an upper case letter, the program needs to convert it to lower case and vise versa. I know the code is letter = tolower(letter); but how do I do it at the same time for upper case?

    2) I also can't get the program to loop for some reason

    Thanks
    1. Since you don't want to do both at the same time, asking for how to do so won't help. You want to do one or the other, but never both.

    2. You should pay more attention to what you want to have happen inside your loop, and what words like "break" mean.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) There is a function called toupper, too.
    2) Think about the logic of your program, how it works now and how you want it to work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM