Reputation: 33
I'm creating a C program as my last project for this year in my school and they wanted to let me create a student database program. Here it is, works almost flawless but it won't register the ID numbers properly.
https://i.sstatic.net/Ht4Y8.png
On this image you can clearly see I've entered 23915741843 as the ID number but it registered itself as; -1854061933.
I've used long to fix it, but nope. It won't really work. I'm allowed to take help from the internet. So here is my full code. What's wrong with it?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
{
long int id;
char firstname[20];
char lastname[20];
int mark;
}student;
int main()
{
long int idnumber;
int flag,choice,shift,found,continu,length;
char studentname[20];
FILE *fp;
printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM");
Label1:
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:
Label2:
printf("\nEnter Student Details:\n\nID number: ");
scanf("%d",&student.id);
printf("\nName:");
scanf("%s",student.firstname);
printf("\nSurname:");
scanf("%s",student.lastname);
printf("\nMark(0 - 100 integer) : ");
scanf("%d",&student.mark);
fp=fopen("studentfile.txt","a+");
fprintf(fp,"\n%d\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");
printf("\n\n1 -> Wish to add another record to database");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label2;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("Exiting.........");
break;
}
case 2:
Label4:
printf("\nEnter student first name: ");
scanf("%s",&studentname);
printf("Searching record with studentname=%s.\n",studentname);
found=0;
if((fp=fopen("studentfile.txt","r"))==NULL)
{
printf(" ! The File is Empty...\n\n");
}
else
{
while(!feof(fp)&& found==0)
{
fscanf(fp,"\n%d\t%s\t%s\t%d\t",&student.id,student.firstname,student.lastname,&student.mark);
length = strlen(student.firstname);
if(student.firstname[length]==studentname[length])
found=1;
}
}
if(found)
{
printf("\nThe record is found.\n");
printf("\nID: %d\nName: %s\nSurname: %s\nMark: %d \n",student.id,student.firstname,student.lastname,student.mark);
}
else
{
printf("Not found...\n");
getch();
}
Label5:
printf("\n\n1 -> Wish to search another record");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label4;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("\nEnter a valid choice");
goto Label5;
}
case 3:
Label6:
printf("\nEnter the ID: ");
scanf("%d",&idnumber);
printf("Searching record with ID=%d.\n",idnumber);
found=0;
if((fp=fopen("studentfile.txt","r"))==NULL)
{
printf(" ! The File is Empty...\n\n");
}
else
{
while(!feof(fp)&& found==0)
{
fscanf(fp,"\n%d\t%s\t%s\t%d\t",&student.id,student.firstname,student.lastname,&student.mark);
if(student.id==idnumber)
found=1;
}
}
if(found)
{
printf("\nThe record is found.");
printf("\nID no: %d\nName: %s\nSurname: %s\nMark: %d \n",student.id,student.firstname,student.lastname,student.mark);
}
else
{
printf("Not found...\n");
getch();
}
Label7:
printf("\n\n1 -> Wish to search more..");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label6;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("\nEnter a valid choice");
goto Label7;
}
case 4: break;
default :
printf(" Bad choice...Enter the choice again...\n");
goto Label1;
}
getch();
return 0;
}
Upvotes: 2
Views: 190
Reputation: 134356
TL;DR --> Please use appropriate format specifiers and always check for the limit of the value that can be held by the used data type.
In your code,
scanf("%d",&student.id);
%d
is not the correct format specifier for long int
.
long int
it should be %ld
unsigned long int
it should be %lu
Also, 23915741843
is a value too big to be held by long int
. From §5.2.4.2.1
of c99
maximum value for an object of type long int
LONG_MAX +2147483647 // 231 - 1
You may use long long int
[%lld
].
Sidenote: You never checked for the success of fopen()
, scanf()
etc.
Upvotes: 2
Reputation: 19874
scanf("%ld",&student.id);
Use proper format specifier to scan long int. IMO id
will be an unsigned value so if that is a mistake make id
as unsinged long int id
and use %lu
to scan the value . Please make sure that the value fits the allowed range if not then use long long int
and scan it accordingly using %lld
format specifier.
Your code looks obfuscate with using goto
make sure you overcome it.
Display your prompt and have switch cases handling it accordingly. If the user wants to exit have a seperate case for it and handle this case instead of using goto
Upvotes: 0