help on program

This is a discussion on help on program within the C Programming forums, part of the General Programming Boards category; I am supposed to do this assignment stated: Write a C Program to have a char variable. Use printf to ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    help on program

    I am supposed to do this assignment stated:

    Write a C Program to have a char variable. Use printf to print “Press any Key!”. Make sure to print the double quotes as above. Use getch() to input the key pressed. Use puts to Output “You pressed” and putch to output the actual character.

    this is what I have done so far
    Code:
    #include <conio.h>
    #include <stdio.h>
    
    
    main(void){
               
               char c;
              
              
               printf("press any key!\n");
            getch();
              puts("you pressed\n"); 
               
               
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    You're missing some important parts of the assignment. You didn't really ask any questions, but I've bolded what you missed:

    Write a C Program to have a char variable. Use printf to print “Press any Key!”. Make sure to print the double quotes as above. Use getch() to input the key pressed. Use puts to Output “You pressed” and putch to output the actual character.

    getch() returns the character typed, but you're not capturing it. You're just letting it float away into space, never to be seen again. HINT: You have a char c. Use it.

    Pass the character returned by getch() to a putch() call. HINT: The character should be in your variable c if you follow the instructions in the previous paragraph.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    getch() returns the character typed, but you're not capturing it. You're just letting it float away into space, never to be seen again.

    So I should be putting in double quotes in ?
    HINT: You have a char c. Use it.
    I do not quite understand what you mean here?

    is this any better?
    Code:
    #include <stdio.h>
    main(){
       
               char c;
       
               printf("press a key!\n");
               getch("");
               puts("you pressed"); 
               putch("");
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    haee no
    Code:
    getch() returns the character typed, but you're not capturing it. You're just letting it float away into space, never to be seen again.
    getch() return a whatever key u have pressed on the keyboard. so you will have to capture the return calue of the getch so that u can print the char later on in the program.if you dont capture i will wont get the value return by getch to be printed in future program.

    so getch returns of type int so the code will look like this
    Code:
    main()
    {
         int ch;
         .
         .
         ch=getch()
         putch(ch);
    }
    and in the question it is also asked to print the message within the double qoutes
    so for that u will have to do this
    Code:
    printf("\"Press any key\");
    hope this helps you out, if u get any prob let me know

    s.s.harish

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    He'll definitely run into problems with that printf() syntax. I was actually hoping he'd figure it out on his own, since everything he needed to know to complete this assignment was probably covered in class or was assigned reading.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Why don't teachers ever push C standards? <conio.h> pshshs.

    Also, itsme gave him a perfect amount of advice. The other posts are just going to confuse him. robasc seemed to have completely misunderstood the original post.

  7. #7
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    Quote Originally Posted by ssharish
    and in the question it is also asked to print the message within the double qoutes
    so for that u will have to do this
    Code:
    printf("\"Press any key\");
    You forgot something:
    Code:
    printf("\"Press any key!\"\n");
    Like \n, \"(that prints a ") is an escape sequence

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    Puts and putch

    I do not understand what puts or ptuch is is

    how do you use these words?
    what do you use them for?

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Quote Originally Posted by robasc
    I do not understand what puts or ptuch is is

    how do you use these words?
    what do you use them for?
    puts - http://cplusplus.com/ref/cstdio/puts.html

    ptuch - Havn't heard of it.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Well, I am completely lost ! I have been working on this for a week now and I still cannot figure it out. What in the world am i doing wrong.
    Code:
    #include <conio.h>
    #include <stdio.h>
    
     main(void){
                
                 char c;
              
               printf("Press any key!\n");
               c=getch();
    puts("you pressed");
    putch;
          
              
              }

  11. #11
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    First off, I'd like to point out, even though this is kinda off topic, you might want to post this in C++ Programming next time.

    As for the code, you're very close. Again, you're not printing the quotes in the printf statement. Since " is a special character in the syntax of printf and puts (namely it designates the string to be outputted) you can't simply put another quote down. Here's an example of how your code is being read.
    Code:
    printf("press any key!\n");
    The red text is what is actually being outputted. \n is a special character that is really the same as you pressing the enter key. The dark red quotes are not printed. They simply tell the program that everything between them is a character string. Because the double quote serves this purpose, you can't just put exrta double quotes in there. If you do that, the program will group two separate strings and then you will have problems. Now, since people such as yourself want to output a double quote character, you simply put in \" wherever you want one to be outputted. It is read just like the \n character, except it outputs a double quote instead of going to the next line.

    The puts (which stands for put string) is perfectly fine, as I'm sure you know.

    However, you don't quite seem to understand how putch (put character) works. Remember, that any function, no matter what, has parenthases, whether it takes arguments or not. So, your first mistake was that you put
    Code:
    putch;
    instead of
    Code:
    putch();
    Secondly, it outputs a character, but you need to tell it what character. If you want it to output the vlue of a character variable, simply put the name of the variable in the parenthases. If you want it to always output the same character, simply put that character inside the parenthases and surround it with a single quote('). Like so.
    Code:
    char variable;
    putch(variable);
    Code:
    putch('a');
    You think you get it now? I hope I didn't ramble too much.
    Last edited by Jaken Veina; 03-26-2005 at 03:58 PM.

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Ok, I understand everything but the double quote thing you say: Again, you're not printing

    the quotes in the printf statement. I am still confused here. Are you saying that after my

    string I need to add \".
    Code:
    #include <conio.h>
    #include <stdio.h>
    
     main(void){
                
                 char c;
                         printf("press a key!");
               c=getch();
    puts("you pressed");
    putch(c);
          
              
              }

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    I am assumming I need to write like this

    Code:
    printf(\" press a key!\");
    or should it be
    Code:
    printf("press a key!"\");

  14. #14
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    Your assignment is to print "Press any key!" INCLUDING the quotes.
    Code:
    printf("Press any key!\n"); /* outputs: Press any key! */
    /* however */
    printf("\"Press any key!\"\n"); /* outputs: "Press any key!" */
    Why this happens is explained in Jaken Veina's post above [edit]and now below[/edit].
    Last edited by l0cke; 03-26-2005 at 05:46 PM.

  15. #15
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Okay, let me explain exactly what the quotes do. A single quote MUST BE BEFORE AND AFTER a character. (not yelling at you, just conveying that it's important) To the computer, 'a' means the character a. The same goes for double quotes, except they tell the computer it is a string of characters. Therefore the character ' " ' DOES NOT EXIST.

    Let's say you wanted to output a double quote and a double quote only. The way you're thinking, you would do this.
    Code:
    putch('"');
    //if you can't tell, that's a double quote surrounded by single quotes
    But, as I said, that character doesn't exist.

    Now, look for a second at the character '\n'. Notice I have single quotes around that. But that's a string with the characters '\' and 'n', right? Nope. '\n' is a SINGLE CHARACTER. When it's read by printf, putch, or puts, it tells the computer not to display \n at the terminal, but to go to the next line.

    The same thing goes for a double quote. Since " has a special function in designating a string, you can't simply put ""Hello"" to output the string Hello in quotes. Here's how the computer would read it.
    Code:
    printf(""Hello"");
    It reads that there are two separate character strings (with nothing in them) and in between them is gibberish. Undoubtedly, you will get a compiler error if you do that.

    So, like with '\n' there is a special character for outputing an actual double quote. That character is \". When the computer reads the \" character, instead of saying, "Oh, a character string is beginning", it says, "Oh, the user wants me to output a double quote."

    Think you can figure it out now. If not, I can show you how to do it, but I'm really not prone to simply telling people the answer.

    EDIT: Ahh, I see someone has already explained it. Oh, well. If you still don't get it, I have another idea for explaining it.
    Last edited by Jaken Veina; 03-26-2005 at 05:41 PM.

Page 1 of 3 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