Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct NodeT_
{
	char name[30], lastName[20];
	char year[10];
	char id[30];
	int randomID;
	float average;
	char email[30];
	char ppsn[10];
	struct NodeT_* NEXT;
}node;

void addElementAttheEnd(node* top, char studentID[]);
void addElementAttheStart(node** top, char studentID[]);
void display(node* top);
void displayToFile(node* top);
int checkPpsn(node* top);
int search(node* top, char searchID[]);
int location(node* top, char searchID[]);
int length(node* top);
void deleteAtEnd(node* top);
void deleteAtStart(node** top);
void deleteintheMiddle(node* top, char searchID[]);
void deletedAtLocation(node* top, int loc);
void updateNodeValues(node* top, char ID[]);
int main()
{
	node* HEAD = NULL;
	node* temp;
	node* temp2;
	int option;
	int searchNum;
	int result;
	int foundLocation;
	char searchTerm[20];
	//int foundLocation;
	FILE* inp;
	node fileValue;


	/** Repopulate the linked list based on the file **/
	inp = fopen("links.txt", "r");

	if (inp == NULL)
		printf("Sorry the file could not be opened\n");
	else
	{
		while (!feof(inp))
		{
			result = fscanf(inp, "%s %s %s %s %s", fileValue.ppsn, fileValue.name, fileValue.lastName, &fileValue.year, fileValue.email);

			if (result == 5)
			{
				printf("%s %s %s %s %s\n", fileValue.ppsn, fileValue.name, fileValue.lastName, fileValue.year, fileValue.email);
				//Add the fileValue data to the list
			}

		}

		fclose(inp);
	}

	printf("1) Add customer(Note: PPS Number must be unique).\n");
	printf("2) Display all customer details to screen\n");
	printf("3) Display Customer Details\n");
	printf("4) Update a Customer Details\n");
	printf("5) Delete Customer\n");
	printf("6) Generate statistics(a – e) based on the user selecting a booking type\n");
	printf("7) Print all customer details into a report file.\n");
	printf("8) List all the customers in order of year born :\n");
	scanf("%d", &option);

	while (option != -1)
	{

		if (option == 1)
		{
			if (HEAD == NULL)
				addElementAttheStart(&HEAD, searchTerm);
			else
			{
				result = location(HEAD, searchTerm);
				if (result == -1)
					addElementAttheEnd(HEAD, searchTerm);
				else
					printf("Sorry this student already exists\n");
			}
		}

		else if (option == 2)
		{
			display(HEAD);
		}

		else if (option == 3)
		{
			if (HEAD == NULL)
				printf("Can not delete from an empty list\n");
			else if (HEAD->NEXT == NULL)
				deleteAtStart(&HEAD);
			else
				deleteAtEnd(HEAD);
		}

		else if (option == 4)
		{
			printf("Please enter the PPSN or Name you wish to update\n");
			scanf("%s", searchTerm);

			updateNodeValues(HEAD, searchTerm);
		}

		else if (option == 5)
		{
			if (HEAD == NULL)
			{
				printf("The list is empty so this option can not be completed\n");
			}
			else
			{
				printf("Please enter the number being searched for\n");
				scanf("%d", &searchNum);
				foundLocation = location(HEAD, searchNum);

				printf("The location is %d\n", foundLocation);


				if (foundLocation < 0)
					printf("This value is not in the list\n");
				else if (foundLocation == 1)
					deleteAtStart(&HEAD);
				else if (foundLocation == length(HEAD))
					deleteAtEnd(HEAD);
				else
					deletedAtLocation(HEAD, foundLocation);

			}
		}



		printf("1) Add customer(Note: PPS Number must be unique).\n");
		printf("2) Display all customer details to screen\n");
		printf("3) Display Customer Details\n");
		printf("4) Update a Customer Details\n");
		printf("5) Delete Customer\n");
		printf("6) Generate statistics(a – e) based on the user selecting a booking type\n");
		printf("7) Print all customer details into a report file.\n");
		printf("8) List all the customers in order of year born :\n");
		printf("Press -1 to exit\n");
		scanf("%d", &option);
	}

}


void addElementAttheEnd(node* top, char studentID[])
{
	node* temp;
	node* temp2;

	//Creating the new node
	temp = (node*)malloc(sizeof(node));

	//Reading in the data 
	printf("Please enter PPSN number\n");
	scanf("%s", &temp->ppsn);
	printf("Please enter your first name\n");
	scanf("%s", temp->name);
	printf("Please enter your surname\n");
	scanf("%s", &temp->lastName);
	//printf("Please enter your ID\n");
	//scanf("%s", temp->id);
	strcpy(temp->id, studentID);
	printf("Please enter the year you were born\n");
	scanf("%s", &temp->year);
	printf("Please enter your email\n");
	scanf("%s", temp->email);

	temp->NEXT = NULL;

	//Finding the last node
	temp2 = top;
	while (temp2->NEXT != NULL)
	{
		temp2 = temp2->NEXT;
	}

	temp2->NEXT = temp;
}

