Program crashing near the end of compiling - c

I've got a program for taking input and storing it into structs. The structs are for contact details, Name, Address, and Phone numbers. My program works just fine, I can enter all my information into the program however when I try to printf the results the program crashes halfway through. I think they may be an issue with memory or corruption, or something. The reason I think it may be with corruption is because if I cut off some of my program and compile it, instead of the program crashing I get a 'Run-Time check failure #2 Stack around the variable 'optionAddress' was corrupted' error. Here's my program
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
int main(void)
{
// Declare variables here:
struct Name names;
char optionName;
struct Address addresses;
char optionAddress;
struct Numbers number;
char optionCell;
char optionHome;
char optionBusiness;
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Contact Name Input:
printf("Please enter the contact's first name: ");
scanf("%s", &names.firstName);
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%s", &optionName);
while (optionName == 'y' || optionName == 'Y') {
printf("Please enter the contact's middle initial(s): ");
scanf("%s", &names.middleInitial);
break;
}
printf("Please enter the contact's last name: ");
scanf("%s", &names.lastName);
// Contact Address Input:
printf("Please enter the contact's street number: ");
scanf("%s", &addresses.streetNumber);
printf("Please enter the contact's street name: ");
scanf("%s", &addresses.street);
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%s", &optionAddress);
while (optionAddress == 'y' || optionAddress == 'Y') {
printf("Please enter the contact's apartment number: ");
scanf(" %c", &addresses.apartmentNumber);
break;
}
printf("Please enter the contact's postal code: ");
scanf("%s", &addresses.postalCode);
printf("Please enter the contact's city: ");
scanf("%s", &addresses.city);
// Contact Numbers Input:
printf("Do you want to enter a cell phone number? (y or no): ");
scanf("%s", &optionCell);
while (optionCell == 'y' || optionCell == 'Y') {
printf("Please enter the contact's cell phone number: ");
scanf(" %c", number.cell);
break;
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf("%s", &optionHome);
while (optionHome == 'y' || optionHome == 'Y') {
printf("Please enter the contact's home phone number: ");
scanf(" %c", &number.home);
break;
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf("%s", &optionBusiness);
while (optionBusiness == 'y' || optionBusiness == 'Y') {
printf("Please enter the contact's business phone number: ");
scanf(" %c", number.business);
break;
}
// Display Contact Summary Details
printf("Contact Details\n");
printf("---------------\n");
printf("Name Details\n");
printf("First name: ");
printf("%s", names.firstName);
printf("\nMiddle initials(s): ");
printf("%s", names.middleInitial);
printf("\nLast name: ");
printf("%s", names.lastName);
printf("\n\nAddress Details\n");
printf("Street number: ");
printf("%s", addresses.streetNumber);
printf("\nStreet name: ");
printf("%s", addresses.street);
printf("\nApartment: ");
printf("%s", addresses.apartmentNumber);
printf("\nPostal code: ");
printf("%s", addresses.postalCode);
printf("\nCity: ");
printf("%s", addresses.city);
printf("\n\nPhone Numbers: ");
printf("\nCell phone number: ");
printf("%s", number.cell);
printf("\nHome phone number: ");
printf("%s", number.home);
printf("\nBusiness phone number: ");
printf("%s", number.business);
// Display Completion Message
printf("\n\nStructure test for Name, Address, and Numbers Done!");
return 0;
}
And the structs in the header file:
// Structure type Name declaration
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
// Structure type Address declaration
// Place your code here...
struct Address {
char streetNumber;
char street[41];
char apartmentNumber;
char postalCode[8];
char city[41];
};
// Structure type Numbers declaration
// Place your code here...
struct Numbers {
char cell[21];
char home[21];
char business[21];
};
My program reaches the point of printing out "Street number: " then stops working. It is a windows error window that shows up, not a compiler window.

My program reaches the point of printing out "Street number: " then
stops working.
Take a look at:
struct Address {
char streetNumber; // declared as character!
char street[41];
char apartmentNumber; // character
char postalCode[8];
char city[41];
};
char optionName;
char optionAddress;
char optionCell;
char optionHome;
char optionBusiness;
Later on you try to read the values to the characters:
scanf("%s", &optionName);
scanf("%s", &optionAddress);
scanf("%s", &optionCell);
scanf("%s", &optionHome);
scanf("%s", &optionBusiness);
scanf("%s", &addresses.streetNumber); // <-------------- string read
using string format %s. That invokes UB since you are reading at least 2 bytes. "y" is a string with string null terminator '\0'. That extra byte overwrites the memory location.
Change declarations of variables to strings or reading format to character read as you already do here:
scanf(" %c", &addresses.apartmentNumber);

scanf("%s", &optionName);
This invites undefined behavior which could lead to run time error you are getting as optionName is char one byte and scanf() with %s will try to write '\0' which will surely go beyond bounds

Related

Integers don't show when written into .dat file in c

I am trying to write some records (patient info) into a .dat file using c. The patient info contains the National ID no., name, gender and the telephone no.
Here's my code:
#include<stdio.h>
int main(void)
{
char order = 'y';
int nic = 0; // NIC no.
char name[] = "xxx"; // name
char gender = 'x'; // Patients' gender
int tel_no = 0; // telephone no.
FILE *fp;
fp = fopen("patientdetails.dat", "w"); // Creat patientdetails file
if (fp == NULL)
{
printf("Cannot open file\n");
return -1;
}
else
{
printf("Do you want to enter a record (y/n): ");
scanf("%c", &order);
while (order == 'y' || order == 'Y')
{
printf("Enter nic no.: ");
scanf("%d", &nic);
printf("Enter name: ");
scanf("%s", name);
printf("Enter gender (m/f): ");
scanf(" %c", &gender);
printf("Enter tel no.: ");
scanf("%d", &tel_no);
fprintf(fp, "%d %s %c %d\n", nic, name, gender, tel_no);
printf("Do you want to enter a record (y/n): ");
scanf(" %c", &order);
}
fclose(fp);
}
return 0;
}
However when I enter the following info into my .dat file in the terminal:
Do you want to enter a record (y/n): y
Enter nic no.: 200007102766
Enter name: Timothy
Enter gender (m/f): m
Enter tel no.: 0779428897
Do you want to enter a record (y/n): n
There is an error in the integers, namely nic no. and telephone no.
Additionally I made another program to read the file, the error in the integers still seem to show up.
Errors in the .dat file:
7956596 Timothy m 779428897
I can't seem to figure out how to correct the integer errors,
where 20007102766 is shown as 7956596
and 0779428897 is shown as 779428897.
Any help is appreciated!
Your variable byte size is larger than a int, so you should use bigger variable type.
In your example, you can use long type for nic and tel_no veriables.
Also you should use %ld in format, otherwise it gives you warning.
char order = 'y';
long nic = 0; // NIC no.
char name[] = "xxx"; // name
char gender = 'x'; // Patients' gender
long tel_no = 0; // telephone no.
And while formating
printf("Enter nic no.: ");
scanf("%ld", &nic);
printf("Enter tel no.: ");
scanf("%ld", &tel_no);
fprintf(fp, "%ld %s %c %ld\n", nic, name, gender, tel_no);

Pointers and/or functions not working correctly

Attempting to run this code will display blank inputs for most of the fields and will be mixed up, for example the street number would be in place of first name. Not sure what's going on. Was hesitant about using stackoverflow at first but now it seems I have no choice
header file:
// Structure type Name declaration
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
// Structure type Address declaration
struct Address {
int streetNumber;
char street[41];
int apartmentNumber;
char postalCode[8];
char city[41];
};
// Structure type Numbers declaration
struct Numbers {
char cell[11];
char home[11];
char business[11];
};
// Structure type Contact declaration
struct Contact {
struct Name name;
struct Address address;
struct Numbers numbers;
};
//------------------------------------------------------
// Function Prototypes
//------------------------------------------------------
// Get and store from standard input the values for Name
void getName(struct Name* name);
// Get and store from standard input the values for Address
void getAddress(struct Address* address);
// Get and store from standard input the values for Numbers
void getNumbers(struct Numbers* numbers);
input file
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
//This function will clear the input buffer after every input
void clear() {
while (getchar() != '\n');
}
// Get and store from standard input the values for Name
void getName(struct Name* name) {
char option = 0;
printf("Please enter the contact's first name: ");
scanf("%31s", name[0].firstName);
clear();
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's middle initial(s): ");
scanf("%7[^\n]s", name[0].middleInitial);
clear();
}
printf("Please enter the contact's last name: ");
scanf("%36[^\n]s", name[0].lastName);
clear();
}
// Get and store from standard input the values for Address
void getAddress(struct Address* address) {
char option = 0;
printf("Please enter the contact's street number: ");
scanf("%d", &address[0].streetNumber);
clear();
printf("Please enter the contact's street name: ");
scanf("%40[^\n]s", address[0].street);
clear();
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's apartment number: ");
scanf("%d", &address[0].apartmentNumber);
clear();
}
printf("Please enter the contact's postal code: ");
scanf("%7[^\n]s", address[0].postalCode);
clear();
printf("Please enter the contact's city: ");
scanf("%40[^\n]s", address[0].city);
clear();
}
// Get and store from standard input the values for Numbers
void getNumbers(struct Numbers* numbers) {
char option = 0;
printf("Do you want to enter a cell phone number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's cell phone number: ");
scanf("%11s", numbers[0].cell);
clear();
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's home phone number: ");
scanf("%11s", numbers[0].home);
clear();
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf("%c", &option);
clear();
if (option == 'y' || option == 'Y') {
printf("Please enter the contact's business phone number: ");
scanf("%11s", numbers[0].business);
clear();
}
}
my main program:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
int main(void)
{
// Declare variables:
struct Contact contact[] = {{0}};
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Call the Contact function getName to store the values for the Name member
getName(contact);
// Call the Contact function getAddress to store the values for the Address member
getAddress(contact);
// Call the Contact function getNumbers to store the values for the Numbers member
getNumbers(contact);
// Display Contact summary details
printf("\n");
printf("Contact Details\n");
printf("===============\n");
printf("Name Details\n");
printf("------------\n");
printf("First name: %s\n", contact[0].name.firstName);
printf("Middle initial(s): %s\n", contact[0].name.middleInitial);
printf("Last name: %s\n", contact[0].name.lastName);
printf("\n");
printf("Address Details\n");
printf("--------------\n");
printf("Street number: %d\n", contact[0].address.streetNumber);
printf("Street name: %s\n", contact[0].address.street);
printf("Apartment: %d\n", contact[0].address.apartmentNumber);
printf("Postal code: %s\n", contact[0].address.postalCode);
printf("City: %s\n", contact[0].address.city);
printf("\n");
printf("Phone Numbers\n");
printf("-------------\n");
printf("Cell phone number: %s\n", contact[0].numbers.cell);
printf("Home phone number: %s\n", contact[0].numbers.home);
printf("Business phone number: %s\n", contact[0].numbers.business);
printf("\n");
// Display Completion Message
printf("Structure test for Contact using functions done!\n");
return 0;
}
any help would be greatly appreciated, thanks
you have an error in main when calling your functions
getName(contact);
getAddress(contact);
getNumbers(contact);
your passing a contact struct while they wait for other structure types.
void getName(struct Name* name);
void getAddress(struct Address* address);
void getNumbers(struct Numbers* numbers);
try to correct this with
getName(&contact->name);
getAddress(&contact->address);
getNumbers(&contact->numbers);

