Thread: Needs help on finding bug

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    Needs help on finding bug

    I am trying to build a menu based program. User gets to take records (min 5 & max 15) of students. after taking records, user gets to select 6 functionalities.
    1. print records
    2. Search by first name - prints record of the student with a given first name. If there are multiple students with the same first name, print records for all of them
    3. Search by last name - prints record of the student with a given last name. If there are multiple students with the same lastname, print records for all of them
    4. Sort by score - sort the records of studens according to their scores, and then print the sorted records
    5. sort by last name
    6. Exit the program

    I was able to get my function 1 and 6 right. But for some reason, 2 to 5 functionalities dont work. Can somebody help me figure out what am i doing wrong?

    here is the code

    Code:
    #define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    void menu();
    void print_records(int size, char firstname[][20], char lastname[][20], float score[]);
    void search_by_firstname(int size, char firstname[][20], char lastname[][20], float score[]);
    void search_by_lastname(int size, char firstname[][20], char lastname[][20], float score[]);
    void sort_by_score(int size, char firstname[][20], char lastname[][20], float score[]);
    void sort_by_lastname(int size, char firstname[][20], char lastname[][20], float score[]);
    
    
    
    
    int main(void)
    {
    	char fn[15][20]; // 15 rows 20 columns or 15 students 20 characters
    	char ln[15][20]; // 15 rows 20 columns or 15 students 20 characters
    	float sc[15]; // 15 scores max not all used
    	int n = 0;
    	int i = 0;
    	int x = -1;
    	char nametofind[20];
    
    
    	printf("Please indicate the number of records you want to enter (min 5, max 15):\n");
    	scanf("%d", &n);
    
    
    	printf("Please input records of students (enter a new line after each record), with the following format \nfirst name last name score\n");
    	for (i = 0; i<n; i++)
    	{
    		scanf("%s %s %f", &fn[i][0], &ln[i][0], &sc[i]);       //stores first name and last name characters in ith row starting at collumn 0
    		// score is stored in sc array starting at index 0
    	}
    	do
    	{
    		printf("\n---you may access the records by---\n");
    		printf("Print records (press 1)\n");
    		printf("Search by first name (press 2)\n");
    		printf("Search by last name (press 3)\n");
    		printf("Sort by score (press 4)\n");
    		printf("Sort by last name (press 5)\n");
    		printf("Exit program (press 0)\n");
    		printf("--------------------------\n\n");
    		printf("Please a function by entering a value from 0 to 5.\n");
    		scanf_s("%d", &x);
    
    
    		printf("You have selected option: %d\n", x);
    		switch (x)
    		{
    		case 1:
    			print_records(n, fn, ln, sc);
    			break;
    		case 2:
    			search_by_firstname(n, fn, ln, sc);
    			break;
    		case 3: search_by_lastname(n, fn, ln, sc);
    			break;
    		case 4: sort_by_score(n, fn, ln, sc);
    			break;
    		case 5: sort_by_lastname(n, fn, ln, sc);
    			break;
    		default: printf("Please only enter a number from 0 to 5\n");
    		}
    	} while (x != 0);
    	return 0;
    }
    void print_records(int size, char firstname[][20], char lastname[][20], float score[])
    {
    	int i = 0;
    	for (i = 0; i < size; i++)
    	{
    		printf("First Name: %s, Last Name: %s, Score: %.2f\n", firstname[i], lastname[i], score[i]);
    	}
    
    
    }
    void search_by_firstname(int size, char firstname[][20], char lastname[][20], float score[])
    {
    
    
    	int i = 0;
    	char nametofind[20];
    	printf("Please enter the first name of student record you wish to print: ");
    	/*fflush(stdin);*/
    	gets(nametofind);
    	for (i = 0; i<size; i++)
    	{
    		if (strcmp(nametofind, firstname[i]) == 0) // if nametofind is found in firstname array
    		{
    			printf("First Name: %s, Last Name: %s, Score: %.2f\n", firstname[i], lastname[i], score[i]);
    		}
    	}
    }
    
    
    void search_by_lastname(int size, char firstname[][20], char lastname[][20], float score[])
    {
    	int i = 0;
    	char nametofind[20];
    	printf("Please enter last name of student record you wish to print: ");
    	/*fflush(stdin);*/
    	gets(nametofind);
    	for (i = 0; i<size; i++)
    	{
    		if (strcmp(nametofind, lastname[i]) == 0) // if nametofind is found in lastname array
    		{
    			printf("First Name: %s, Last Name: %s, Score: %.2f\n", firstname[i], lastname[i], score[i]);
    		}
    	}
    }
    
    
    void sort_by_score(int size, char firstname[][20], char lastname[][20], float score[])
    {
    	int i = 0;
    	int j = 0;
    	float tempscore;
    	char tempfirstname[20];
    	char templastname[20];
    
    
    	// bubble sort
    	for (i = 0; i<size - 1; i++)
    	{
    		for (j = 0; j<size - i - 1; j++)
    		{
    			if (score[j]>score[j + 1])
    			{
    				tempscore = score[j];
    				score[j] = score[j + 1];
    				score[j + 1] = tempscore;
    
    
    				strcpy(tempfirstname, firstname[j]);
    				strcpy(firstname[j], firstname[j + 1]);
    				strcpy(firstname[j + 1], tempfirstname);
    
    
    				strcpy(templastname, lastname[j]);
    				strcpy(lastname[j], lastname[j + 1]);
    				strcpy(lastname[j + 1], templastname);
    			}
    		}
    	}
    	print_records(size, firstname, lastname, score);
    }
    
    
    void sort_by_lastname(int size, char firstname[][20], char lastname[][20], float score[])
    {
    	int i = 0;
    	int j = 0;
    	float tempscore;
    	char tempfirstname[20];
    	char templastname[20];
    
    
    	// bubble sort
    	for (i = 0; i<size - 1; i++)
    	{
    		for (j = 0; j<size - i - 1; j++)
    		{
    			if (strcmp(lastname[j], lastname[j + 1])>0)
    			{
    				strcpy(tempfirstname, firstname[j]);
    				strcpy(firstname[j], firstname[j + 1]);
    				strcpy(firstname[j + 1], tempfirstname);
    
    
    				strcpy(templastname, lastname[j]);
    				strcpy(lastname[j], lastname[j + 1]);
    				strcpy(lastname[j + 1], templastname);
    
    
    				tempscore = score[j];
    				score[j] = score[j + 1];
    				score[j + 1] = tempscore;
    			}
    		}
    	}
    	print_records(size, firstname, lastname, score);
    } 

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Quote Originally Posted by Atish Shrestha View Post

    Code:
            printf("Please a function by entering a value from 0 to 5.\n");
            scanf_s("%d", &x);

    The buffer still contains the newline character..

    Try placing this line of code after the 2 lines above to flush the buffer

    Code:
    while(getchar() != '\n');
    btw gets() is now deprecated and fgets() should be used instead.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Also cross-posted here -> Cant get output right.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    2
    Quote Originally Posted by Salem View Post
    Also cross-posted here -> Cant get output right.
    Thank you very much. That worked. You are a life saver. thanx again

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I've changed a few things for you:
    Code:
    printf("Please enter the first name of student record you wish to print: ");
    fflush(stdout);
    fgets(nametofind, sizeof(nametofind), stdin);
    The fflush(stdout) clears the output buffer (changed from stdin) - The C standard specifies that stdout does not have to flush (to the screen in your implementation) until it finds a new line. Although you probably will never see a system with that limitation, it is good practice to have that after a printf that does not end with a \n.

    And the fgets instead of the gets is needed - It basically stops the user from breaking stuff. The syntax is very simple as well - array, size of array, stream.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the max in row
    By qweqwe in forum C Programming
    Replies: 5
    Last Post: 12-04-2011, 10:46 PM
  2. Finding the mean
    By openwindow in forum C Programming
    Replies: 2
    Last Post: 11-20-2011, 07:34 AM
  3. GPA and finding a job
    By camel-man in forum General Discussions
    Replies: 3
    Last Post: 10-17-2011, 06:18 AM
  4. Finding Max and Min Value
    By will15 in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2006, 11:27 AM
  5. finding a job!
    By nima_ranjbar in forum C++ Programming
    Replies: 2
    Last Post: 02-12-2002, 12:00 PM