Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Why the shit doesn't this work?

Name: Anonymous 2020-12-24 3:41

The following code is supposed to take a .txt file like the one below:
https://pastebin.com/HfjkVZpy
It will then go by each line and see if each password is valid by the amount of times the letter appears. For example, "1-9 a: aaaer" is valid, but "1-2 q: aqeeqgqf" is not".
#include <stdio.h>
#include <string.h>

int main( int argc, char *argv[])
{
FILE *file;
int i = 0;
int file_line_count;
int valid_passwords = 0;
file = fopen(argv[1], "r");
typedef struct node {
char ranges[4];
char filter_letter[3];
char password[51];
} Password_Verification;

if (file) {
for (char c = getc(file); c != EOF; c = getc(file)) {
if (c == '\n') file_line_count = file_line_count + 1;
}
Password_Verification Pswds[file_line_count + 1];
while (3 == fscanf(file, " %4c %3c %51c", &Pswds[i].ranges,
&Pswds[i].filter_letter,
&Pswds[i].password))
{

int k = 0;
for (int j = 0; j < strlen(Pswds[i].password); j++) {
if (Pswds[i].password[j] == Pswds[i].filter_letter[0]) {
k = k + 1;
if ( (k >= (int)(Pswds[i].ranges[0] - 48)) && (k <= (int)(Pswds[i].ranges[2] - 48)) ) {
valid_passwords = valid_passwords + 1;
}

}
continue;
}
}
printf("%d\n", valid_passwords);
return 0;
}
}


But for some reason, when I run this code with the input above, the valid_passwords factor never increments. Why is this the case?

Name: Anonymous 2020-12-24 7:09

People writing smart code are not smart enough to debug it.
write simpler code. Separate statement for each action.
Add debug output to each line. Observe what is actually processed.

Name: Anonymous 2020-12-24 15:02

The immediate cause is continuing the while when j is 0. Also, your parsing is bollocks since there are two-digit range endpoints, and you don't increment i.

Name: Anonymous 2020-12-24 16:35

Please rewrite the code in JavaScript.

Name: Anonymous 2020-12-24 20:21

poast indented coad

Name: Anonymous 2020-12-24 20:53

>>1
You consume the entire file with getc to count lines, then you run the while with fscanf at the end of the file. To go over the same content again you need to rewind.

Name: Anonymous 2020-12-25 13:39

Use Python FFS, it would be a few lines with a regular expression or C++14/17 if you need something compiled, but definitely not a dead language like this.

Name: Anonymous 2020-12-29 5:57

Code tags look like [ c o d e ] ... [ / c o d e ]

// https://dis.tinychan.net/read/prog/1608781261
// probably based on https://adventofcode.com/2020/day/2 [part 2 not shown]

#include <stdio.h>
// #include <string.h> // use format strings don't implement parser
#include <stdlib.h>

// fuck this shit...
// scanf() format string "%*s" is a check & skip,
// without return count. not a field width.
// use fixed field width (SZ-1 because of '\0')
#define SZ 128
#define GETLINE(lo, hi, ch, pass) scanf("%d-%d %c: %127s", &lo, &hi, &ch, pass)
#define FIELDS 4

#define SHOWBAD 1

#define DEBUG 0
#define LOG(fmt, ...) fprintf(stderr, "TRACE: " fmt, __VA_ARGS__)

int count(char ch, char *str) {
int sum = 0;
if(!str || !*str) return 0;
for(int i = 0; str[i]; i++) {
if(ch == str[i]) sum++;
}
return sum;
}

int main(int argc, char *argv[]) {
int ch, cnt, lo, hi;
char pass[SZ];

if(argc > 1) {
FILE *f = freopen(argv[1], "r", stdin);
if(DEBUG) LOG("FILE = %p, stdin = %p\n", f, stdin);
if(!f || f != stdin) { perror("can't open password file!"); exit(1); }
}

int line = 0, valid = 0, invalid = 0;
int x = GETLINE(lo, hi, ch, pass);
if(DEBUG) LOG("x = %d\n", x);

while(x == FIELDS) {
line++;
cnt = count(ch, pass);
if(DEBUG) LOG("[%d-%d] %c=%d: %s\n", lo, hi, ch, cnt, pass);

if(lo <= cnt && cnt <= hi) valid++;
else {
if(SHOWBAD) printf("BAD!!! %d-%d %c: '%s' BAD!!! line: %d\n", lo, hi, ch, pass, line);
invalid++;
}

x = GETLINE(lo, hi, ch, pass);
if(DEBUG) LOG("x = %d\n", x);
}

printf("[%d] valid passwords [%d bad]\n", valid, invalid);
return 0;
}

Name: Anonymous 2020-12-29 8:23

Rewrite it in Rust. RIIR

Name: Anonymous 2020-12-30 5:59

>>8
Wow friend, thanks for schooling me in good programming practice.

Name: Anonymous 2020-12-30 6:53

Check em, dubs.

Name: Anonymous 2020-12-31 4:55

time stamp dubs

Don't change these.
Name: Email:
Entire Thread Thread List