How can I choose an option from a fgets input in C? - c

So I have this code:
printf("S: Search\n");
printf("A: Add\n");
printf("D: Delete\n");
printf("Q: Quit\n");
printf("\nYour choice: ");
while (fgets(line, MAXLINE, stdin) && once == 1)
{
printf("Option: %s\n", line);
once++;
}
And here comes where I'm struggling:
if (line == 's')
{
printf("option s function here");
} else if (line == 'a')
{
printf("option a function here...");
} /* and so on */
This is not working and I've been trying to fix it but nothing I tried worked. How can I write that last piece of code in order to be able to select an option from the fgets input?

Related

Why is my login menu not working here? (first scanf seems to be the problem but i cant fix it)

int login_user()
{
wrong:
printf("-------------Login-------------\n");
int loop,dev;
char usernmcmpr[30],pswdcmpr[4],loginchoice,test[30];
printf("Username:");
scanf(" %s",&usernmcmpr);
printf("Password:");
scanf(" %s", &pswdcmpr);
printf("1. %s\n",username);
printf("2. %s",usernmcmpr);
if(strcmp(password,pswdcmpr) == 0 && strcmp(username,usernmcmpr) == 0)
{
system("cls");
printf("-------------Main Menu-------------\n");
printf("a.Calculate Cost\nb.Payment\nc.Recepit\nd.Exit\n");
scanf(" %c", &loginchoice);
if (loginchoice == 'a')
{
system("cls");
calculate_cost();
}
else if (loginchoice == 'b')
{
system("cls");
payment();
}
else if (loginchoice == 'c')
{
system("cls");
receipt();
}
else if (loginchoice == 'd')
{
keep_Looping = 1;
system("cls");
}
else
{
system("cls");
printf("-------------Error-------------\n");
printf("Invalid input please try again\n");
goto wrong;
clear();
}
}
else
{
goto wrong;
}
return 0;
}
It used to work and then all of a sudden it stopped, don't know why i didn't even change the code in this function nor did i change anything related to it. Really can't tell what is wrong.
usernmcmpr is a char array which is basically a char* under the hood. Since usernmcmpr is already a pointer you want scanf(" %s", usernmcmpr); instead of scanf(" %s",&usernmcmpr);. Same for your other char arrays.
Furthermore, you should be more cautious about buffer overflow problems as currently the user can enter more than your arrays can hold. See How to prevent scanf causing a buffer overflow in C?

Debug assertion failed for editing and deleting records

I am a relatively new programmer currently learning the C language at college. For my project I have been tasked to design a library system for students, I have been experiencing a debug assertion failed-error whenever I try to delete the same book that has been recently edited. Can somebody help me on this question?
PS: I have to submit the program in a few days' time, so I need some help! D:
Here's my code for editing books:
void EditBookInformation()
{
int found = 0;
char user_input[5];
system("cls");
printf("You have selected Edit Book Information \n");
printf("Please enter the Book ID to edit the book: \n");
scanf_s(" %s", &user_input, 5);
fflush(stdin);
fopen_s(&BookList, "record.txt", "rb+");
fopen_s(&BookList2, "newrecord.txt", "wb+");
fopen_s(&Logsheet, "log.txt", "a+");
rewind(BookList);
rewind(BookList2);
while (fread(&Books, sizeof(Books), 1, BookList) != NULL)
{
if (strcmp(user_input, Books.Book_ID) == 0)
{
found = 1;
printf("Book has been found! \n");
printf("\nBook ID:%s \n", Books.Book_ID);
printf("Title:%s \n", Books.Title);
printf("Edition:%s \n", Books.Edition);
printf("Year of publication:%s \n", Books.Year_of_publication);
printf("Shelf location:%s \n", Books.Shelf_location);
printf("Price in RM:%s \n", Books.Price);
break;
}
else
{
fwrite(&Books, sizeof(Books), 1, BookList2);
}
}
if (!found)
{
printf("Book not found! \n");
_fcloseall();
system("pause");
system("cls");
main();
}
char confirm;
printf("\nDo you want to edit this book?");
scanf_s("%c", &confirm);
fflush(stdin);
if (confirm == 'y')
{
printf("New Title:");
scanf_s("%[^\n]s", &Books.Title, 50);
while (getchar() != '\n');
printf("New Edition:");
scanf_s("%s", &Books.Edition, 6);
while (getchar() != '\n');
NewYearInput:
printf("New Year of publication:");
scanf_s("%s", &Books.Year_of_publication, 5);
while (getchar() != '\n');
int length = strlen(Books.Year_of_publication);
int digit = 0;
if (length == 4)
{
for (; digit < length; digit++)
if (!isdigit(Books.Year_of_publication[digit]))
break;
}
if (digit != 4)
{
printf("Wrong input! Please enter 4 digits for year! \n");
system("pause>nul");
goto NewYearInput;
}
printf("New Shelf Location:");
scanf_s("%s", &Books.Shelf_location, 5);
while (getchar() != '\n');
printf("New Price in RM:");
scanf_s("%s", &Books.Price, 5);
while (getchar() != '\n');
fprintf(Logsheet, "Book edited: %s \n", Books.Title);
fseek(BookList, ftell(BookList) -sizeof(Books), SEEK_SET);
fwrite(&Books, 1, sizeof(Books), BookList);
fclose(BookList);
fclose(Logsheet);
printf("Book has been edited! \n");
printf("Press any key to return to main menu \n");
system("pause>nul");
system("cls");
main();
}
else if (confirm == 'n')
{
printf("You have cancelled your operation! \n");
_fcloseall();
system("cls");
main();
}
}
And here is my code for deleting books:
void DeleteBookByBookID()
{
int found = 0;
char user_input1[5];
system("cls");
printf("You have selected Delete Book \n");
printf("Please enter the Book ID that you want to delete the book \n");
scanf_s("%s", &user_input1, 5);
fflush(stdin);
fopen_s(&BookList, "record.txt", "rb+");
fopen_s(&BookList2, "newrecord.txt", "wb+");
fopen_s(&Logsheet, "log.txt", "a+");
rewind(BookList);
rewind(BookList2);
while (fread(&Books, sizeof(Books), 1, BookList) != NULL)
{
if (strcmp(user_input1, Books.Book_ID) == 0)
{
printf("Book found! \n");
found = 1;
}
else
{
fwrite(&Books, sizeof(Books), 1, BookList2);
}
}
if (!found)
{
printf("Book ID not found! \n");
_fcloseall();
system("pause");
system("cls");
main();
}
fclose(BookList);
fclose(BookList2);
char confirm;
printf("Do you want to delete this book? \n");
scanf_s(" %c", &confirm);
fflush(stdin);
if (confirm == 'y')
{
remove("record.txt");
rename("newrecord.txt", "record.txt");
fprintf(Logsheet, "Book deleted: %s \n", Books.Title);
printf("Book has been deleted! \n");
printf("Press any key to return to main menu \n");
system("pause>nul");
system("cls");
main();
}
else if (confirm == 'n')
{
printf("You have cancelled your operation! \n");
fclose(BookList);
fclose(BookList2);
system("cls");
main();
}
fclose(Logsheet);
}
If a book is not found, you are calling main. But main should not be called by the user. It is called by the system and is the start of your program. Maybe you mean return?
You are reading from BookList and if the record read is not the one you want, you write it to BookList2. Once the record is found, you edit it and then write it to BookList2 and then you close the files and rename the new file to the old file. But what happens to all the remaining records in the old file? Shouldn't you copy those too to the new file?
See also the comments to your post for other helpful sugestions.

Implementing bubble sort to File Processing in C

