Functions and Macros!!

Discussion in 'C' started by Roza, Aug 17, 2012.

  1. Roza

    Roza New Member

    Joined:
    Aug 17, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Need code of this Program:
    1. This program displays a running counter starting from 0 till infinity. The counter starts/resets by pressing
    enter and stops by pressing some other key. Use as many define directives and macros to achieve this
    task. The counter when played should be like a real counter (no separate line should be used for the
    next counter increment). Hint: use clrscr() command inside the for loop.
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    use clrscr() huh? Maybe a different instructor is in order. :D You didn't indicate what compiler you're using... I'm guessing borland's turbo C?? If not, you'd need to mill over the compiler's documentation to see if similar functions are implemented.

    The counter starts/resets by pressing enter and stops by pressing some other key

    implies using unbuffered input - conio.h and getch or getche

    The counter when played should be like a real counter (no separate line should be used for the next counter increment)

    clrscr() would probably do, but you could also try gotoxy along with cprintf also in conio.h If you're operating in a Windows environment, just be aware that using this stuff in a console window may have goofy results.

    I don't know about achieving infinity... overflow may occur first. :)
     
  3. Roza

    Roza New Member

    Joined:
    Aug 17, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I am using the same compiler..
    I am done with the counter loop but I need help in how to stop/reset the program.
    Code:
    #include<stdio.h>
    #include<conio.h>
     
     
    void beep(void);
    int main(void)
    {
         printf("Press enter to start..."); 
         while ('\n'!= getchar ());   
         beep();
          
    }
        void beep(void)
        {
         int n,j;
         
         for(n=1;;n++)
         {
                      printf("\n%d",n); 
                        
                      for(j=1;j<200000000;j++)
                      ;
                      system("cls");
                      
         }
         
    }
     
  4. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    I don't have a copy of TC installed so I can't really test with it, but you'd probably want an infinite outer loop that breaks if any key other than '\r' is pressed. getchar, scanf, fgets, etc are buffered, so try using getch instead.

    The inner loop would probably need to test against overflow (if user doesn't start / stop) and whether or not the inner is currently running or stopped. If it's running, increment the counter and display it. If it's stopped, reset the counter to 0 and wait on another '\r' to start it up again.

    Seems to be a state machine of some type.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    int main(void) {
    
        char is_running = 1;
        unsigned char count = 0, quit = 0;
    
        for(;;) {
    
            if(is_running == 0) { 
                // simulate toggle off / on
                is_running = 1;
                count = 0;
                ++quit;  
            } else if(quit == 5) { // simulate keypress other than '\r'
                break;
            } else {
                if(count < UCHAR_MAX) {
                    system("cls");
                    printf("%d", count);
                    ++count;
                } else
                    is_running = 0;
                }
            }
        }
    
        return 0;
    }
     
  5. Roza

    Roza New Member

    Joined:
    Aug 17, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    It has Syntax Error Before Return.Can you Correct it
     
  6. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    oops - fat fingered the keyboard again. :D

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    int main(void) {
    
        char is_running = 1;
        unsigned char count = 0, quit = 0;
    
        for(;;) {
    
            if(is_running == 0) { 
                // simulate toggle off / on
                is_running = 1;
                count = 0;
                ++quit;  
            } else if(quit == 5) { // simulate keypress other than '\r'
                break;
            } else {
                if(count < UCHAR_MAX) {
                    system("cls");
                    printf("%d", count);
                    ++count;
                } else
                    is_running = 0;
            
                } <--  remove this brace    
            }
        }
    
        return 0;
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice