#Help with buffers

163 messages · Page 1 of 1 (latest)

knotty quiver
#

hi, could you post a shorter snippet? and as code form not screenshots (so we can copy+paste it if need be)

vernal badger
#

is there anyway i can copy from vim

knotty quiver
#

what computer are you on

vernal badger
#

mac

knotty quiver
#

open <filename> -a textedit

#

then just copy the relevant parts

#

or just the entire thing and put it in a pastebin and i'll take a looksee

#

erm, it might be -a TextEdit, i cant remember if it's case sensitive

#

(alternatively, you can cat <filename> | pbcopy. fun fact, on mac, pbcopy is a command where it'll copy its stdin to your clipboard. pbpaste will write your entire clipboard to stdout)

vernal badger
#

theres the paste bin

knotty quiver
#

got example input?

vernal badger
#

included in the image but i can get that too

knotty quiver
#

ah i see you have hw6.h

#

let me see if i can jsut spot the error

vernal badger
#

do u need that too

knotty quiver
#

gimmie a sec to read stuff

vernal badger
#

okay thank you

knotty quiver
#

and which part specifically is the weird part

vernal badger
#

some courses store extra pre requisites

#

usually from the course before

knotty quiver
#

so in this example

#

oh ok cours ebefore

#

that means you havent reset a variable to 0

#

first off, have you tried breaking this out into separate functions?

vernal badger
#

no i haven't

knotty quiver
#

it'll make it significantly easier and will probably solve the bug

#

erm, can you show me the definition of enum course_type_t

vernal badger
#

enum course_type_t {
LECTURE,
PSO,
LABORATORY,
RECITATION,
INDEPENDENT,
};

knotty quiver
#

i see

#

and what's the variable lecture equal to?

#

oh

vernal badger
#

no clue

#

so i just type casted

knotty quiver
#

?? lol

#
  int lecture = (int) LECTURE;
  int pso = (int) PSO;
  int lab = (int) LABORATORY;
  int rec = (int) RECITATION;
  int ind = (int) INDEPENDENT;
#

ah i see

#

well, so here's a fun fact abotu C for you

vernal badger
#

seems to be 0 1 2 3 4

knotty quiver
#

so when you say ```c
enum foo {
THING1,
THING2,
...
};

#

it'll essentially define a global constant called THING1, whose type is an enum foo (which is 100% interchangeable with int)

#

so all those int lecture = ... declarations are irrelevant

#

in fact, this entire block ```c
if(reader == 2) {
if(enum_int == lecture) {
type = LECTURE;
}
else if(enum_int == pso) {
type = PSO;
}
else if(enum_int == lab){
type = LABORATORY;
}
else if(enum_int == rec) {
type = RECITATION;
}
else if(enum_int == ind) {
type = INDEPENDENT;
}
}

#

just do ```c
reader = fscanf(fp, "|%d|%d\n", &type, &credit_hours);
printf("int %d, hours %d\n", type, credit_hours);

vernal badger
#

so i could just see if the num is just equal to the type itesself

knotty quiver
#

yeah, so enums start at 0, and count up from there

#

so generally peopel do this

#
enum whatever {
  VARIANT1,
  VARIANT2,
  ...,
  VARIANTWHATEVER,
  PRIVATE_LAST_VARIANT
};
#

and then they just check to see if their enum whatever they just read is < PRIVATE_LAST_VARIANT

#

a-la

vernal badger
#

ahhhhhhh

knotty quiver
#
reader = fscanf(fp, "|%d|%d\n", &enum_int, &credit_hours);
printf("int %d, hours %d\n", enum_int, credit_hours);

if (reader == 2 && enum_int >= PRIVATE_LAST_VARIANT)
  error_out_with_messagE("bad enum variant: %d", enum_int);
#

anyways, so my first recommendation is to break this apart into functions

#

your entire do ... while loop should be a function (maybe call it "get courses" ?)

#

whenever a chunk of code is longer than 5 or so lines, generally breaking it out into a function is the right idea

vernal badger
#

my do while is essentially the whole entire function

knotty quiver
#

yeah

#

well, ok, step back

#

i'm giving you the stackoverflow "do it this way not that way" answer lol

#

would you rather i just help you find the bug or to clean it up too

vernal badger
#

fix the bug, im kind of in a time crunch to submit this

knotty quiver
#

sounds good

#

how much of a time crunch? out of curiosity

#

like, hours? or a day or so

vernal badger
#

got until noon tomorrow but i wanna sleep

knotty quiver
#

mk

#

can you zip up everythign you have and send it to me

#

and stay tuned i'll have questions

vernal badger
#

im on a linux shell from my university so i cant really zip any files

knotty quiver
#

ah ok

#

rip, iw as hoping to rapidly test locally

#

ok, let me take another look

vernal badger
#

i could pastebin some test cases i do have though

knotty quiver
#

it's useless without the hw6.h header

#

oo wait

#

ok hold on

#
      printf("---------------------------------------------\n");
      printf("g num course: %d\n", g_course_count);
      printf("course name: %s\n", course.course_name);
      printf("pre reqs: ");
      for(int i = 0; i < MAX_PREREQ; i++) {
        printf("%s ", course.prerequisites[i]);
      }
      printf("\n");
      printf("enum #: %d\n", (int) course.course_type);
      printf("hours: %d\n", course.credit_hours);
      printf("---------------------------------------------\n");
#

is hte issue the course.prerequesites being printed incorrectly?

#

OH

#

i see your issue

#

for(int i = 0; i < MAX_PREREQ; i++) {

#

for(int i = 0; i < num_prereq; i++) {

#

try that lol

#

i cant believe i didnt catch that at the start

vernal badger
#

thats just for the prints

#

its not being stored in the global array correctly

knotty quiver
#

that's still an issue though

vernal badger
#

or in the struct rather

knotty quiver
#

also, your indentation is all over the place lol

vernal badger
#

yea im aware

#

i fix it to adhere to the coding standard they give me

knotty quiver
#

what's this reader variable

vernal badger
#

checks the value of fscanf

#

to make sure it reads in the string

knotty quiver
#

also, can you rerun it with more clearly defined error cases

#

or, rather

#

which thing in your example input is incorrect

vernal badger
#

yea i have buffer error cases

#

such as

knotty quiver
#

mm

#

wat lol

vernal badger
#

which i also fail

knotty quiver
#

how about this

#

do course1, course2, etc. and then for prereqs to course1-prereq1, course1-prereq2,e tc

#

then show me the output of that

vernal badger
#

so if the name or any prereqs are over 200 chars, its a buffer error, if its less but over the max len of 80 you truncate it to fit into the course name

#

thats the buffer error

#

which i also dont know how im not dealing with

knotty quiver
#

oh so youre sure it's a buffer error

vernal badger
#

almost positive

knotty quiver
#

ok

#

i'll take a closer look at that, i was looking at the variables

#

can i see the declaration of course_t

vernal badger
#

is it how i read the prerequisites in? because some classes do not have pre requisites and you also do not know how many pre reqs they have

knotty quiver
#

it's very very weird that it doesnt have the count of prereqs in it

vernal badger
#

and each pre req must have ? before it

knotty quiver
#

i presume you didnt make that type

vernal badger
#

nope it was provided

knotty quiver
#

that's fuckign weird

vernal badger
#

yea

knotty quiver
#

what's MAX_PREREQ, and MAX_NAME_LEN

vernal badger
#

max pre req is 3 and name len is 80

knotty quiver
#

ahh

#

ok

vernal badger
#

so 79 with a null

#

for the name

knotty quiver
#

yeah

#

gimmie onee second i gotta grab something

vernal badger
#

okay

knotty quiver
#

were getting close

#

ok

#

in here, which of these is a problem

vernal badger
#

pol 348 should not have KOR

#

i mightve found something

#

gimme a sec

#

so when you told me to change the for loop printing to num_prereq instead of MAX_PREREQ it printed the correct values

#

which means its still storing it even though im restricting it

knotty quiver
#

oo

vernal badger
#

and index 2 was empty

#

but its weird cause i reset num_prereq at the end of the do while

knotty quiver
#

yeah

sharp ridgeBOT
#

@vernal badger Has your question been resolved? If so, run !solved :)

vernal badger
#

bot is trolling me

#

still havent got it

knotty quiver
#

rip

#

well it's now 1am and i have to sleep, i'll check in on you in the am

vernal badger
#

its always storing an extra pre req

#

ok

#

its 4 here

#

am

knotty quiver
#

rippp

vernal badger
#

got it

#

lets go

#

im such an idiot

#

i should've initialized the course at the beginning of the do while

knotty quiver
#

oh nice

#

im falling asleep now im glad you figured it out lol

#

mark the thread as done

vernal badger
#

!solved

sharp ridgeBOT
#

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

#

@vernal badger

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

knotty quiver
#

👍