logo
down
shadow

C database program can't find 1st input. Why?


C database program can't find 1st input. Why?

By : Sami Almalfouh
Date : November 22 2020, 10:38 AM
This might help you Just remove the first fscanf the one outside the while loop, its scanning the first line of the file.
A copy of your code fixed
code :
#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;
    }

    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
can we use scanner input for inserting values into database in jdbc program

can we use scanner input for inserting values into database in jdbc program


By : Mayank
Date : March 29 2020, 07:55 AM
To fix this issue I have a code like this, , Yes, it is possible. It should look something like
code :
String sql = "insert into tbl1(id,catagory) values(?, ?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, a);
ps.setString(2, b);
ps.executeUpdate();
String sql="insert into tbl1(id,catagory) values('" + a + "','" + b + "')";
st.executeUpdate(sql);
Can't find the bug in the program to print the longest input line

Can't find the bug in the program to print the longest input line


By : user3479619
Date : March 29 2020, 07:55 AM
hop of those help? This is a bit simpler and does the job by using the available string functions, instead of examining char by char. If you need to, you can open a file for input and replace stdin.
code :
#include <stdio.h>
#include <string.h>

#define MAXLINE 1000

int main(){
    int len;    // length of the line
    int max;    // maximum length seen so far
    char line[MAXLINE];
    char longest[MAXLINE];
    char *sptr;
    max = 0;
    while((sptr = fgets(line, MAXLINE, stdin)) != NULL) {
        if ((sptr = strtok(sptr, "\r\n")) != NULL) {
            len = strlen(sptr);
            if (len > max) {
                max = len;
                strcpy(longest, sptr);
            }
        }
    }
    if (max)
        printf("%d: %s\n", max, longest);
    else 
        printf("No strings read\n");
    return 0;
}
I wrote this program to find if a given input is power of 2 , this program is not running for very large number such as

I wrote this program to find if a given input is power of 2 , this program is not running for very large number such as


By : SOS
Date : March 29 2020, 07:55 AM
help you fix your problem The problem is that 1018 is out of range of Java int, which stores numbers up to 231-1, or roughly 2*109. You can expand the range of your program by using long in place of int to accept numbers up to 9*1018, or to make it accept virtually unlimited range by using BigInteger:
code :
BigInteger n = new BigInteger(numericString);
BigInteger test = n.and(n.subtract(BigInteger.ONE));
if (test.equals(BigInteger.ZERO)) {
    ...
}
How to find some files and input them to another program in bash?

How to find some files and input them to another program in bash?


By : Ramzi
Date : March 29 2020, 07:55 AM
help you fix your problem If they're all in the current directory you can just call the program with the pattern. The shell will expand it and call the program with the matching file names.
code :
program -input *A*.b
find . -name '*A*.b' -exec program -input {} +
Saving input data from a program without a database

Saving input data from a program without a database


By : Blair Johnston
Date : October 13 2020, 01:00 AM
should help you out You can use:
Deserialization Use a local db Use a local file format (csv, json, xml) Save an encrypted string
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