#K&N King Chapter 7, programming, exercise 2

38 messages · Page 1 of 1 (latest)

frozen badger
#

This is not really that I can't do the exercise, I am very close to doing it, but something is wrong.
Code in the thread

pseudo turretBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

frozen badger
#

for some reason when I hit enter

#

it does not continue the loop

stable nymph
#

!f

pseudo turretBOT
#
// Modify the square2 . c program of Section 6.3 so that it pauses after every
// 24 squares and displays the following message: Press Enter to continue...
// After displaying the message, the program should use getchar to read a
// character, getchar won’t allow the program to continue until the user presses
// the Enter key.

/* Prints a table of squares using a for statement */
#include <stdio.h>

void clear_i_buffer(void);

int main(void) {
  int i = 0, entries = 0;
  char action = '!';

  printf("This program prints a table of squares. \n");
  printf("Enter number of entries in table: ");
  scanf("%d", &entries);
  clear_i_buffer();
  for (i = 1; i <= entries; i++) {
    if (i >= 24) {
      printf("You shall not pass \n");
      while (((action = getchar()) == '\n')) {
        continue;
      }
    } else {
      printf("%10d%10d\n", i, i * i);
    }
  }

  return 0;
}

void clear_i_buffer(void) {
  while ((getchar()) != '\n')
    ;
}
✝The Christian Orangigang✝
stable nymph
#

Is this copied from the book?
Controversially, I don't have it.

frozen badger
#

so this is from the book, I only did a slight update to square2.c to bbetter understand it, I changed the n to entries, let me send it

#

square.2:

/* Prints a table of squares using a for statement */
#include <stdio.h>

int main(void)
{
    int i = 0, n = 0;
    printf("This program prints a table of squares. \n") ;
    printf("Enter number of entries in table: ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        printf("%10d%10d\n", i, i * i) ;
    }
    return 0;
}
pseudo turretBOT
#

@frozen badger

It looks like you may have code formatting errors in your message

Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks

Markup

```c
int main() {}
```

Result
int main() {}
frozen badger
#

this is the original square2.c from the book

#

wait

#

I wanted to add debugging

#

uh

#
...
 printf("You shall not pass \n");
            while( ((action = getchar()) == '\n') )
            {
                printf("Ok \n");
                continue;
            }
...

Now the output is:

You shall not pass

Ok

Ok

Ok

Ok

Ok

Ok

Ok

Ok

#

why can't it

#

OHHH

#

wait no

#

oh

#

i got it

stable nymph
#

Do debugging using a real debugger, then.
Adding code for the purpose of debugging is cursed really.
I'd forgive the odd printf(), but really... a debugger is the best tool for what you need.

frozen badger
#

well I got it

#

just in case anybody in the thread needs to look

#
// Modify the square2 . c program of Section 6.3 so that it pauses after every 24 squares and
// displays the following message:
// Press Enter to continue...
// After displaying the message, the program should use getchar to read a character,
// getchar won’t allow the program to continue until the user presses the Enter key.

/* Prints a table of squares using a for statement */
#include <stdio.h>

void clear_i_buffer(void);

int main(void)
{
    int i = 0, entries = 0;
    int max_entries = 24;
    char action = '!';

    printf("This program prints a table of squares. \n") ;
    printf("Enter number of entries in table: ");
    scanf("%d", &entries);
    clear_i_buffer();
    for (i = 1; i <= entries; i++)
    {
        if(i >= max_entries)
        {
            printf("You shall not pass \n");
            while( ((action = getchar()) == '\n') )
            {
                printf("Ok \n");
                max_entries += 24;
                break;
            }
        }else{
            printf("%10d%10d\n", i, i * i) ;
        }
    }

    return 0;
}


void clear_i_buffer(void)
{
        while ((getchar()) != '\n');
}
pseudo turretBOT
# pseudo turret

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

woeful radish
#

@frozen badger how is the book. Is it good ?

woeful radish
#

for a person who is not a beginner nor a intermediate either ?

frozen badger
#

like

#

i used to watch a C programming video before, from which I couldn't even make a tic tac toe but I did learn something

#

then I found this book, but I skipped so many chapters for youtube video's again

#

now I am just only using this book to learn

#

yea

#

it's good

woeful radish
#

oki, it's just that I just finished another beginner C book. And on a dilema whether I should read K&N one too. Because I am shit at memory. btw I just made tic-tac-toe 3 days ago. : P

frozen badger
#

nice

#

i used everything i learned in my tic tac toe, even pointer

#

s