Thread: Can some one check this code for me?!

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    Can some one check this code for me?!

    I have an assignment due and the lecturer gave me this code to add to the end of it. however I think Ive written it down wrong as its doesnt compile.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    void main (void)
    {
        char ch;
        
        printf("\n\n\nAnother Go? (y/n)");
        do
        {
             ch=getch();
             ch=toupper(ch);
         }
        while (ch!='Y' && ch!='N');
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    void main(void)
    write it as
    Code:
    int main(void)

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It would help if you explained how it doesn't compile, i.e. the exact error message(s) you get along with any line numbers. Copy-paste them from your terminal/IDE.

    Also, getch is not a standard function. Perhaps you should be using getchar(), which returns an int. That means ch should be declared as an int, to handle all possible return values (all chars, plus EOF).

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Gareth Morgan View Post
    however I think Ive written it down wrong as its doesnt compile.
    And why don't you tell us the errors you get?

    Code:
    $ make foo
    cc -ggdb3 -Wall -Wextra    foo.c   -o foo
    foo.c:5:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    foo.c: In function ‘main’:
    foo.c:12:10: warning: implicit declaration of function ‘getch’ [-Wimplicit-function-declaration]
    /tmp/cc3YI3U1.o: In function `main':
    /home/andreas/Code/foo.c:12: undefined reference to `getch'
    collect2: ld returned 1 exit status
    make: *** [foo] Error 1
    There is no standard function called getch(). You're probably on Windows/DOS and want to use a function with that name declared in <conio.h>, so you have to include this header.

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    16
    Do you mean to use getchar();?

    Code:
    ch = getchar();

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Thanks for your comments. Its working now. Used a different compiler and used your changes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check my code.
    By Mr.Lnx in forum C Programming
    Replies: 10
    Last Post: 10-19-2012, 03:59 PM
  2. Help! Check my code over.
    By omGeeK in forum C Programming
    Replies: 6
    Last Post: 11-04-2010, 11:34 PM
  3. Code Check.
    By unkownname in forum C++ Programming
    Replies: 2
    Last Post: 05-20-2006, 02:24 AM
  4. Check my code Please
    By AdioKIP in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2002, 08:52 PM
  5. Can anyone check this code
    By a_learner in forum C Programming
    Replies: 5
    Last Post: 10-06-2001, 02:41 PM