Function problem

This is a discussion on Function problem within the C Programming forums, part of the General Programming Boards category; I used scanf in the main function to scan a character, I put the scanf in a while loop, while ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Function problem

    I used scanf in the main function to scan a character, I put the scanf in a while loop, while keyboard input it being scanned I want the other functions to operate, but I can't figure out how to get it to run properly.

    Code:
    //TENNIS
    #include <stdio.h>
    
    #define PRINT 0
    #define GAME_A 1
    #define GAME_B 2
    #define DEUCE 3
    #define AD_SERVER 4
    #define AD_RECEIVER 5
    
    int scoreA = 0;
    int scoreB = 0;
    
    int scoreFunct(char c);
    void printFunct(int scoreA, int scoreB, char c);
    
    int main (int argc, char **argv){
    
      char c;
      char team;
      
      printf ("Team A or Team B serves first?: ");
      scanf ("%c", &team);
      printf ("Team %c to serve\n", team);
      
      while(scanf("%c", &c) == 1){
    
      }  
      
      return(0);
    }
    
    int scoreFunct(char c){
      
        while((scoreA < 3) && (scoreB < 3)){
        if(c == 'A'){
          scoreA+=1;
        }
        else if (c == 'B'){
          scoreB+=1;
        }
        else if (c == 'S'){
          return(PRINT);
        }
      }
      
      return(0);
    }
    
    void printFunct(int scoreA, int scoreB, char c){
      
      if(scoreFunct(c) == PRINT){
        printf("%d-%d", scoreA, scoreB);
      }
    }
    The ones in red are what I'm stuck on, i want that function to execute while the scanf is running in the main function. I tried getchar, that gives me worse problems.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Does this help?


    //unsigned char fgetc(FILE* stream);

    while (c = (char)fgetc(stdin) != NULL) // while there is input


    Not sure if this is what you want but:
    Code:
    while (c = fgetc(stdin) != NULL) {
       scoreFunc(c);
    }
    But you could also replace fgetc(stdin) with getchar(). Also I hope you'll do some error checking cause your program is awfully brittle.
    Last edited by keira; 08-15-2007 at 03:34 AM.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Interesting, but I have no idea what that means, so I'm not going to try to use that :P

    Thanks anyways.

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I just did:

    Code:
     while(scanf("%c", &c) == 1){
    
    scoreFunct(c);
      
    }
    to call the function, but then when I run it, and it gets to that bit, I can't enter anything, so I guess the while loop is doing something random.

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Hmm, I think I solved it, I put the scanf in the other function, and just used scoreFunct(c); to call the function in the main function, without any loops. That seemed to do the trick, but now other problems :S

    Programming...

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You check the return value of [f]getc() and getchar() against EOF for failure, not NULL. And parentheses are a good idea around code like that.
    Code:
    unsigned char fgetc(FILE* stream);
    It's int. You can't store EOF in a char, so don't even try.

    I used scanf in the main function to scan a character, I put the scanf in a while loop, while keyboard input it being scanned I want the other functions to operate, but I can't figure out how to get it to run properly.
    So you want your program to do other things while it's waiting for the user to press a key?

    There's no standard way to do that, but you'll find mention in the FAQ of kbhit() and getch() which might do the trick. I believe that both of those functions work with Dev-C++.
    Code:
    #include <conio.h>
    
    int c = 0;
    
    do {
        if(kbhit()) {
            c = getch();
            printf("Received a key press: &#37;c\n", c);
        }
    
        puts("Processing...");
    } while(c != 27);  /* 27, or 0x1B, is the code for ESCAPE */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by dwks View Post
    You check the return value of [f]getc() and getchar() against EOF for failure, not NULL. And parentheses are a good idea around code like that.
    Code:
    unsigned char fgetc(FILE* stream);
    It's int. You can't store EOF in a char, so don't even try.


    So you want your program to do other things while it's waiting for the user to press a key?

    There's no standard way to do that, but you'll find mention in the FAQ of kbhit() and getch() which might do the trick. I believe that both of those functions work with Dev-C++.
    Code:
    #include <conio.h>
    
    int c = 0;
    
    do {
        if(kbhit()) {
            c = getch();
            printf("Received a key press: %c\n", c);
        }
    
        puts("Processing...");
    } while(c != 27);  /* 27, or 0x1B, is the code for ESCAPE */
    Haha what is all that, never even seen some of that code before, I can't use code I've not learnt in this assignment, but eitherways, I just put the scanning in the second function, so I pretty much figured it out, thanks anyways

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Yes i'm sorry I completely bombed it. My suggestion was wrong altogether. I'm new to c, but another thing that was messing me up is you have an infinite loop in your scorefunct if the characters dont match A B or S.

    The correct way to get a character read from stdin is:
    Code:
    do {
      c = getchar();
    } while (c != 'x');
    in this case x would exit the loop. scanf() is a poor function to use so get used to fgetc or getchar() (which is the same as fgetc(stdin) for character input.

    I wish I knew the criteria for your program so I could help more, as it is your program does nothing. I had to do a lot of assuming just to get it to do anything...

    Also look into <ctype.h> for the functions tolower(char) and toupper(char); they help in comparing user input cause you only have to test one case instead of both upper and lower cases....
    Last edited by keira; 08-15-2007 at 04:28 AM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Just like [f]getc(), it's tolower(int) and toupper(int), although only CHAR_BITS (usually 8, from <limits.h>) of the int are used . . .

    Most standard C functions that deal with single characters use ints, not chars. This is probably to handle EOF.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 01:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM

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