Thread: My program doesn't work as it should?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    55

    My program doesn't work as it should?

    This program has all valid syntax, and no compilier errors or warnings. I am mostly posting this in order to get help on a function that doesn't seem to be doing its' job until much later. I'm also posting this for how to clear the screen more efficiently in C. My current program just checks the system platform then uses an inefficient system command for clearing the screen.

    Unexpected Output : The loop I made for getting user input and storing it in the heap works, but the if statement that tests if the user enters more than they should does not execute until after the user presses enter. Basically, if I typed 100 characters, the error message won't pop up and take care of it until after a enter key press. Any help with making it pop up exactly when you exceed the limit would be appreciated.


    My inefficient screen clearing code :


    Code:
    void ClearScreen(void) {
    
    
        #if SYSTEM==Windows
        system("cls");
        #elif SYSTEM==linux
        system("clear");
        #endif
    
    
        return;
    }
    My getting user input code :


    Code:
        do {
    
    
            if ( CharCtr >= MemCtr ) {
            printf("\n \a Oops! Not enough allocated memory space");
            printf(" Printing received data... \n \n");
    
    
            countdown(3);
    
    
            StartOfiPtr += CharCtr;
            *StartOfiPtr = '\0';
            StartOfiPtr -= CharCtr;
    
    
            break;
    
    
            }
    
    
            scanf("%c", iPtr);
    
    
            ++iPtr;
            ++CharCtr;
    
    
    
    
        } while ( *(iPtr - 1) != '\n' );
    Full program :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <time.h>
    
    
    #ifdef _WIN32
    #define SYSTEM Windows
    #ifndef SYSTEM
    #ifdef linux
    #define SYSTEM linux
    #endif
    #endif
    #endif
    
    
    void countdown(int seconds);
    void countdowntoexit(int seconds);
    void wait(int seconds);
    void ClearScreen(void);
    
    
    int main() {
    
    
        char * iPtr;
        char * StartOfiPtr;
        int CharCtr = 0;
        int MemCtr = 0;
    
    
        iPtr = (char *)malloc(MemCtr = 10 * sizeof(char));
        StartOfiPtr=iPtr;
    
    
        if ( !iPtr ) {
            printf("Memory allocation has failed!");
            exit(1);
        } else {
            printf("Memory allocation has succeeded with %d bytes allocated so far! \n \n", MemCtr);
        }
    
    
        countdown(3);
    
    
        do {
    
    
            if ( CharCtr >= MemCtr ) {
            printf("\n \a Oops! Not enough allocated memory space");
            printf(" Printing received data... \n \n");
    
    
            countdown(3);
    
    
            StartOfiPtr += CharCtr;
            *StartOfiPtr = '\0';
            StartOfiPtr -= CharCtr;
    
    
            break;
    
    
            }
    
    
            scanf("%c", iPtr);
    
    
            ++iPtr;
            ++CharCtr;
    
    
    
    
        } while ( *(iPtr - 1) != '\n' );
    
    
        *iPtr='\0';
    
    
        printf("\n You entered : %s \n \n ", StartOfiPtr);
    
    
        free(iPtr);
    
    
        countdowntoexit(5);
    
    
        return 0;
    }
    
    
    void countdown( int seconds ) {
    
    
        int a = 0;
    
    
        printf("\n \t \t Continuing in... ");
    
    
        for (a=0; a < seconds; a++ ) {
            printf(" %d", a+1);
            wait(1);
        }
    
    
        ClearScreen();
    
    
        return;
    }
    
    
    void ClearScreen(void) {
    
    
        #if SYSTEM==Windows
        system("cls");
        #elif SYSTEM==linux
        system("clear");
        #endif
    
    
        return;
    }
    
    
    void wait( int seconds ) {
    
    
        clock_t end_wait = { clock() + ( seconds * CLOCKS_PER_SEC ) };
    
    
        while ( clock() < end_wait ) {}
    
    
        return;
    
    
    }
    
    
    void countdowntoexit ( int seconds ) {
    
    
        int a = 0;
    
    
        printf("\n \t \t Exiting in... ");
    
    
        for (a=0; a < seconds; a++ ) {
            printf(" %d", a+1);
            wait(1);
        }
    
    
        return;
    }


    Any other suggestions you can think of would be nice if you have good ideas for improving this code.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    That's just the way the standard input routines work. It is part of buffering terminal, or console, input for your program.

    Getting input as it is typed is not part of the standard library. If you want to do that, say that you may say "Stop. You are typing too much." or whatever, you'll have to look beyond standard C.

    I'd advise looking at some "curses" implementation as most can do that.

    Soma

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    free(iPtr);
    "iPtr" doesn't point anymore to the beginning of the allocated block, thus you can't free it. You need to free "StartOfiPtr".

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by BatchProgrammer View Post
    Code:
    void wait( int seconds ) {
         clock_t end_wait = { clock() + ( seconds * CLOCKS_PER_SEC ) };
         while ( clock() < end_wait ) {} 
    }
    Notice that this function as written will maximize CPU usage and/or drain the battery during the interval. Try it by calling wait(60) to confirm this. Normally one uses sleep() to accomplish what you're doing with this function, which asks the OS to wait without using any CPU.

    Quote Originally Posted by BatchProgrammer View Post
    Any other suggestions you can think of would be nice if you have good ideas for improving this code.
    I second the suggestion to use curses. It has routines for clearing the screen and implementing a character limit, and a timeout as well. I posted an example on my blogpost using pdcurses: Private Misc: pdcurses - timeout example

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why this program doesn't work!!
    By AlSadiq in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2011, 09:00 AM
  2. Why doesn't this c program work?
    By hobolux in forum C Programming
    Replies: 16
    Last Post: 01-17-2011, 02:05 PM
  3. My program doesn't work on others comps
    By Livijn in forum C# Programming
    Replies: 8
    Last Post: 10-29-2008, 06:03 AM
  4. My first program and it doesn't work.
    By Magneto in forum C++ Programming
    Replies: 16
    Last Post: 07-16-2005, 02:13 PM
  5. Program doesn't work as it should.
    By Cris987 in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 01:06 PM

Tags for this Thread