Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32
  1. #1

    Default Logical Error? Please HELP!


    Mga Bro, Good Day! Naa nasad ko diri karon to ask for help from your most gracious wisdom! Anyways, I have another C programming language problem. This time, it includes structures, pointer to pointer and linked lists. I'm posting my code first:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    #define MAX 100
    
    typedef struct {
     char fname[40];
     char lname[40];
     char mi;
    }NAMETYPE;
    typedef struct {
     NAMETYPE name;
     long int id;
     char course[20];
     int year;
     float GPA;
    }STUDREC;
    
    struct node {
     STUDREC x;
     struct node *next;
    };
    typedef struct node nd;
    
    int GetStudRec(STUDREC *, nd *);
    void Search(nd *);
    void DisplayStudRec(nd *);
    void DisplayAllStudentRec(nd *);
    void DisplayTopStudent(nd *, int);
    
    int main(void)
    {
     STUDREC s[MAX];
     nd *head;
     int last;
     clrscr();
    
     head=(nd *)malloc(sizeof(nd));
     last = GetStudRec(s, head);
     Search(head);
     DisplayAllStudentRec(head);
     DisplayTopStudent(head, last);
    
     return 0;
    }
    int GetStudRec(STUDREC *s, nd *head)
    {
     nd *p;
     int i=0, j, last;
     char ch;
     float a;
    
     do
     {
      printf("First name of student %d: ", i+1);
      scanf("%s", s[i].name.fname);
      printf("Last name of student %d: ", i+1);
      scanf("%s", s[i].name.lname);
      printf("Middle initial of student %d: ", i+1);
      fflush(stdin);
      scanf("%c", &s[i].name.mi);
      printf("ID number of student %d: ", i+1);
      scanf("%d", &s[i].id);
      printf("Course of student %d: ", i+1);
      scanf("%s", s[i].course);
      printf("Year of student %d: ", i+1);
      scanf("%d", &s[i].year);
      printf("GPA of student %d: ", i+1);
      scanf("%f", &a);
      s[i].GPA=a;
    
      printf("\n");
    
      printf("Do you wish to continue? Y/N ");
      fflush(stdin);
      scanf("%c", &ch);
    
      if (ch=='Y' || ch=='y')
      {
       j=1;
       i++;
       printf("\n");
      }
      else if (ch=='N' || ch=='n')
      {
       j=0;
       last=i;
      }
     }while (j!=0 && i<MAX);
     printf("\n");
    
     head->x=s[0];
     p=head;
     for (i=1;i<=last;i++)
     {
      p->next=(nd *)malloc(sizeof(nd));
      p->next->x=s[i];
      p=p->next;
     }
     p->next=NULL;
    
     return (last);
    }
    void Search(nd *head)
    {
     nd *p;
     long int iden;
    
     p=head;
    
     printf("Student ID to search: ");
     scanf("%d", &iden);
    
     while (p->x.id!=iden && p!=NULL)
      p=p->next;
     if (iden==p->x.id)
      DisplayStudRec(p);
     else if (p==NULL)
     {
      printf("\n");
      printf("STUDENT DOES NOT EXIST\n");
     }
    }
    void DisplayStudRec(nd *p)
    {
     printf("The student you are looking for:\n");
     printf("Name: %s %c %s\tID number: %d\n", p->x.name.fname, p->x.name.mi, p->x.name.lname, p->x.id);
     printf("Course: %s\t\t\tYear: %d\n", p->x.course, p->x.year);
     printf("GPA: %.1f\n", p->x.GPA);
     printf("\n");
    }
    void DisplayAllStudentRec(nd *head)
    {
     nd *p;
    
     p=head;
     printf("\nRecord of all students:\n");
     while (p!=NULL)
     {
      printf("Name: %s %c %s\tID number: %d\n", p->x.name.fname, p->x.name.mi, p->x.name.lname, p->x.id);
      printf("Course: %s\t\t\tYear: %d\n", p->x.course, p->x.year);
      printf("GPA: %.1f\n", p->x.GPA);
      printf("\n");
      p=p->next;
     }
    }
    void DisplayTopStudent(nd *head, int last)
    {
     nd *p, *q;
     float temp;
     int count=0;
     q=head;
    
     if (last>0)
      while (q!=NULL && count!=last)
      {
       p=q->next;
       if (q->x.GPA > p->x.GPA)
        temp=q->x.GPA;
       q=p;
       p=p->next;
       count++;
      }
     else if (last==0)
      temp=p->x.GPA;
    
     printf("Student with the Highest GPA: %.1f\n", temp);
    }
    Explain nako briefly ang expected operation each function: (These functions are required though we can add more functions if we have to.)
    -GetStudRec: Mukuha siya ug information about a student. Then transfers the data into a linked list.
    -Search: Asks an input (student ID) from the user, then searches the linked list for that specific student of that distinct ID.
    -DisplayStudRec: Mao ni ang tawagon ni Search() function if naay match nga student ID.
    -DisplayAllStudentRec: Displays all student records.
    -DisplayTopStudent: I-display niya ang student nga naay pinaka dako ug GPA from the linked list.

    Ang problems nako ani nga code kay kani:
    -Sa Search() function: pirmi NULL ang labas sa pag trace nako.
    -Sa pag print sa student with the highest GPA: zero man ang iyang i-print. Nag provide ko ug duha ka pointers so that ma follow nila ang liked list and para mas dali nila ma compare ang sulod.

    Mga bro, naa sad koy mga questions that I want to ask in continuation ani:
    Is it possible to call function main()? Included man gud sa problem namo nga maghimo ug user interface nga mu ask sa user unsa iyang gusto buhaton after inputting the student records. Naghuna-huna ko nga magbutang ug call ni main() in order to provide an effective program.

    Mga Bro, Thank You kaayo sa inyong tabang kanako! I know "noobs" ko, that's why ning ask ko ninyo ug tabang. I acknowledge my limitations but I don not accept defeat. I hope matabangan ko ninyo mga bro! Thank You!

  2. #2
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    declaration:
    Code:
    void DisplayTopStudent(nd *);
    function call @ main:
    Code:
    DisplayTopStudent(head);
    function code:
    Code:
    void DisplayTopStudent(nd *head)
    {
     nd *p;
     p=head;
     float highestGPA=0.0;
    
     while (p!=NULL)
     {
    
      if( highestGPA < p->x.GPA) highestGPA = p->x.GPA;
      p=p->next;
     }
    
     printf("Student with the Highest GPA: %.1f\n", highestGPA);
    
    }

    sample output:
    Code:
    First name of student 1: m
    Last name of student 1: c
    Middle initial of student 1: c
    ID number of student 1: 10
    Course of student 1: coe
    Year of student 1: 2001
    GPA of student 1: 88
    
    Do you wish to continue? Y/N y
    
    First name of student 2: d
    Last name of student 2: d
    Middle initial of student 2: d
    ID number of student 2: 100
    Course of student 2: doe
    Year of student 2: 2001
    GPA of student 2: 75
    
    Do you wish to continue? Y/N y
    
    First name of student 3: y
    Last name of student 3: y
    Middle initial of student 3: y
    ID number of student 3: 50
    Course of student 3: tt
    Year of student 3: 2001
    GPA of student 3: 91
    
    Do you wish to continue? Y/N n
    
    Student ID to search: 50
    The student you are looking for:
    Name: y y y     ID number: 50
    Course: tt                      Year: 2001
    GPA: 91.0
    
    
    Record of all students:
    Name: m c c     ID number: 10
    Course: coe                     Year: 2001
    GPA: 88.0
    
    Name: d d d     ID number: 100
    Course: doe                     Year: 2001
    GPA: 75.0
    
    Name: y y y     ID number: 50
    Course: tt                      Year: 2001
    GPA: 91.0
    
    Student with the Highest GPA: 91.0
    Press any key to continue...

  3. #3
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    Quote Originally Posted by marcdaven View Post
    Mga bro, naa sad koy mga questions that I want to ask in continuation ani: Is it possible to call function main()? Included man gud sa problem namo nga maghimo ug user interface nga mu ask sa user unsa iyang gusto buhaton after inputting the student records. Naghuna-huna ko nga magbutang ug call ni main() in order to provide an effective program.
    Yes it is possible. but take extra care on infinite recursive call, multiple variable declaration, assigning pointers & memory location... as this is prone to bugs.

    and to be honest, you don't have to do that... OS handles your main, that is why the return value from main is pass/signal to operating system if it is normally terminated. for case of UI, just make another function, more easy during modifications and debugging.

    Code:
    void func(void);
    
    int CSTAT = 0; /*Global*/
    
    int main(void)
    {
    	if (CSTAT < 5) /* try to remove this line see what happens*/
    	func();
    	return 0;
    }
    
    void func(void)
    {
    	printf("hello fron func %d\n", CSTAT);
    	CSTAT++;
    	main();
    	
    }

  4. #4
    Quote Originally Posted by MarkCuering View Post
    for case of UI, just make another function, more easy during modifications and debugging.
    Yes, bro. I'll create a new function to call my user interface. I'm currently editing my code. I'll post here if naka run ko sa expected output.

    Bro, ask lang pud ko, wala ning error ang pag search sa desired student? Pirmi man gud NULL ang makit-an sa logic nako. Do I have to blame TC? Then ang GPA pud diay unta pud bro kay i-print ang tibuok record sa student.

    Thank you sa help bro!

  5. #5
    @TS, ako giusab imong code para readable unya straight to the point ang program, daghan man gud pakapin (ex. array sa main function - not needed). latest version sa gcc under sa linux akong gigamit pag-compile sa source code pero ako pud siya gisulayan sa turbo c sa dos. tan-awa lang giunsa nako. naa pud ko gi-apil nga screenshot sa ubos.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define FALSE 0
    
    typedef struct
    {
    	char first[40];
    	char last[40];
    	char middle;
    } NAME;
    
    typedef struct
    {
    	NAME name;
    	long int id;
    	char course[20];
    	int year;
    	float gpa;
    } STUDENT;
    
    struct NODE
    {
    	STUDENT student;
    	struct NODE *next;
    };
    
    void GetStudRec(struct NODE *);
    void DisplayAllStudentRec(struct NODE *);
    void Search(struct NODE *);
    void DisplayTopStudent(struct NODE *);
    void DisplayStudRec(STUDENT);
    void DisplayDivider(void);
    void Pause(void);
    
    int main(void)
    {
    	struct NODE *record;
    
    	float x = 0;	// Fix Borland's wrong compiler implementation.
    	float *y = &x;  // If you're not using one of Borland's old compilers,
    	x = *y;		// please remove this lines. Turbo C/C++ sucks.
    
    	record = (struct NODE *) malloc(sizeof(struct NODE));
    	GetStudRec(record);
    	Search(record);
    	DisplayAllStudentRec(record);
    	DisplayTopStudent(record);
    
    	return 0;
    }
    
    void GetStudRec(struct NODE *record)
    {
    	int done = FALSE;
    	int index = 1;
    	char input = 0;
    	STUDENT student;
    
    	while (record->next != NULL)
    	{
    		index++;
    		record = record->next;
    	}
    
    	while (!done)
    	{
    		printf("First name of student %d : ", index);
    		scanf("%s", student.name.first);
    
    		printf("Last name of student %d : ", index);
    		scanf("%s", student.name.last);
    
    		printf("Middle initial of student %d : ", index);
    		scanf("%s", &student.name.middle);
    
    		printf("ID number of student %d : ", index);
    		scanf("%ld", &student.id);
    
    		printf("Course of student %d : ", index);
    		scanf("%s", student.course);
    
    		printf("Year of student %d : ", index);
    		scanf("%d", &student.year);
    
    		printf("GPA of student %d : ", index);
    		scanf("%f", &student.gpa);
    
    		record->student = student;
    
    		record->next = (struct NODE *) malloc(sizeof(struct NODE));
    		record->next->next = NULL;
    		record = record->next;
    
    		input = 0;
    		printf("Do you wish to continue (Y/N)? ");
    		while (input != 'Y' && input != 'N')
    		{
    			input = getchar();
    			input = input - 32;
    		}
    
    		if (input == 'N')
    			done = !FALSE;
    		else
    		{
    			index++;
    			DisplayDivider();
    		}
    	}
    }
    
    void Search(struct NODE *record)
    {
    	int found = FALSE;
    	long int id;
    
    	DisplayDivider();
    	printf("Student ID to search : ");
    	scanf("%ld", &id);
    	getchar();
    
    	while(record->next != NULL)
    	{
    		if (record->student.id == id)
    		{
    			printf("The student you are looking for is :\n");
    			DisplayStudRec(record->student);
    			found = !FALSE;
    			break;
    		}
    		record = record->next;
    	}
    	if (!found)
    		printf("Student ID does not exist!\n");
    	Pause();
    }
    
    void DisplayAllStudentRec(struct NODE *record)
    {
    	DisplayDivider();
    	printf("Record of all students :\n");
    	while(record->next != NULL)
    	{
    		DisplayStudRec(record->student);
    		record = record->next;
    		Pause();
    		if (record->next != NULL)
    		{
    			DisplayDivider();
    			printf("Record of all students (continuation) :\n");
    		}
    	}
    }
    
    void DisplayTopStudent(struct NODE *record)
    {
    	STUDENT student;
    	DisplayDivider();
    	printf("Student with the highest GPA :\n");
    	while(record->next != NULL)
    	{
    		if (student.gpa < record->student.gpa) student = record->student;
    		record = record->next;
    	}
    	DisplayStudRec(student);
    	Pause();
    }
    
    void DisplayStudRec(STUDENT student)
    {
    	printf("ID number : %ld\n", student.id);
    	printf("Name : %s %c %s\n", student.name.first, student.name.middle, student.name.last);
    	printf("Course : %s\n", student.course);
    	printf("Year : %d\n", student.year);
    	printf("GPA : %.1f\n", student.gpa);
    }
    
    void DisplayDivider(void)
    {
    	printf("------------------------------------------\n");
    }
    
    void Pause(void)
    {
    	char input;
    	printf("Press ENTER to continue....");
    	while ((input = getchar()) != '\n');
    }
    screenshot:



    good luck sa programming

  6. #6
    @darkhero: Thanks sa code bro! But, I was told man gud recently that we have to show the user interface first, even before inputting the student records or showing the records. Mauwaw nako ug ask pa ug maayo ninyo mga bro... Try ko ni ug revise imong code bro according to our requirements, basin makaya ra ni nako..

  7. #7
    Quote Originally Posted by marcdaven View Post
    @darkhero: Thanks sa code bro! But, I was told man gud recently that we have to show the user interface first, even before inputting the student records or showing the records. Mauwaw nako ug ask pa ug maayo ninyo mga bro... Try ko ni ug revise imong code bro according to our requirements, basin makaya ra ni nako..
    i-describe daw bro unsa nga interface... menu? wa man pud ko lingaw hehe. tabang2x lang ko.

  8. #8
    Hatagan tika ani nga example bro:

    Code:
    printf("Enter desired operation:\n");
     printf("(1) Add Student Record\n");
     printf("(2) Search Student Record\n");
     printf("(3) Display all Student Records\n");
     printf("(4) Display Top Student\n");
     printf("(5) Exit\n");
     scanf("%d", &x);
    ...

  9. #9
    ah ok. tan-awa daw akong giusab, menu ray napuno unya naay changes nga gamay sa original post nko, i-compare dira sa imoha... btw, this time wala na nko ni gi-try sa turbo c, diretso na lang ko sa gcc kay kapoy na ug testing sa turbo

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define FALSE 0
    
    typedef struct
    {
    	char first[40];
    	char last[40];
    	char middle;
    } NAME;
    
    typedef struct
    {
    	NAME name;
    	long int id;
    	char course[20];
    	int year;
    	float gpa;
    } STUDENT;
    
    struct NODE
    {
    	STUDENT student;
    	struct NODE *next;
    };
    
    /* Prototypes */
    void DisplayMenu(void);
    void GetStudRec(struct NODE *);
    void DisplayAllStudentRec(struct NODE *);
    void Search(struct NODE *);
    void DisplayTopStudent(struct NODE *);
    void DisplayStudRec(STUDENT);
    void Pause(void);
    
    int main(void)
    {
    	/* 
    	 * Fix Borland's wrong compiler implementation.
    	 * If you're not using one of Borland's old compilers,
    	 * please remove the following 3 lines. Turbo C/C++ sucks.
    	 */
    	float x = 0;
    	float *y = &x;
    	x = *y;
    
    	DisplayMenu();
    	return 0;
    }
    
    void DisplayMenu(void)
    {
    	struct NODE *record;
    	int done = FALSE;
    	char input;
    
    	/* Linked list initialization */
    	record = (struct NODE *) malloc(sizeof(struct NODE));
    	record->next = NULL;
    	while (!done)
    	{
    		printf("----------- MAIN MENU ------------\n\n");
    		printf("(1) ADD STUDENT RECORD\n");
    		printf("(2) SEARCH STUDENT RECORD\n");
    		printf("(3) DISPLAY ALL STUDENT RECORDS\n");
    		printf("(4) DISPLAY TOP STUDENT\n");
    		printf("(5) EXIT\n\n");
    		input = 0;
    		while (!(input >= '1' && input <= '5'))
    		{
    			printf("ENTER DESIRED OPERATION (1-5) : ");
    			scanf("%s", &input);
    
    			/* 
    			 * Remove the newline character that
    			 * scanf left in the input stream.
    			 */
    			getchar();
    
    			if (!(input >= '1' && input <= '5')) printf("INVALID OPERATION. PLEASE TRY AGAIN.\n\n");
    		}
    		printf("\n");
    		if (input == '1')
    		{
    			printf("------- ADD STUDENT RECORD -------\n\n");
    			GetStudRec(record);
    		}
    		else if (input == '2')
    		{
    			printf("----- SEARCH STUDENT RECORD ------\n\n");
    			Search(record);
    		}
    		else if (input == '3')
    		{
    			printf("-- DISPLAY ALL STUDENT RECORDS ---\n\n");
    			DisplayAllStudentRec(record);
    		}
    		else if (input == '4')
    		{
    			printf("------- DISPLAY TOP STUDENT ------\n\n");
    			DisplayTopStudent(record);
    		}
    		else if (input == '5')
    		{
    			printf("GOODBYE!\n");
    			done = !FALSE;
    			Pause();
    		}
    		printf("\n");
    	}
    
    	/*
    	 * Some OS does not or cannot reclaim memory properly so we have to
    	 * free all of it manually. MS-DOS AKA messy-dos sucks!
    	 */
    	while (record->next != NULL)
    	{
    		struct NODE *pointer;
    		pointer = (struct NODE *) malloc(sizeof(struct NODE));
    		pointer = record;
    		free(record);
    		record = pointer->next;
    	}
    }
    
    void GetStudRec(struct NODE *record)
    {
    	int done = FALSE;
    	int index = 1;
    	char input;
    	STUDENT student;
    
    	/* Loop until a blank record is found */
    	while (record->next != NULL)
    	{
    		index++;
    		record = record->next;
    	}
    	while (!done)
    	{
    		printf("FIRST NAME OF STUDENT %d : ", index);
    		scanf("%s", student.name.first);
    		printf("LAST NAME OF STUDENT %d : ", index);
    		scanf("%s", student.name.last);
    		printf("MIDDLE INITIAL OF STUDENT %d : ", index);
    		scanf("%s", &student.name.middle);
    		printf("ID NUMBER OF STUDENT %d : ", index);
    		scanf("%ld", &student.id);
    		printf("COURSE OF STUDENT %d : ", index);
    		scanf("%s", student.course);
    		printf("YEAR OF STUDENT %d : ", index);
    		scanf("%d", &student.year);
    		printf("GPA OF STUDENT %d : ", index);
    		scanf("%f", &student.gpa);
    		record->student = student;
    		record->next = (struct NODE *) malloc(sizeof(struct NODE));
    		record->next->next = NULL;
    		record = record->next;
    		input = 0;
    		while (input != 'Y' && input != 'N')
    		{
    			printf("\nADD ANOTHER STUDENT RECORD (Y/N)? ");
    			scanf("%s", &input);
    			input = input - 32;
    			if (input != 'Y' && input != 'N')
    			{
    				printf("INVALID RESPONSE. PLEASE TRY AGAIN.\n");
    			}
    		}
    		if (input == 'N')
    			done = !FALSE;
    		else
    		{
    			index++;
    			printf("\n");
    		}
    	}
    }
    
    void Search(struct NODE *record)
    {
    	int found = FALSE;
    	long int id;
    
    	printf("ENTER STUDENT ID TO SEARCH : ");
    	scanf("%ld", &id);
    
    	/*
    	 * Remove the newline character that
    	 * scanf left in the input stream.
    	 */
    	getchar();
    
    	while (!found && record->next != NULL)
    	{
    		if (record->student.id == id)
    		{
    			printf("\nTHE STUDENT YOU'RE LOOKING FOR IS :\n\n");
    			DisplayStudRec(record->student);
    			found = !FALSE;
    		}
    		record = record->next;
    	}
    	if (!found) printf("\nSTUDENT ID DOES NOT EXIST!\n");
    	Pause();
    }
    
    void DisplayAllStudentRec(struct NODE *record)
    {
    	int index = 0;
    	while (record->next != NULL)
    	{
    		index++;
    		DisplayStudRec(record->student);
    		record = record->next;
    		Pause();
    		if (record->next != NULL) printf("\n");
    	}
    	if (index == 0)
    	{
    		printf("STUDENT RECORDS DOES NOT EXIST!\n");
    		Pause();
    	}
    }
    
    void DisplayTopStudent(struct NODE *record)
    {
    	int index = 0;
    	STUDENT student;
    
    	student.gpa = 0;
    	while (record->next != NULL)
    	{
    		index++;
    		if (student.gpa < record->student.gpa) student = record->student;
    		record = record->next;
    	}
    	if (index > 0)
    	{
    		printf("STUDENT WITH THE HIGHEST GPA IS :\n\n");
    		DisplayStudRec(student);
    	}
    	else
    		printf("STUDENT RECORDS DOES NOT EXIST!\n");
    
    	Pause();
    }
    
    void DisplayStudRec(STUDENT student)
    {
    	printf("ID NUMBER : %ld\n", student.id);
    	printf("NAME : %s %c %s\n", student.name.first, student.name.middle, student.name.last);
    	printf("COURSE : %s\n", student.course);
    	printf("YEAR : %d\n", student.year);
    	printf("GPA : %.1f\n", student.gpa);
    }
    
    void Pause(void)
    {
    	char input;
    	printf("\nPRESS ENTER TO CONTINUE....");
    	while ((input = getchar()) != '\n');
    }

  10. #10
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    if you want some nice ui, why not try to make some boxes? or turn your graphics into VGA mode?

  11.    Advertisement

Page 1 of 4 123 ... LastLast

Similar Threads

 
  1. cross fire error. please help me with this
    By orvillejoy in forum Video Games
    Replies: 6
    Last Post: 02-14-2011, 09:21 AM
  2. LTDIS1N.dll error please help!
    By vienabyam in forum Programming
    Replies: 1
    Last Post: 11-11-2010, 04:41 PM
  3. codec error..please help!!!
    By mikhailbogz in forum Software & Games (Old)
    Replies: 30
    Last Post: 10-14-2009, 11:42 PM
  4. Java Error... Please help!!!
    By happy_chique in forum Software & Games (Old)
    Replies: 3
    Last Post: 08-22-2008, 09:08 AM
  5. service error 6A00 for pixma ip4200..please help
    By zney25 in forum Computer Hardware
    Replies: 6
    Last Post: 07-10-2008, 10:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top