C Programming: How to prevent printf statement from printing out new line values?

My code works fine, except for one line which keeps printing out the data that supposed to be for the next line only.
The output is supposed to look something like this:
Address Details
Street number: 100
Street name: Bedrock
Apartment: 14
Postal code: Z8Z 7R7
City: Markham
Instead my output is this:
Address Details
Street number: 100
Street name: Bedrock
Apartment: 14
Postal code: Z8Z 7R7Markham
City: Markham
As you can see Markham is printed along the same line of postal code.
Below are the potential files which might have caused this error. All help is much appreciated!
a1ms4.c file:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
// to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:
#include "contacts.h"
int main(void)
{
// Declare variables here:
// Create a variable of type Contact and call it something self-describing like "contact"
// - HINT: Be sure to initialize the values to 0 and empty C strings
struct Contact contact = {
{ "", "", "" },
{0," ", 0, " ", " "},
{ "", "", "" }
};
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Call the Contact function getName to store the values for the Name member
getName(&contact.name);
// Call the Contact function getAddress to store the values for the Address member
getAddress(&contact.address);
// Call the Contact function getNumbers to store the values for the Numbers member
getNumbers(&contact.numbers);
// Display Contact summary details
printf("\nContact Details\n");
printf("---------------\n");
printf("Name Details\n");
printf("First name: %s", contact.name.firstName);
printf("\n");
printf("Middle initial(s): %s", contact.name.middleInitial);
printf("\n");
printf("Last name: %s", contact.name.lastName);
printf("\n");
printf("\n");
printf("Address Details\n");
printf("Street number: %d", contact.address.streetNumber);
printf("\n");
printf("Street name: %s", contact.address.street);
printf("\n");
printf("Apartment: %d", contact.address.apartmentNumber);
printf("\n");
printf("Postal code: %s", contact.address.postalCode);
printf("\n");
printf("City: %s", contact.address.city);
printf("\n");
printf("\n");
printf("Phone Numbers:");
printf("\n");
printf("Cell phone number: %s", contact.numbers.cell);
printf("\n");
printf("Home phone number: %s", contact.numbers.home);
printf("\n");
printf("Business phone number: %s", contact.numbers.business);
printf("\n");
printf("\n");
// Display Completion Message
printf("Structure test for Contact using functions done!\n");
return 0;
}
contacts.c file
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
// to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:
#include "contacts.h"
// Get and store from standard input the values for Name
// Put your code here that defines the Contact getName function:
void getName(struct Name * name) {
//Variable/Struct declaration:
char notify;
// Contact Name Input:
printf("Please enter the contact's first name: ");
scanf("%30s", name->firstName);
//Prompts the user to see if they want to enter a middle name using y, yes or n, no.
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%s", &notify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's middle initial(s): ");
scanf("%6s", name->middleInitial);
}
printf("Please enter the contact's last name: ");
scanf("%30s", name->lastName);
}
// Get and store from standard input the values for Address
// Put your code here that defines the Contact getAddress function:
void getAddress(struct Address * address) {
//Variable/Structure declaration:
char notify;
// Contact Address Input:
printf("Please enter the contact's street number: ");
scanf("%d",& (address->streetNumber));
printf("Please enter the contact's street name: ");
scanf("%40s", address->street);
//Prompts the user to see if they want to enter an apartment number using yes, or no.
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%s", &notify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's apartment number: ");
scanf("%d",& (address->apartmentNumber));
}
printf("Please enter the contact's postal code: ");
scanf(" %[^\n]", address->postalCode);
printf("Please enter the contact's city: ");
scanf(" %40s", address->city);
}
// Get and store from standard input the values for Numbers
// Put your code here that defines the Contact getNumbers function:
void getNumbers(struct Numbers *numbers) {
//Variable/Structure declaration:
char notify;
// Contact Numbers Input:
printf("Do you want to enter a cell phone number? (y or n): "); //Prompt the user to see if they want to enter a cellphone.
scanf(" %s", &notify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's cell phone number: ");
scanf("%20s", numbers->cell);
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf(" %s", &notify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's home phone number: ");
scanf("%20s", numbers->home);
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf(" %s", &notify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's business phone number: ");
scanf("%20s", numbers->business);
}
}
contacts.h
// Structure type Name declaration (Milestone 1)
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
// Structure type Address declaration
// Place your code here... (from Milestone 1)
struct Address {
int streetNumber;
char street[40];
int apartmentNumber;
char postalCode[7];
char city[40];
};
// Structure type Numbers declaration
// Place your code here... (from Milestone 1)
struct Numbers {
char cell[20], home[20], business[20];
};
you should have a null terminator on the end of the postal code:
struct Address {
int streetNumber;
char street[40];
int apartmentNumber;
char postalCode[8];// add space for the null terminator
char city[40];
};
printf("Please enter the contact's postal code: ");
scanf(" %[^\n]", address->postalCode);
address->postalCode[7] = '\0'; // add a null terminator
this should be enough to solve the issue

Run-Time check failure #2 Stack around the variable 'optionAddress' was corrupted

I've been getting this error AFTER my program is done running, but before I get the 'Press any key to continue...' prompt. Here is my code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
int main (void)
{
// Declare variables here:
struct Name names;
char optionName;
struct Address addresses;
char optionAddress;
struct Numbers number;
char optionCell;
char optionHome;
char optionBusiness;
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Contact Name Input:
printf("Please enter the contact's first name: ");
scanf ("%s", &names.firstName);
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf(" %c", &optionName);
while (optionName == 'y' || optionName == 'Y') {
printf("Please enter the contact's middle initial(s): ");
scanf("%s", &names.middleInitial);
break;
}
printf("Please enter the contact's last name: ");
scanf("%s", &names.lastName);
// Contact Address Input:
printf("Please enter the contact's street number: ");
scanf("%s", &addresses.streetNumber);
printf("Please enter the contact's street name: ");
scanf("%s", &addresses.street);
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%s", &optionAddress);
while (optionAddress == 'y' || optionAddress == 'Y') {
printf("Please enter the contact's apartment number: ");
scanf("%s", &addresses.apartmentNumber);
break;
}
printf("Please enter the contact's postal code: ");
scanf("%s", &addresses.postalCode);
printf("Please enter the contact's city: ");
scanf("%s", &addresses.city);
// Contact Numbers Input:
printf("Do you want to enter a cell phone number? (y or no): ");
scanf("%s", &optionCell);
while (optionCell == 'y' || optionCell == 'Y') {
printf("Please enter the contact's cell phone number: ");
scanf("%s", number.cell);
break;
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf("%s", &optionHome);
while (optionHome == 'y' || optionHome == 'Y') {
printf("Please enter the contact's home phone number: ");
scanf("%s", &number.home);
break;
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf("%s", &optionBusiness);
while (optionBusiness == 'y' || optionBusiness == 'Y') {
printf("Please enter the contact's business phone number: ");
scanf("%s", number.business);
break;
}
I've looked through here to see how other people have fixed this issue but it seems like it's a memory issue or something and different for everyone. If I expand my program the error will not come until the code is finished. I've tried it without the "%s" and with " %c" instead, but when I do that the program skips the scanf's. If I've not provided enough information please let me know.
I've tried it without the "%s" and with " %c" instead, but when I do that the program skips the scanf's.
The reason behind stack corruption when you use %s is due to the null character (\0) which would be inserted after the single character. Since the size you have allocated for optionAddress is just a single character, you would be using more (at least two characters) than what you have allocated.
Therefore, using %c would be the right choice, and the skipping of scanfs would be probably due to the remaining newline characters in the input buffer. I'm not really sure which code will be able to fix this (Since I do not know how many scanfs are being skipped), but using getchar() to just remove a character(which could be a newline) from the input buffer, or using fflush(stdin) could help clearing the input buffer.
Using fflush(stdin) is not good practice though ,and even may not work depending on your environment. Just using " %c" like you did should usually solve the newline issue, but if it doesn't you could play around with getchar() until the scanfs are not skipped anymore.

How to compare strings with user input?

I have a question in my paper. I have 10 employee ids M001,A004,D007,etc...User is inputting one of the the mentioned Ids and if the id is not there it prints id not found. I tired with strcmp and got stuck. Its good if you tell me a way to do it? Thanks, note: i am a beginner in C.I am trying an easy way now it gives the error with the for loop.
subscripted value is neither array nor pointer nor vector
#include<stdio.h>
#include<string.h>
float tSalary(float salary,float bonus);
char searchid(char search);
int main(void)
{
char status,input,search,C,P;
char searchId[8][4]={"M001","A004","D007","D010","D012","Q008","Q015","DE09"};
float salary,bonus,tSalary;
int i,j;
do{
printf("Enter the employee id: ");
scanf("%s", &search);
printf("Enter the job status: ");
scanf("%s", &status);
printf("Enter the salary: ");
scanf("%f", &salary);
for(i=0;i<8;i++){ //This is where all things go wrong
for(j=0;j<4;j++){
if(searchid[i][j]=search){ //the [i] where the subscripted error occurs
printf("Id is valid\n");
}
else printf("Invalid id\n");
}
}
printf("Do you want to enter another record?(Y-Yes/N-No): ");
scanf(" %c", &input);
}while(input=='Y'||input=='y');
return 0;
}
There are quite a few problems in the posted code. For starters, searchId should be declared as searchId[8][5], to make room for the \0 terminator at the end of each string.
It appears from the input code that status and search should hold strings, but these are declared as chars. After fixing this, note that there is no need for the address operator & in the calls to scanf() that read into these arrays. Also, maximum widths should always be specified when using the %s conversion specifier with scanf() to avoid buffer overflows.
Strings can not be compared using the == comparison operator, so strcmp() should be used here. This can be done in a loop that steps through the array of strings; the loop exits when the index reaches 8, or a comparison is successful. Then, after the loop, if the index has reached 8 (all valid id strings failed the test) the search string was not valid.
Here is a modified version of the posted code that implements all of this:
#include <stdio.h>
#include <string.h>
float tSalary(float salary,float bonus);
char searchid(char search);
int main(void)
{
char status[1000];
char search[1000];
char input, C, P;
char searchId[8][5] = { "M001", "A004", "D007", "D010",
"D012", "Q008", "Q015", "DE09" };
float salary, bonus, tSalary;
int i, j;
do {
printf("Enter the employee id: ");
scanf("%999s", search);
printf("Enter the job status: ");
scanf("%999s", status);
printf("Enter the salary: ");
scanf("%f", &salary);
i = 0;
while (i < 8 && strcmp(search, searchId[i]) != 0) {
++i;
}
if (i < 8) {
printf("Id is valid\n");
} else {
printf("Invalid id\n");
}
printf("Do you want to enter another record?(Y-Yes/N-No): ");
scanf(" %c", &input);
} while (input == 'Y' || input == 'y');
return 0;
}
Sample program interaction:
Enter the employee id: A004
Enter the job status: pending
Enter the salary: 2000
Id is valid
Do you want to enter another record?(Y-Yes/N-No): y
Enter the employee id: Q015
Enter the job status: completed
Enter the salary: 3000
Id is valid
Do you want to enter another record?(Y-Yes/N-No): y
Enter the employee id: B001
Enter the job status: completed
Enter the salary: 1200
Invalid id
Do you want to enter another record?(Y-Yes/N-No): n

Resources