Click here to Skip to main content
15,990,892 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The Promblem is..

make the menu like this

1. Add data
2. Delete Data
3. Display Data.
4. Quit
Input: __

If user choose "1" in the menu, program should print

Input value: __

and take only integer value for input value.

Program has to take the input data in linked list in regular sequence.
If user input un-integer value, it should print "Error: Input value is invalid",
find previous input data in linked list, erase it and take input data again.
When program erase un-integer value, it prints "Data was erased."
If it dose not have input data, it should print "Corresponding data does not exist.".

If user choose 3. Display Data, it should print all data in regular sequence.
(print 5 data in one line)

If user choose 4. Quit, program should be over.
(in this time, linked list that made should be relieved.



-> My Question is How can I take the value(when I choose menu 1. Add Data) only integer.
I have to be warning message "Error : Input value is invalid" when user input un-integer value( like 3.5, 5.6)
And I don't know How to print 5 Data on each line.
(like

1 2 5 6 8
7 0 9 4 11
...

)

tanks and regards





And this is my code that I made.

C++
#include <stdio.h>
#include <stdlib.h>
#define FUNC_SIZE 4  

typedef struct node_record {
 int nData;
 struct node_record *next;
} _node;

typedef _node* list_node;  
list_node head = NULL; 
int list_menu(int*);    
int input_num(int*, int);  
int insert_node(void);   
int delete_node(void);   
int print_node(void);  
int end_proc(void);   


int main(void)
{
	int nSelect=0, nRetry=1; 
	int (*func_node[FUNC_SIZE])(void)={insert_node, delete_node, print_node, end_proc};  
	while(nRetry)  
	{
		list_menu(&nSelect);  
		nRetry = func_node[nSelect-1]();   
	}
	return 0;
}

 
int list_menu(int* pSelect)
{
	puts("********MENU********");
	puts("* 1. Add Data      *");
	puts("* 2. Delete Data   *");
	puts("* 3. Display Data  *");
	puts("* 4. Quit          *");
	puts("********************");
	do 
	{
		fflush(stdin);
		printf("*Choice Menu : ");
		scanf_s("%d", pSelect); 
	} while(*pSelect > 4 || *pSelect < 1);   
	return 0;
}


int input_node(int* pNum, int nFlag)
{
	printf("*%s : ", (nFlag == 1)?"Input value":"To delete value");
	scanf_s("%d", pNum); 
	return 0;
}


int insert_node(void)
{
	list_node temp, ptr, preptr;  
	int nData=0;   
	preptr = head;  
	temp = (list_node)malloc(sizeof(_node));   
	input_node(&nData, 1); 
	if(temp == NULL)  
	{
		printf("*Memory wasn't enough.");
		exit(1);   
	}
	temp->nData = nData;  
	if(head == NULL || head->nData > nData)  
	{
		temp->next = head;  
		head = temp;    
	}
	else  
	{
		ptr = preptr->next;  
		while(ptr != NULL)  
		{
			if(ptr->nData > nData) 
				break;   
			preptr = ptr;  
			ptr = ptr->next; 
		}
		temp->next = ptr;   
		preptr->next = temp;  
	}
	return 1;
}



int delete_node(void)
{
	list_node preptr, ptr; 
	int nData=0;     
	input_node(&nData, 2); 
	if(head == NULL)   
	{
		puts("*Data does not exist.");
	}
	else if(head->nData == nData)  
	{
		preptr = head;  
		head = preptr->next;  
		free(preptr);     
		puts("*Data was erased.");  
		return 1;
	}
	else
	{
		preptr = head;    
		ptr = preptr->next;   
		while(ptr != NULL && ptr->nData != nData)  
		{
			preptr = ptr;    
			ptr = ptr->next;   
		}
		
		if(ptr == NULL)  
			puts("*Corresponding data does not exist.");  
		else  
		{
			preptr->next = ptr->next;  
			free(ptr);       
			puts("*Data was erased."); 
		}
	}
	return 1;
}


int print_node(void)
{
	int nIdx=1;  
	list_node preptr;  
	preptr = head;  

	if(preptr == NULL)  
		puts("*Data does not exist.");
	else  
	{
		printf("*Display Data : ");
		while(preptr != NULL)
		{
			printf("%d ",  preptr->nData);
			preptr = preptr->next;  
		}
		printf("\n");
	}
	return 1;
}


int end_proc(void)
{
	list_node temp;
 
	while(head != NULL) 
	{
		temp = head;  
		head = temp->next;
		free(temp); 
	}
	puts("Quit the program.");   
	return 0;
}
Posted
Updated 26-May-12 21:20pm
v3

1 solution

At the moment you input_node routine is a bit basic:
C#
int input_node(int* pNum, int nFlag)
{
    printf("*%s : ", (nFlag == 1)?"Input value":"To delete value");
    scanf_s("%d", pNum);
    return 0;
}

But if you look at the definition of scanf_s[^] you will see that it has a return value, which you are ignoring.
The description tells us what the return code is:

"Returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character."

So, what do you suppose happens when your user enters a bad integer value?

Perhaps, if you tested the return code for a valid number? Or against the error code value?

BTW: Do yourself a big favour: comment that code! You may remember what it does and how it does it today, but next week? Next month? Get into the habits of using descriptive names and commenting everything as you go along - it will save you a lot of time in the future!
 
Share this answer
 

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