help on program

This is a discussion on help on program within the C Programming forums, part of the General Programming Boards category; Ok, I got it this \" stands for " . so it will be wrwitten like Code: printf("\"Press any key!\""); ...

  1. #16
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Ok, I got it this \" stands for " . so it will be wrwitten like
    Code:
     printf("\"Press any key!\"");
    now comes the part that is very confusing. My assignment says to Use getch() to input the

    key pressed. Use puts to Output “You pressed” and putch to output the actual character.
    Code:
    #include <conio.h>
    #include <stdio.h>
    
     main(void){
                
                 char c;
                
                         printf("\"Press any key!\""); 
               c=getch();
    puts("you pressed");
    putch(c);
          
              
              }
    If I compile my code and run the program I get : "press a key!"

    then I press a key and it exits out of the program. I am missing something here but what?

    I have declared the variable c, I am using getch(). If I am not mistaking getch() takes a

    character input and assigns it to c in my program. Is that correct or maybe you could give

    an explanation in this?

  2. #17
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    ok my new code is this
    Code:
    #include <conio.h>
    #include <stdio.h>
    
     main(void){
                
                 char c;
               
               
                
                         printf("\"Press any key!\"\n"); 
     getch();
          
    puts("\"you pressed\"");
    
    putch(c);
    
          
             getche();
              }
    I am starting to get the hang of this.

    The only problem I have now after running my code is that it does not display a character on the screen?

  3. #18
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Code:
    #include <conio.h>
    #include <stdio.h>
    
    int main(void)
    {
      char c;
      printf("\"Press any key!\"\n");
      c = getch();
      puts("you pressed: ");
      putch(c);
      return 0;
    }
    Works fine. Just run it from the command line.
    I like to play pocket pool.

  4. #19
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Hey its finally working. Wow, this is cool. Man I sure do appreciate everyone's help on this.
    all of you really help me out alot thanks.
    I noticed my code will not hold on the screen with return 0; but it will work with getch();.

  5. #20
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    hehe


    HINT: You have a char c. Use it.


    char c;
    getch("");
    putch("");
    hahahahahahahhahaha, suttle, but still nasty as ever.

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What the hell is wrong with you? Why do you keep trying to pass arguments to getch? getch doesn't take arguments, so stop doing it. Furthermore, unless you're trying to be funny, instead of just stupid, your call to putch is wrong also. putch does NOT take a string as an argument. It takes a single character. (Integer if you want to be pedantic, which we usually do.)

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Hey, I am not trying to be funny I am simply just a newbie at this with about 2 months of experience at this and no more.

  8. #23
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    quzah started yesterday morning, but I believe he was talking to djwicks.

  9. #24
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    So is Qusah saying that this: getch(""); is wrong?
    My understanding is that noyhing goes between the parenthesis.

  10. #25
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Correct, nothing goes between the parentheses when calling getch().

  11. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by robasc
    So is Qusah saying that this: getch(""); is wrong?
    My understanding is that noyhing goes between the parenthesis.
    Correct. This is something between parenthesis:
    Code:
    foo( "" );
    This is nothing between parenthesis:
    Code:
    foo(  );
    See the difference?

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #27
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    regretfully, i think a lot of people missed my joke on the whole "HINT: u have a char c, so use it". apologees to anyone who thought i was serious about that. i just thought it was funny the way whoever said it that way.

    maybe i can make it beta this way... GOOD LUCK robasc with this whole assignment, once u finish it, u will understand it all a lot more. sorry for confusing u.

  13. #28
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Seeing as getch() is implementation defined it would depend on the compiler you're using whether it takes an argument or doesn't.

  14. #29
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    then I press a key and it exits out of the program. I am missing something here but what?
    For future programs, I believe I know what was happening.

    Your code was working perfectly. What happened was, and this will happen with all of your programs written in this format, the window is set to automatically close after the program is done running. It just ran so fast, like computers do, that it looked like the character wasn't getting outputted.

    There are a couple of ways to prevent this. You could add in another c = getch(); line, which would force the program to wait for a key input, thereby keeping it open until you input a character.

    Or, like I prefer to do, you could put the contents of your main function into a loop. Like so.
    Code:
    #include <conio.h>
    #include <stdio.h>
    
     main(void)
     {
      char c;
      int loop = 0;
      while(loop == 0)
       {
        printf("\"Press any key!\"\n"); 
        getch();
        puts("\"you pressed\"");
        putch(c);
       }
     }
    What this does is it forces the program to repeat itself indefinately. By this I mean that as long as loop is equal to 0 (which it ALWAYS is) the program will start over again once it is finished. So, once it outputs c it will go back to the beginning of the loop, print "Press any key!", and wait for a character to be inputted. Then it will print that character and so on and so forth.

    The only problem with this is if you have a program that doesn't wait for an input, (scanf, getch, gets, cin, etc.) It will not have any reason to stop the program and wait, and instead it will loop at an incredible rate, never stopping and, even with a slow computer, could go through the program hundreds of times per second. Be wary when using an infinite loop like this.

  15. #30
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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