void addElementAttheStart(node** top, char studentID[])
{
	node* temp;

	//Creating the new node
	temp = (node*)malloc(sizeof(node));

	//Reading in the data 
	printf("Please enter PPSN number\n");
	scanf("%s", &temp->ppsn);
	printf("Please enter your first name\n");
	scanf("%s", temp->name);
	printf("Please enter your surname\n");
	scanf("%s", &temp->lastName);
	//printf("Please enter your ID\n");
	//scanf("%s", temp->id);
	strcpy(temp->id, studentID);
	printf("Please enter the year you were born\n");
	scanf("%s", &temp->year);
	printf("Please enter your email\n");
	scanf("%s", temp->email);


	temp->NEXT = *top; //*top currently has the address of the first node 
	//in the list which become the second node when temp is added infront of it
	*top = temp;
}

void display(node* top)
{
	node* temp;
	if (top == NULL)
		printf("The list is empty\n");
	else
	{
		temp = top;

		while (temp != NULL)
		{
			//Display payload data
			//Move to the next node
			printf("==================================================================================================================\n");
			printf("PPSN:           %s\n", temp->ppsn);
			printf("Firstname:      %s\n", temp->name);
			printf("Surname:        %s\n", temp->lastName);
			printf("Year:           %s\n", temp->year);
			printf("Email:          %s\n", temp->email);
			printf("==================================================================================================================\n");
			temp = temp->NEXT;
		}
	}
}

void displayToFile(node* top)
{
	FILE* outp;
	node* temp;

	if (top == NULL)
		printf("The list is empty\n");
	else
	{
		temp = top;

		outp = fopen("Links.txt", "w");

		if (outp == NULL)
			printf("Sorry the file could not be opened\n");

		else
		{
			while (temp != NULL)
			{
				//Display payload data
				//Move to the next node
				fprintf(outp, "PPSN:           %20s\n", temp->ppsn);
				fprintf(outp, "Firstname:      %20s\n", temp->name);
				fprintf(outp, "Surname:        %20s\n", temp->lastName);
				fprintf(outp, "Year:           %20s\n", temp->year);
				fprintf(outp, "Email:          %20s\n", temp->email);
				temp = temp->NEXT;
			}

			fclose(outp);
		}
	}
}

int checkPpsn(int ppsn)
{
	return 1;
}

int search(node* top, char searchID[])
{
	node* temp;


	if (top == NULL)
		printf("The list is empty\n");
	else
	{
		temp = top;

		while (temp != NULL)
		{
			//Display payload data
			//Move to the next node
			if (strcmp(temp->name, searchID) == 0)
			{
				printf("Found the element");
				return 1;
			}

			temp = temp->NEXT;
		}
	}

	return 0;
}

int length(node* top)
{
	node* temp;
	int length = 0;

	if (top == NULL)
		return 0;

	else
	{
		temp = top;

		while (temp != NULL)
		{
			//Display payload data
			//Move to the next node
			length++;
			temp = temp->NEXT;
		}

	}

	return length;
}

void deleteAtEnd(node* top)
{
	node* last;
	node* secondlast{};

	last = top;

	while (last->NEXT != NULL)
	{
		secondlast = last;
		last = last->NEXT;
	}

	secondlast->NEXT = NULL;
	free(last);
}
void deleteAtStart(node** top)
{
	node* temp;

	temp = *top;
	*top = temp->NEXT;
	free(temp);

}
void deleteintheMiddle(node* top, char searchID[])
{
	node* last;
	node* secondlast{};
	//int searchNumber;
	int removed = 0;

	last = top;



	while (last->NEXT != NULL)
	{
		if (strcmp(last->id, searchID) == 0)
		{
			//This is the node to remove...
			secondlast->NEXT = last->NEXT;
			free(last);
			removed = 1;
			break;
		}

		secondlast = last;
		last = last->NEXT;
	}

	if (removed == 0)
	{
		printf("The node could not be found\n");
	}
}

void updateNodeValues(node* top, char ID[])
{
	node* temp;
	int search = 0;
	temp = top;

	while (temp != NULL)
	{
		if (strcmp(temp->ppsn, ID) == 0 || strcmp(temp->name, ID) == 0)
		{
			printf("Please enter the ppsn you wish to update\n");

			//Reading in the data
			printf("Please enter new ppsn\n");
			scanf("%s", temp->ppsn);
			printf("Please enter new first name\n");
			scanf("%s", temp->name);
			printf("Please enter new surname\n");
			scanf("%s", &temp->lastName);
			printf("Please enter the new year you were born\n");
			scanf("%s", &temp->year);
			printf("Please enter your new email\n");
			scanf("%s", temp->email);

			search = 1;
			break;
		}
	}

	if (search == 0)
		printf("The value of the ppsn was never found\n");
}


