Thread: Not very proud of this Code.

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Not very proud of this Code.

    I simply need to "write a program that reads input until encountering #. have the program print each input character and its ASCII decimal code. Print eight character code pairs per line"

    I did the following code using the conio.h.
    Code:
    /* review #1.c */
    
    
    #include <stdio.h>
    #include <conio.h> /* for getche() */
    #include <stdlib.h>
    
    int main(void){
    
        char ch;
        int chrcnt = 0;
    
        printf("Type any text and I will display your ASCII value\n\n");
        while ( (ch = getche()) != '#'){  /* returns single charater automatically. meaning is not buffered
        not ainsi and not posix  - bad*/
              chrcnt++;
              printf(" = %d ", ch);
              if ( (chrcnt % 8 == 0)){
                 printf("\n\n");
                 }
              }
    
    
       printf("\n\nYou have %d characters.\n", chrcnt);
    
       getche();
       return (0);
    
    }
    However conio is not ANSI nor is it POSIX. And I would like to make it ANSI at least. I was wondering how you could rewrite the code without the conio.h.

    A max of 8 characters per line must be outputed and there can be no use of pointers nor arrays. This is where I'm at in my studies. However I can not think of a way to use getchar() without incurring a newline. As far as I know getchar() is buffered right? meaning you need to press enter before it is displayed. However Pressing Enter will start a newline the opposite of my intent. How do I solve this please. What am I not looking at?Thank you, I don't know why it seems i've drawn a blank.

    P.S. This is not homework. I'm self-taught and reading "The waite group's new C primer plus"

  2. #2
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Never mind...

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Read this

    Basically there isn't a standards way to do this.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by nomi
    Never mind...
    getch() gets a character without echoing it. getche() with echo.

    Just use fgets() to read the input into a buffer then look at each character in the buffer.

    Or use getchar() which will read a character at a time, but only after an ENTER was typed on the keyboard. Unfortunately, this will leave all characters from the '#' to the '\n' in the buffer and should be cleared after your loop ends.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    I thought getche (); would give me errors....

    I didnt know it was part of conio and i change my earlier message.

    What do you mean by echo?
    Coz getch doesnt work on my compiler..

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    getche() is usally defined as follows:
    Code:
    {
      char ch;
      ch = getch();
      putchar(ch);
      return ch;
    }
    getch() captures the character but doesn't display it. And I might be wrong on ch being char, it might be int. Its been a little bit since I looked at it.

  7. #7
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    So i wont be able to use the value i get from getch but i can use the value i get from getche...or is it just my compiler...

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    if your compiler doesn't have getch() it won't have getche().

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Originally posted by Thantos
    Read this

    Basicall2)y there isn't a standards way to do this.
    1)I see. Thanks for the links. I'm assuming there must be a way as my book is giving me this as an example. There must be something collectively we are not thinking of?

    2)>>Just use fgets() to read the input into a buffer then look at each character in the buffer.

    Can't use that now, as I have not covered it yet. Also using getchar. It would echo my input and in order to output the ASCII value i would need to press enter. This would not conform to the task at hand. Meaning output eight characters and their ascii value on one line.

    3)>>Read this.
    I read it and even compiled the code using getch(), however when I inputed my password, i failed to recieve a '*' in place of the char. what gives?

    4)>>"I thought getche (); would give me errors....I didnt know it was part of conio and i change my earlier message.
    What do you mean by echo?
    Coz getch doesnt work on my compiler.."

    you might want to take a look


    here for some ANISI and POSIX functions nomi, It gives some explanations along with a table of contents.


    In regards to my question. Is there nothing anyone can think of right now?

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    This would not conform to the task at hand. Meaning output eight characters and their ascii value on one line.
    What about \b?

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by caroundw5h
    1)I see. Thanks for the links. I'm assuming there must be a way as my book is giving me this as an example. There must be something collectively we are not thinking of?
    Bad assumption. You can not if your compiler does not have getch(). What compiler does your book use? What compiler are you using?

    2)>>Just use fgets() to read the input into a buffer then look at each character in the buffer.

    Can't use that now, as I have not covered it yet. Also using getchar. It would echo my input and in order to output the ASCII value i would need to press enter. This would not conform to the task at hand. Meaning output eight characters and their ascii value on one line.
    What your book is asking is beyond Standard C. Your book is not teaching Standard C but an enhanced C.

    In regards to my question. Is there nothing anyone can think of right now?
    Yes. Change to the compiler the book is using if it works on your system. If not, SOL!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    getch() and getche() are compiler specific. If this is for a class then most likely the compiler they are using has those functions.

  13. #13
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    There are no perscribed compilers that the book recommends. It's actually conforming to ANSI C

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It's actually conforming to ANSI C
    If it uses getch() then it isn't conforming 100% to ANSI C. Every book I've read has stated that they will be making an assumption on the compiler.

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    OK, I for one give up. WHAT is the answer the book has to this problem?

    If you are correct and
    1) it does not specify a compiler
    2) it is teaching getch()

    then throw the book out and get one that knows what it's talking about.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM