Thread: Active c programs.....a small prog. as starter

  1. #1
    Animate
    Guest

    Unhappy Active c programs.....a small prog. as starter

    How can I make this program work.
    I like to start to make a bit active programs, and starts in the small. I really like to hear from some of you, who knows something about this.

    Tx

    /* This program should be able to count up when the key u is pushed, and the same number should count down, if the key d is pushed..
    */

    #include <stdio.h>
    #include <conio.h>
    #define U 100
    #define D 117

    int main()
    {

    int count = 0;
    int ch;
    printf("Her we count up or down..\n");
    printf("Use the key 'u' to count up\n");
    printf("Use the key 'd' to count down\n");
    ch = getch();
    switch(ch)
    {
    case U:
    while(kbhit())
    {
    count++;
    printf("%d", count);
    if (ch == 'q')
    return 0;
    }
    case D:
    while(kbhit())
    {
    count--;
    if (ch == 'q')
    return 0;
    }
    default:
    printf("Wrong key");
    }

    getch();
    return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h> /* Nonstandard */
    
    /* The following program may not run on all systems
    */
    int main ( void )
    {
      int ch, count = 0;
      do {
        ch = getch();
        switch ( toupper ( ch ) ) {
        case 'U': printf ( "%d\n", ++count ); break;
        case 'D': printf ( "%d\n", --count ); break;
        }
      } while ( toupper ( ch ) != 'Q' );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Your main problem is the structure of the program. The while loops shouldn't be in the switch, in that way you can never count up AND down, just up OR down.

    Code:
    #include <stdio.h> 
    #include <conio.h> 
    
    int main() 
    { 
        int count = 0; 
        int ch; 
        
        printf("Here we count up or down..\n"); 
        printf("Use the key 'u' to count up\n"); 
        printf("Use the key 'd' to count down\n"); 
        
        while ((ch = getch ()) != 'q')
        {
            switch (ch)
            {
                case 'u':
                case 'U':
                    count++;
                    printf ("New value of count = %d\n", count);                
                    break;                
                case 'd':
                case 'D':
                    count--;
                    printf ("New value of count = %d\n", count);
                    break;
                default:
                    printf ("Wrong key\n");
                    break;
            }
        }
        
        return 0; 
    }

  4. #4
    Animate
    Guest
    Tx Perlude your the best...
    And tx to Shiro too, It's very nice to have help
    " just around the corner"

  5. #5
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    This will get rid of the double "Wrong Key"...

    Code:
    #include <stdio.h> 
    #include <conio.h> 
    
    int main() 
    { 
        int count = 0; 
        int ch; 
        
        printf("Here we count up or down..\n"); 
        printf("Use the key 'u' to count up\n"); 
        printf("Use the key 'd' to count down\n"); 
        
        while((ch = getch ()) != 'q')
        {
            while(ch == NULL) ch = getch();
            switch(ch)
            {
                case 'u':
                case 'U':
                    count++;
                    printf("New value of count = %d\n", count);                
                    break;                
                case 'd':
                case 'D':
                    count--;
                    printf("New value of count = %d\n", count);
                    break;
                default:
                    printf("Wrong key\n");
                    break;
            }
        }
        
        return 0; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-19-2006, 02:37 PM
  2. wierd Problem on Small If Prog
    By God in forum C++ Programming
    Replies: 7
    Last Post: 07-13-2004, 08:08 AM
  3. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 05:42 AM