int location(node* top, char searchID[])
{
	node* temp;
	int location = -1;
	int i = 1;

	temp = top;

	while (temp != NULL)
	{
		if (strcmp(temp->ppsn, searchID) == 0)
		{
			location = i;
			break;
		}
		i++;
		temp = temp->NEXT;
	}

	return location;
}

void deletedAtLocation(node* top, int loc)
{
	node* temp;
	node* prevTemp{};
	int i;

	temp = top;

	for (i = 0; i < loc - 1; i++)
	{
		prevTemp = temp;
		temp = temp->NEXT;
	}


	prevTemp->NEXT = temp->NEXT;
	free(temp);

}


What I have tried:

The problem is in line 127 I keep getting an error saying cant convert argument 2 from int to char would anyone have a solution to this as I've tried changing the signs and this hasnt helped at all so far thanks very much

The function is to delete a node from a specific location and this error just keeps appearing
Posted
Updated 9-May-21 20:31pm

Here is the function prototype :
C++
int location(node* top, char searchID[]);
and here is how you are calling it :
C++
foundLocation = location(HEAD, searchNum);
and here is the declaration of searchNum :
C++
int searchNum;
The problem is as the error message described : you are passing an integer to a function that expects an array of characters.

To fix it, accept the input as a string and pass that string to the function. Note - I am referring to an array of characters as a string.
 
Share this answer
 
Comments
merano99 9-May-21 18:32pm    
When it ppsn is an char-array following is also not korrekt:
int checkPpsn(int ppsn)

Some datatypes should better be integer.
Effort and performance in the string comparison are not optimal.

Another point is the use of unsafe functions that are no longer common these days.
If you dont care use #define _CRT_SECURE_NO_WARNINGS
Rick York 9-May-21 20:17pm    
Tell them, not me.
merano99 10-May-21 16:21pm    
It was intended as an addition to your approach, not a correction.
akeem jokosenumi 9-May-21 20:23pm    
Hi @merano99 would you be able to do a sample code to fix this please as I've been attempting for the past hour now on how to fix this I've tried multiple things by changing it to an int but for some reason the error is still appearing for me
Your code is messing out with data types.
I suggest you to compile it with all the warning enabled (for example, with the -Wall on GCC). Then the first warning you get will be on line
Quote:
result = fscanf(inp, "%s %s %s %s %s", fileValue.ppsn, fileValue.name, fileValue.lastName, &fileValue.year, fileValue.email);
that should be instead
C++
result = fscanf(inp, "%s %s %s %s %s", fileValue.ppsn, fileValue.name, fileValue.lastName, fileValue.year, fileValue.email);


The code does have many of similar issues.
You should probably read a good tutorial on C programming language data types.
 
Share this answer
 
v2
It's more work than leaving it with strings, but it would probably make sense.

The scanf () function in connection with "% s" is problematic.

If ppsn is an int the you have to change the definition and
several other places.
C
typedef struct NodeT_
{
   ....
   int ppsn;
   struct NodeT_* NEXT;
}node;

void addElementAttheEnd(node* top, int studentID);
void addElementAttheStart(node** top, int studentID);
void updateNodeValues(node* top, int ID);
int location(node* top, int searchID);
int search(node* top, int searchID);
void deleteintheMiddle(node* top, int searchID);

-------------

int showmenu()
{
  int option;
  printf("1) Add customer by PPSN (Note: PPS Number must be unique).\n");
  printf("2) Display all customer details to screen\n");
  printf("3) Display Customer Details\n");
  printf("4) Update a Customer Details\n");
  printf("5) Delete Customer\n");
  printf("6) Generate statistics(a – e) based on the user selecting a booking type\n");
  printf("7) Print all customer details into a report file.\n");
  printf("8) List all the customers in order of year born :\n");
  scanf("%d", &option);
}

-------------

result = fscanf(inp, "%d %s %s %s %s", fileValue.ppsn, ...);

printf("%d %s %s %s %s\n", fileValue.ppsn, ...);

-------------

do {
   option = showmenu()

   switch (option) 
   {
   case 1: {
     printf("Please enter the PPSN you wish to add\n");
     scanf("%d", &searchNum);

     if (HEAD == NULL)
          addElementAttheStart(&HEAD, searchNum);
     else {
          result = location(HEAD, searchNum);
          if (result == -1)
          addElementAttheEnd(HEAD, searchNum);
          else
               printf("Sorry this student already exists\n");
          }
     result = location(HEAD, searchTerm);
      } // eof case 1
   case 2:
   ...
   } // eof switch
} while (option != -1)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900