We're learning on File Processing today where we store the scores the user inputted. I got the File Processing part covered but I'm having a hard time arranging the scores in descending order. I'm thinking of using bubble sort but I don't know where to put it since we're still fairly new to the topic of File Processing. Here's my code:
#include<stdio.h>
#define MAX 100
int main(){
FILE *myPtr;
int score=0, choice;
char name[MAX]="";
while (choice != 3) {
printf("What do you want to do?\n");
printf("\t1 - Enter new score\n");
printf("\t2 - View all scores\n");
printf("Choice: ");
scanf("%d", &choice);
switch (choice){
case 1:
printf("Enter name: ");
scanf("%s", name);
printf("Enter score: ");
scanf("%d", &score);
if ((myPtr = fopen("topscores.txt", "a")) == NULL)
printf("File could not be opened. :(\n");
else {
fprintf(myPtr, "Name:\t%s", name);
fprintf(myPtr, "Score:\t%d", score);
fclose(myPtr);
printf("Score added successfully!\n");
system("pause");
}
break;
case 2:
system("cls");
printf("TOP 10 SCORES:\n\n");
if ((myPtr = fopen("topscores.txt", "r") )== NULL)
printf("File could not be opened. :(\n");
else {
printf("NAME\tSCORE\n\n");
fscanf(myPtr, "%s%d", name, &score);
while (!feof(myPtr)){
printf("%s\t%d\n", name, score);
fscanf(myPtr, "%s%d", name, &score);
}
fclose(myPtr);
printf("\n");
system("pause");
}
break;
Do I put the bubble sort in case 2 or case 1? Help, please.

I keep getting an infinite loop when I choose a certain option in my program

i have this program that asks users to enter info stores it in file and allows u to edit entries or add new ones or delete ones by setting gross salary to 0.
However when i try to modify a name , it doesn't modify , and when i try to modify gender, it causes an infinite loop can any1 tell me whats wrong?
And i think there something wrong with my break statements i made within the loop , thanks in advance
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int employee_number;
char employee_name[20];
char employee_sex;
int employee_gross_salary;
}information;
int main()
{//open main function
information customer;
int i;
int choice;
int number;
int choice2;
int number2;
FILE *fptr = fopen("emp.dat", "wb+");
//asking user to enter atleast 5 customers into datarecords
for(i = 0; i<1;i++)
{//open for
printf("enter employee's number\n");
scanf("%d",&customer.employee_number);
getchar();
printf("enter the employee's name\n");
scanf("%s", customer.employee_name);
getchar();
printf("enter employee's gender\n");
scanf("%d",&customer.employee_sex);
getchar();
printf("enter employee's salary\n");
scanf("%d",&customer.employee_gross_salary);
getchar();
fwrite(&customer,sizeof(customer),1,fptr);
}//close for
for(;;)
{//open for
printf("\n what would you like to do\n1]Add entry\n 2]Delete entry \n3]Modify entry\n4]view entries\n5]exit\n");
scanf("%d", &choice);
if(choice == 5)
{break;}
else if(choice == 1)
{//open else if
fseek(fptr,0, SEEK_END);// check the parameters here
printf("enter new employee's number\n");
scanf("%d",&customer.employee_number);
getchar();
printf("enter the new employee's name\n");
scanf("%s", customer.employee_name);
getchar();
printf("enter new employee's gender\n");
scanf("%d",&customer.employee_sex);
getchar();
printf("enter new employee's salary\n");
scanf("%d",&customer.employee_gross_salary);
getchar();
fwrite(&customer,sizeof(customer),1,fptr);
continue;
}//close else if
else if( choice == 2)
{//open else if
printf("enter the employee number of person\n");
scanf("%d",&number);
fseek(fptr,0,SEEK_SET);
while((fread(&customer,sizeof(customer), 1,fptr))!=NULL)
{//open while
if(customer.employee_number == number)
{//open if
customer.employee_gross_salary = 0;
}//close if
}//close while
continue;
}//clsoe else if
else if(choice == 3)
{//open else if
printf("enter the employee number of the employee you would like to modify\n");
scanf("%d",&number2);
printf("what would you like to modify\n");
scanf("%d", &choice2);
fseek(fptr,0,SEEK_SET);
while((fread(&customer, sizeof(customer),1,fptr))!= NULL)
{//open while within else if
//1 to midify name, 2 to modify gender 3 for salary
if(customer.employee_number == number2)
{//open if
if(choice2 == 1)
{
printf("enter new name\n");
scanf("%s",customer.employee_name );
break;
}
else if(choice2 == 2)
{
printf("enter new gender");
scanf("%d", &customer.employee_sex);
break;
}
else if(choice2 == 3)
{
printf("enter new gross salary\n");
scanf("%d", &customer.employee_gross_salary);
break;
}
}//close if
}//close while within else if
continue;
}//close else if
else if(choice == 4)
{
fseek(fptr,0,SEEK_SET);
while((fread(&customer,sizeof(customer),1,fptr))!= NULL)
printf("\n%d\t%s\t%c\t%d\n", customer.employee_number,customer.employee_name,customer.employee_sex,customer.employee_gro ss_salary);
continue;
}
}//close for
return 0;
}//close main function
This is NOT the answer for your debug, just some advice for refactoring your code and future writing:
1.
Avoid using break and continue, they are flow breaker, bug sources, bad and evil, it is the same for go to, they are here for specific no-other-way cases.
You can do something like:
int end = 0,
choice = 0;
do
{
fprintf(stdout, "1:Do stuff\n2:Do other stuff\n3: Do another stuff\nX: end\n");
while(fscanf(stdin, "%d", &choice) != 1){}
if(choice == 1)
{
//Do stuff
}
else if (choice == 2)
{
//Do other stuff
}
else if (choice == 3)
{
//Do another stuff
}
else
{
end = 1;
}
}while(end == 0);
return 0;
No continue, no break, easier to modify, easier to write, easier to read, shorter, in two word: way better
2.
Write in english, always, you have a full keyboard and do not pay by letter, it is almost as fast to type the entire word, and help a lot other to understand.
Also, it will help you to make less error when you write text or code.
3.
You can declare multiple variable at once if they are of the same type:
int var1;
int var2;
int var3;
...
Is long and repetitive, instead you can write:
int var1,
var2,
var3;
A good habit can be to always initialize variables, it help prevent some bug:
int var1=0,
var2=0,
var3=0;
4.
Whenever you use a function, test its return, there is a lot of bug that happen from thinking "it is a stdio function, it is bug-proof". As exemple, your fopen of emp.dat. It can fail (and in fact will fail at some point).
FILE *fptr = fopen("emp.dat", "wb+");
if (fptr == NULL)
{
fprintf(stderr, "Error while opening emp.dat\n");
return -1;
}
5.
If you are a begginner (there is no shame about that, everyone begin at some point, and we can say everyone is still begginning even after 10+years of coding), write your algorithm first, then code. Exemple:
//Get user's choice
//If user choice is do stuff
//Do stuff
//If it is do other stuff
//Do other stuff
//If it is do another stuff
//Do another stuff
//Else if he want to quit
//Quit
Which then become
int choice=0, //User's choice
end=0; //End of program
do
{
//Get user's choice
fprintf(stdout, "1:Do stuff\n2:Do other stuff\n3: Do another stuff\nX: end\n");
while(fscanf(stdin, "%d", &choice) != 1){}
//If user choice is do stuff
if(choice == 1)
{
//Do stuff
}
//If it is do other stuff
else if(choice == 1)
{
//Do other stuff
}
//If it is do another stuff
else if(choice == 1)
{
//Do another stuff
}
//Else if he want to quit
else
{
//Quit
end = 1;
}
}while (end == 0);
return 0;
It also prevent you from going to comment your code weeks later when you don t know anymore why you did that or that stuff.
6.
Log, log, log, especially at debug!
You can put it on stderr if you want, so you can separate it from your output.
Exemple:
int end = 0,
choice = 0;
fprintf(stderr, "Start\n");
do
{
fprintf(stderr, "\tBegin loop\n");
fprintf(stdout, "1:Do stuff\n2:Do other stuff\n3: Do another stuff\nX: end\n");
while(fscanf(stdin, "%d", &choice) != 1){}
fprintf(stderr, "\tChoice is: %d\n", choice);
if(choice == 1)
{
fprintf(stderr, "\t\tStarting do stuff\n");
//Do stuff
fprintf(stderr, "\t\tEnding do stuff\n");
}
else if (choice == 2)
{
fprintf(stderr, "\t\tStarting do other stuff\n");
//Do other stuff
fprintf(stderr, "\t\tEnding do other stuff\n");
}
else if (choice == 3)
{
fprintf(stderr, "\t\tStarting do another stuff\n");
//Do another stuff
fprintf(stderr, "\t\tEnding do another stuff\n");
}
else
{
fprintf(stderr, "\t\tEnd order\n");
end = 1;
}
fprintf(stderr, "\tEnd of loop\n");
}while(end == 0);
fprintf(stderr, "End\n");
return 0;
So you know when and where is your program right now, it is a HUGE help for debug!
That s all I have in mind right now, hope it can help you.
Also, welcome to Stack Overflow.
Edit:
Thanks to chunk, another important point:
7.Always checking scanf for valid user's input. User's input can and will be almost eveything, and will at some point not be what you think, test it, always. (it is not valid only for (f)scanf, but for EVERY ways you get your data from other sources but your own source code)
int check = 0;
fprintf(stderr, "\tBegin loop\n");
fprintf(stdout, "1:Do stuff\n2:Do other stuff\n3: Do another stuff\nX: end\n");
check = fscanf(stdin, "%d", &choice);
if(check != 1)
{
fprintf(stderr, "Bad input\n");
return -1;
}
fprintf(stderr, "\tValid choice is: %d\n", choice);
This way, any other input but a decimal number will be discarded and will close the program, of course you can do it better.
int check = 0;
fprintf(stderr, "\tBegin loop\n");
fprintf(stdout, "1:Do stuff\n2:Do other stuff\n3: Do another stuff\nX: end\n");
while(fscanf(stdin, "%d", &choice) != 1)
{
fprintf(stderr, "Bad input!\n");
}
fprintf(stderr, "\tValid choice is: %d\n", choice);
In this version, when the user type something invalid, he just have to try again.
In addition to DrakaSAN's answer I would add that you should always flush the input buffer when you take character/string input after taking integer input.
One way to flush the input buffer is to use getchar():
while ((ch = getchar()) != '\n');
But if the user gives the input as "123 abc\n" (as mentioned by chux in comment) assuming 123 goes to the integer variable and "abc" to the character array, then there are ways to resolve this:
//can be modified according to programmer's requirements
int a;
char arr[10],ch;
scanf("%d",&a);
while((ch=getchar())==' ' || ch=='\t' || ch=='\n') //loop until non-whitespace character
{
if (ch=='\n')
{
ch=getchar();
break;
}
}
if (ch!='\n') //ch contains the first character of the character array
{
arr[0]=ch;
gets(arr+1);
}
else //if two consecutive new lines after integer, string contains nothing
arr[0]='\0';
You need to write the results when choice == 2 and choice == 3 before the continue, like
fseek(fptr, -sizeof(customer), SEEK_CUR);
fwrite(&customer,sizeof(customer), 1, fptr);

Search Method Failing

I'm trying to do a search Method in which checks 2 IDs which only store integer.
Firstly I have a database of customers. I input the Name,Surname,ID and Address after each other in this order and then these are immediately saved into the file
When the User inputs the ID card this calls this Search Method and checks through all the file to see whether the ID is unique or not. If it is not, then it returns 0 otherwise it returns 1
Now the problem is this.
When I am inputting the ID, whether it is unique or not it keeps on going, but then when it outputs what I wrote it, for the NAME and SURNAME it shows only the first record I have stored there (like stuck in some kind of buffer), the ID and the address outputs normally.
The File is Also NOT Updated meaning, the save file did not occur.
Now when I Remove this method, the appending works normally, but I won't have access to the comparing of IDs.
Any suggestions why this is happening? and if possible any idea how I can fix it?
it's like whenever I'm doing this search method the whole file is getting started from the beginning and getting stuck there. I Tried using the method with boolean but still no avail. When I tried using it with Boolean instead of the line "if (customerID(scanf ("%d",&cCheck)) == 1)" I made it == TRUE, it was giving me an error that the output will
always be == FALSE because the data is not NULL.
and oh TRUE and FALSE are valid in my case as I have a typedef enum boolean in the common.h
The code is as follows [posting the whole file]:
The methods concerned are [ void addCustomer()] and [int customerID (int cCheck)
but I posted all as some of them are interconnecting.
EDIT!!! - The ID even if they are NOT unique, it is still Accepted...
/*
* CustomerMainMenu.c
* Author: DodoSerebro
*
* This class will output the Customer's Main Menu and re-directs to the
* corresponding section
*
*/
#include<io.h>
#include<fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "..\Headers\common.h"
#include "..\Headers\customerManagement.h"
static FILE *cfp;
static customer c;
#define STRUCTSIZE sizeof (customer)
/** This is the Customers's Main Menu in which the various sections can be
* accessed from here
*/
boolean customerMainMenu()
{
int optionC;
clrscr();
copyright();
printf ("\n\n\n\n\t\t ************* Customer's Main Menu *************\n \n \n");
printf ("Press [1] to add a new Customer\n");
printf ("Press [2] to edit a Customer\n");
printf ("Press [3] to list all Customers\n");
printf ("Press [4] to Show a Customer's last Order\n");
printf ("Press [5] to go back to Main Menu\n\n\n");
if (scanf ("%d",&optionC) == 1)
{
switch (optionC)
{
case 1:
{
clrscr();
getchar();
addCustomer();
break;
}
case 2:
{
printf ("Edit a Customer\n");
break;
}
case 3:
{
clrscr();
listCustomers();
system ("PAUSE");
break;
}
case 4:
{
printf ("Customer's Last Order\n");
break;
}
case 5:
{
system ("PAUSE");
break;
}
default:
{
if (optionC != 1 || optionC != 2 || optionC != 3 || optionC != 4 || optionC !=5)
{
clrscr();
printf ("Invalid option!\n");
system ("PAUSE");
customerMainMenu();
}
break;
}
}
}
return TRUE;
}
/**
* This following method will append a customer to the
* database at the end of the file
*
* */
void addCustomer ()
{
char ch;
copyright();
printf ("\n\n\n\n\t\t ************* Add Client **********\n \n \n");
if ((cfp = fopen ("customers.dat","a+b")) == NULL)
{
fputs("Can't open customers.dat file\n",stderr);
}
printf ("\tThis will add another customer to the the database\n");
printf ("\tPress 'Y' to confirm or 'N' to return to the Client Main Menu\n\tWITHOUT adding a customer\n");
ch = getchar();
if (ch == 'n' || ch == 'N')
{
customerMainMenu();
}
else if (ch == 'y' || ch == 'Y')
{
fflush(stdin);
clrscr();
printf ("\n\n\n\n\t\t ************* Add Client **********\n \n \n");
printf ("Please enter Name:\n");
while (scanf ("%s", c.name) == 0 || cCheck(c.name,100) == FALSE);
{
}
printf ("Please Enter Surname: \n");
while (scanf ("%s",c.surname) == 0 && cCheck (c.surname,100) == FALSE);
{
}
printf ("Please Enter ID Card, [NOTE! Only numbers are allowed!]\n");
int cCheck;
if (customerID(scanf ("%d",&cCheck)) == 1)
{
printf ("ID already Taken, Client exists!\n");
printf ("Do you want to enter another ID? 'Y' for Yes and 'N' to return to Main Menu\n");
ch = getchar();
if (ch == 'Y' || ch == 'y')
{
scanf ("%d",&cCheck);
customerID(cCheck);
c.ID = cCheck;
}
else
{
customerMainMenu();
}
}
else
{
c.ID = cCheck;
}
getchar();
printf ("Please Enter Address:\n");
gets(c.address);
fwrite (&c,STRUCTSIZE, 1, cfp);
printf ("For Testing purposes:\n");
printf (" %s\n %s\n %s\n %d\n", c.name, c.surname, c.address, c.ID);
askAnother();
}
else
{
printf ("\nInvalid choice! Either Y or N is accepted\n");
system ("PAUSE");
getchar();
clrscr();
addCustomer();
}
}
void listCustomers()
{
if ((cfp = fopen ("customers.dat","rb")) == NULL)
{
fputs("Can't open customers.dat file\n",stderr);
printf ("Returning to Customer Main Menu");
system ("PAUSE");
customerMainMenu();
}
rewind (cfp);
while (fread (&c,STRUCTSIZE,1,cfp)==1)
{
printf ("Customer: %s %s ID: %d\n", c.surname, c.name, c.ID);
}
fclose (cfp);
}
void askAnother()
{
printf ("Do you want to add another Customer?\n");
printf ("Enter 'Y' for yes and 'N' to return to the Main Menu\n");
char input;
input = getchar();
if (input == 'Y' || input == 'y')
{
getchar();
addCustomer();
}
else if (input == 'N'|| input == 'n')
{
fclose (cfp);
customerMainMenu();
}
else
{
printf ("Invalid Option! Only Y or N are allowed\n");
system ("PAUSE");
clrscr();
askAnother();
}
}
boolean cCheck(char *test, int max)
{
int x;
for (x =0; x<max; x++)
{
if (isdigit(test[x]))
{
return FALSE;
}
if (x==max)
{
return TRUE;
}
x++;
}
return TRUE;
}
/**
* This method will compare the ID passed from the ID of the customer to check
* whether it is exists or not. If it exists it will output 1 otherwise it
* will output -1. This will make sure that the Person's ID is unique
*
*/
int customerID (int cCheck)
{
rewind (cfp);
while (fread (&c,STRUCTSIZE,1,cfp)==1)
{
if (c.ID == cCheck)
{
return 1;
}
else
{
return 0;
}
}
return 1;
}
EDIT!!
Uploading Image to show what I mean if I'm not clear
(Notice how the name and surname differ from those Input)
!http://s017.radikal.ru/i443/1212/c8/1ea9bc56d980.jpg
The following shows what I have in the file
(only one file)
!http://s017.radikal.ru/i431/1212/49/2a0df6acf9ec.jpg
if (customerID(scanf ("%d",&cCheck)) == 1)
Here, you are the return value of scanf() to function customerID() whereas you actually wanted to pass the scanned value cCheck.
You can separate the call to function and input reading as below:
if(scanf ("%d",&cCheck) != 1) {
// Input error handling
}
else if (customerID(cCheck) == 1) {
...
Another issue is, you have a function named cCheck and you also have a local variable named cCheck. Rename one of them appropriately.

Resources