C problem...
Results 1 to 5 of 5

Thread: C problem...

  1. #1
    Junior Member
    Join Date
    Aug 2001
    Posts
    23

    Angry C problem...

    Alright. Im a beginning C programmer, and im having a few difficultys with a piece of code im tryin to get working.

    Its a small program to view whatever you have lying around in your memory. My problem is for some reason or another it refuses to call a getchar() function the first time round and try as I might I cant figure out why. So I thought I'd post it here and let you guys pick it apart and see if you can shed some light on whats gone wrong here. (Comeon, its only a small piece of code.. Help a guy out.. Please?)

    Ok, most of it is pretty straight forward. The variable memstart is a integer for now because it was being antisocial when it was a long integer. But thats not my problem for now. My problem is after the first 2 for loops are finished it should call the function getchar() to pause the screen until the user hits the enter button. It dosen't. It works fine at the end of the 2nd and so on loops, but just not for the first loop. The first page of information goes whizzing off the top of the page before anyone can get a chance to read it. Ive tried stepping through the code, and it appears to be calling it, but it just dosent call! I suspect my while(1) infinite loop is somehow causing the problem, but I have no idea how. Thats an ugly piece of code anyway. Anyway, ive stuck comments all through it to try and help explain whats going on. It looks rather ugly just pasted into the page like that, so I also attached the source code to this thread so you can download it till your hearts content. (I changed the extention to .txt, cos this server dosen't like you attaching .c files)


    #include <stdio.h>
    #include <ctype.h>
    #define COLUMN_NUMBER 55 /* Constant for number of columns printed to screen */
    #define ROW_NUMBER 20 /* Constant for number of rows printed before a page break */
    unsigned int rowcounter, columncounter, memincrement = 0, *ptr;
    unsigned int memstart; /* Value of first memory byte to start at */
    unsigned int charconvert; /* Takes the character to be printed to the screen from the pointer */
    void main(void)
    {
    printf("\nEnter starting memory value: ");
    scanf("%d", &memstart); /* Yeah, memory value to start at */
    printf("\n");
    while (1) /* An infinite loop. I suspect this could be causing my problems... */
    {
    for (rowcounter = 0; rowcounter < ROW_NUMBER; rowcounter++)
    {
    printf("%d ", ptr);
    for (columncounter = 0; columncounter < COLUMN_NUMBER; columncounter++)
    {
    ptr = memstart + memincrement; /* Point the pointer at the memory value we wish to examine */
    charconvert = (char) *ptr; /* Pass the value to the variable charconvert */
    if (isprint(charconvert)) /* Check that its a printable character, dont want to go around printing tabs or page breaks or stuff */
    printf ("%c", charconvert);
    else
    printf (" ");
    memincrement++; /* Increment the variable that controls where the pointer points to */
    }
    puts("");
    }
    getchar(); /* My problem. It calls fine after the 1st cycle, but refuses to call the first time. Why??? I suspect my infinite loop has some part in this problem */
    }
    }


    Can anyone shed some light on what is going on here? Any help would be greatly appreciated. Its a minor problem, but one that is annoying me no end. Thankyou.

  2. #2
    Senior Member
    Join Date
    Jan 2002
    Posts
    121
    the scanf you are using to input the decimal breaks when the enter key is hit however the enter key is left in the input buffer so the getchar() takes it out of the buffer, try adding the following right after the scanf:

    fflush(stdin);

    this may require you to includes stdlib.h though i can't remember off the top of my head
    what is love but contempt for hate?

  3. #3
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    Another issue with apart from the one that indolent pointed out is the way that you are iterating through memory. You really should be using pointer arithmetic, not trying to specify the location through an int.

    That is, use ptr++; instead of just incrementing a variable of type int. If you do a ++ increment or -- decrement on a pointer, the compiler automagically realises the size of the data being pointed to and jumps by the appropriate number of bytes. An int is usally 4 bytes, but not always! In C usually you can get away with using ints and pointers interchangably but this is not good programming practice.
    OpenBSD - The proactively secure operating system.

  4. #4
    Junior Member
    Join Date
    Aug 2001
    Posts
    23
    A'ight. Well I solved the problem, although not how I had intended to. I ended up using:

    quit = getch();
    if (quit == 113 || quit == 81)
    return 0;

    It works. Now hitting Q is the only way to quit, rather than Ctrl-C. I had a play with fflush(stdin); thanks for the idea indolent, but no success. As for the pointer increment smirc, yeah it is a good idea but it increments by the wrong value. Its using integers which chew up 2 bytes, so it increments by 2 rather than the 1 I want. Still, it is a more elegant way of incrementing the pointer, I might have a play with it and see if I can make it work.

    Thanks for the help guys, it was appreciated.
    Hmmm, Im still curious as to why the origional method didn't work though. Can anyone else give any reasons to that?

  5. #5
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    I think I understand what you're trying to do now but note that getch() is not an ANSI C function so it will only work with some compilers. You're probably better off using:

    Code:
    while (getchar() != 'q') {
        // do something here!
    }
    OpenBSD - The proactively secure operating system.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •