logo
down
shadow

My program won't do search function well. Why?


My program won't do search function well. Why?

By : Joey Kane
Date : November 22 2020, 10:31 AM
seems to work fine I made a student database program as my latest project in school. Everything works fine, you can create records with ID's, names, surnames and marks. , This answers your question, but please fix the if thing
code :
if(student.firstname[length]==studentname[length])
if(strcmp(student.firstname[length], studentname[length]) == 0)
scanf("%s", &studentname);
scanf("%s", studentname);
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct
{
    long long int id;
    char firstname[20];
    char lastname[20];
    int mark;
} student;

void
storeRecord()
{
    FILE *fp;

    printf("\nEnter Student Details:\n\nID number: ");
    scanf("%lld",&student.id);

    printf("\nName:");
    scanf("%19s",student.firstname);

    printf("\nSurname:");
    scanf("%19s",student.lastname);

    printf("\nMark(0 - 100 integer) : ");
    scanf("%d",&student.mark);

    fp = fopen("studentfile.txt","a+"); /* check if the file was opened */
    if (fp == NULL)
        return;
    fprintf(fp, "\n%lld\t%s\t%s\t%d\t", 
        student.id, 
        student.firstname, 
        student.lastname, 
        student.mark);
    fclose(fp);

    printf("A student record has been added successfully...\n");
    getchar();
}

void
printStudent()
{
    printf("\nThe record is found.\n");
    printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",
        student.id,
        student.firstname,
        student.lastname,
        student.mark
    );
}

void
searchStudentByName()
{
    char  studentname[20];
    FILE *fp;
    int   found;
    int   matches;

    printf("\nEnter student first name: ");
    scanf("%19s", studentname);

    printf("Searching record with studentname=%s.\n", studentname);

    found = 0;
    fp    = fopen("studentfile.txt", "r");
    if (fp == NULL)
    {
        printf("IO error\n");
        return;
    }

    matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
        &student.id, 
        student.firstname, 
        student.lastname, 
        &student.mark);

    do
    {
        matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
            &student.id, 
            student.firstname, 
            student.lastname, 
            &student.mark);
        if (matches == 4)
            found = (strcmp(student.firstname, studentname));
    } while ((matches == 4) && (found == 0));
    if (found != 0)
        printStudent(); 
    else
        printf("Not found...\n");
    getchar();
}

void
searchStudentById()
{
    int   id;
    int   found;
    int   matches;
    FILE *fp;

    printf("\nEnter student first name: ");
    scanf("%d", &id);

    printf("Searching record with id=%d.\n", id);

    found = 0;
    fp    = fopen("studentfile.txt", "r");
    if (fp == NULL)
    {
        printf("IO error\n");
        return;
    }

    do
    {
        matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
            &student.id, 
            student.firstname, 
            student.lastname, 
            &student.mark);
        if (matches == 4)
            found = (student.id == id);
    } while ((matches == 4) && (found == 0));
    if (found != 0)
        printStudent(); 
    else
        printf("Not found...\n");
    getchar();
}

int main()
{
    int choice;

    choice = 0;
    while (choice != 4)
    {
        printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM"); 
        printf("\n1 -> Store a new record in database\n");
        printf("2 -> Search a student record by Student First Name\n");
        printf("3 -> Search a student record by ID\n");
        printf("4 -> Quit Student Database");
        printf("\n\n");
        printf("Enter your choice : ");

        scanf("%d",&choice);
        switch(choice)
        {
        case  1:
            storeRecord();
            break;
        case 2:
            searchStudentByName();
            break;
        case 3:
            searchStudentById();
            break;
        }
    }
    return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Student
{
    long long int id;
    char firstname[20];
    char lastname[20];
    int mark;
} student;

void
storeRecord()
{
    FILE *fp;

    printf("\nEnter Student Details:\n\nID number: ");
    scanf("%lld",&student.id);

    printf("\nName:");
    scanf("%19s",student.firstname);

    printf("\nSurname:");
    scanf("%19s",student.lastname);

    printf("\nMark(0 - 100 integer) : ");
    scanf("%d",&student.mark);

    fp = fopen("studentfile.txt","a+"); /* check if the file was opened */
    if (fp == NULL)
        return;
    fprintf(fp, "\n%lld\t%s\t%s\t%d\t", 
        student.id, 
        student.firstname, 
        student.lastname, 
        student.mark);
    fclose(fp);

    printf("A student record has been added successfully...\n");
    getchar();
}

int
compareStudentsById(struct Student lhs, struct Student rhs)
{
    return (lhs.id == rhs.id);
}

int
compareStudentsByName(struct Student lhs, struct Student rhs)
{
    return (strcmp(lhs.firstname, rhs.firstname) == 0);
}

void
printStudent()
{
    printf("\nThe record is found.\n");
    printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",
        student.id,
        student.firstname,
        student.lastname,
        student.mark
    );
}

void
searchStudent(int(*compare)(struct Student,struct Student), const char *const name, int id)
{
    FILE *fp;
    int   found;
    int   matches;


    if (name != NULL)
        printf("Searching record with Name = %s.\n", name);
    if (id != -1)
        printf("Searching record with ID   = %d.\n", id);

    found = 0;
    fp    = fopen("studentfile.txt", "r");
    if (fp == NULL)
    {
        printf("IO error\n");
        return;
    }

    matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
        &student.id, 
        student.firstname, 
        student.lastname, 
        &student.mark);

    do
    {
        struct Student other;

        if (name != NULL)
            strcpy(other.firstname, name);

        other.id = id;
        matches  = fscanf(fp,"\n%lld\t%s\t%s\t%d\t", 
            &student.id, 
            student.firstname, 
            student.lastname, 
            &student.mark);

        if (matches == 4)
            found = (compare(student, other) != 0);

    } while ((matches == 4) && (found == 0));

    if (found != 0)
        printStudent(); 
    else
        printf("Not found...\n");

    getchar();
}

void
searchStudentByName()
{
    char studentname[20];

    printf("\nEnter student first name: ");
    scanf("%19s", studentname);

    searchStudent(compareStudentsByName, studentname, -1);
}

void
searchStudentById()
{
    int id;

    printf("\nEnter student first name: ");
    scanf("%d", &id);

    searchStudent(compareStudentsByName, NULL, id);
}

int main()
{
    int choice;

    choice = 0;
    while (choice != 4)
    {
        printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM"); 
        printf("\n1 -> Store a new record in database\n");
        printf("2 -> Search a student record by Student First Name\n");
        printf("3 -> Search a student record by ID\n");
        printf("4 -> Quit Student Database");
        printf("\n\n");
        printf("Enter your choice : ");

        scanf("%d",&choice);
        switch(choice)
        {
        case  1:
            storeRecord();
            break;
        case 2:
            searchStudentByName();
            break;
        case 3:
            searchStudentById();
            break;
        }
    }
    return 0;
}


Share : facebook icon twitter icon
C++ Basic function program wont compile with a screen worth of errors

C++ Basic function program wont compile with a screen worth of errors


By : necktarios
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , This program is giving me a full screen of errors when i try to compile it with g++ but i didnt even start the hard part. Just printing instructions. What is wrong here? , Here's one problem:
code :
<< endl < endl;
<< endl << endl;
Program wont run to the end after function is called

Program wont run to the end after function is called


By : jamal_ch
Date : March 29 2020, 07:55 AM
around this issue I wrote a program to accept 15 integer values in an array, then pass this array to a function which will multiply each even index value by 4.
code :
for (int i = 0; i < SZ; i + 2)     
c++ program wont exit the loop in a void() function

c++ program wont exit the loop in a void() function


By : ajgbg
Date : March 29 2020, 07:55 AM
hope this fix your issue hi i'm working on an assignment and having trouble with this piece of the program. , This is wrong:
Why Wont my program search in array?

Why Wont my program search in array?


By : Hans Pot
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further My program allows me to add as many items until you hit the max, when you when all it shows you all the information you have entered in. When you go to display one, when I search for the first spot in the array it pops up, but after I search the 4th or 5th item I entered in, it says it hasn't been found, but it's obviously there. Any help. , This part is wrong:
Program wont execute Function c++

Program wont execute Function c++


By : Jay W
Date : March 29 2020, 07:55 AM
This might help you My function wont be called. This function should work like it would ask for the name,username,password and balance of a certain member just like what it does in main. , You have a for loop
code :
for(a=max-1;a==max;a++)
Related Posts Related Posts :
  • Program works but outputs trailing garbage values
  • Letting 2 pointer pointing to same address
  • Different ways to print the two-dimensional array's contents
  • C Programming : Confusion between operator precedence
  • C code inside a loop not being executed
  • C - Weird symbols
  • C - Get pointer adress to string
  • how to start a function using a Struct?
  • Trying to tweak sscanf() to ignore \n and \t
  • How to find the inverse of a Rectangular Matrix in C using GSL
  • sizeof() showing different output
  • How to select/read/copy values after specific character in a string
  • Jump from bootloader generates exception
  • Array dropping values, picks up garbage
  • Swig: Syntax error in input(3)
  • multiple definition and making sure function is correctly written
  • MD4 openssl core dumped
  • Undefined-Behavior at its best, is it -boundary break? -bad pointer arithmetic? Or just -ignore of aliasing?
  • Why am i getting problem3.c:20:23: error: expected expression before ‘int’?
  • Right Justified Zero filled String in C
  • C Function with parameter without type indicator still works?
  • How to transmit data from an interrupt handler to an user application?
  • Why do I get the error "bash: ./a.out: Permission denied" when I execute a C program in Linux mint 15
  • syntax of sigchld and its declaration
  • error using g_idle_add() in C++, same thing works in C
  • why if else or nested if else are called single statement in C
  • How do I interpret this printf in C
  • load the functions of a shell script without executing it
  • Is FilterSendNetBufferLists handler a must for an NDIS filter to use NdisFSendNetBufferLists?
  • How to write to flash memory using inline assembly?
  • More Return Statements vs. More Indentation
  • How to show an image on a PictureBox from resource?
  • Having malloced some memory,I could't calculator the proper size of the memories I malloced.I don't know why
  • What is the main difference between integer pointer and character pointer?
  • Why are some functions declared extern and header file not included in source in Git source code?
  • what is the use of fflush(stdin) in c programming
  • Is it safe to return file File descriptor locally allocated from another function In C
  • Changing undefined values of an array
  • What does an empty parameter list mean?
  • using strtol on a string literal causing segmentation fault
  • Same structure objects memory overlap?
  • C-Linux-Any way to pass command "history" to Linux shell?
  • Using #define in defining string size C
  • How to use thread pool and message queues in Multithreaded Matrix Multiplication?
  • Can't find how to select path to run a C program
  • Automatic variable in C not initialized but given fixed value within loop
  • main() function defined without return type gives warning
  • Output of following code with integer, float, char variable
  • why buffer memory allocation error in opencl
  • Why am I getting this error during run-time?
  • Strange behaviour of the pow function
  • task in increment , decrement , printf() , why these are evaluated in this manner in C
  • 28 extra bytes in bss
  • Waiting for multiple events without polling
  • Why are my variables reporting as "undeclared identifier" when compiling?
  • Correct AddNode function but somehing happens when I printf
  • When I traverse in the splay tree, then now which one is root?
  • Data type conversion in Postfix evaluation
  • No output in terminal (Head First C)
  • Data writes over after realloc
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org