#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct 
{
	int code;
	char name;
	float amount;
	int shares;
	float c_price;
}DATA;

typedef struct stockList
{
	DATA stock;
	stockList* link;
}STOCK;

STOCK* insertStock(STOCK* pList, STOCK* pPre, DATA item);
STOCK* buildList(char* fileID);
void printList(STOCK* pList);
void fprintList(STOCK* pList);
bool searchList(STOCK* pList, STOCK** pPre, STOCK** pCur, int target);
void modifyList(STOCK* pList,int pre_code, int code, char name, float amount, int shares, float c_price);
void display_List(STOCK* pList, STOCK** pPre, STOCK** pCur, int target);


int main (void)
{
	STOCK* pList;
	STOCK* pPre;
	STOCK* pCur;
	DATA stock;
	int pre_code, code, name, amount, shares, c_price;
	int more = 1;
	int num;

	printf("Stock List contains\n\n");
	pList = buildList("Stock1.txt");
	
	if(!pList)
	{
		printf("Error opening stock file\n");
		exit(210);
	}
	
	
		printList(pList);

		printf("Please select the task preferred\n");
		printf("=========================================================\n");
		printf("1 for modifying existing stock\n");
		printf("2 for searching for a stock and printing the stock data\n");
		printf("3 for printing data back to file\n\n");
		printf("0 for Exit\n\n");
		printf("Please enter number = ");
		scanf("%d", &num);
		printf("\n\n");

		switch(num)
		{
		case 1:
			{
				printf("Please enter the stock code to be modified = ");
				scanf("%d", &pre_code);
			
				if(searchList(pList, &pPre, &pCur, pre_code))
				{
					printf("Stock exists\n\n");
					printf("Enter new stock code = ");
					scanf("%d", &code);
					printf("Enter new stock name = ");
					scanf("%s", &name);
					printf("Enter new stock amount = ");
					scanf("%f", &amount);
					printf("Enter new stock shares = ");
					scanf("%d", &shares);
					printf("Enter new stock current price = ");
					scanf("%f", &c_price);

					modifyList(pList,pre_code, code, name, amount, shares, c_price);
				}
				else
					printf("Stock does not exist\n\n");

				break;
			}
		case 2:
			{
				printf("Please enter the stock code to be viewed = ");
				scanf("%d", &code);
				if (searchList(pList, &pPre, &pCur, code) ==1)
					printf("Stock exists\n\n");
				else
					printf("Stock does not exist\n\n");

				display_List(pList, &pPre, &pCur, code);

				break;			
			}
		case 3:
			{
				fprintList(pList);
				printf("Summary.txt can be printed out\n");
								
				break;
			}
		default:
			{
				printf("Error in number inserted. Please try again\n");
				break;
			}
		}
	return 0;
}

STOCK* buildList(char* fileID)
{
	DATA stock1;
	STOCK* pList;
	STOCK* pPre;
	STOCK* pCur;
	FILE* fStock;
 
	pList = NULL;
	fStock = fopen(fileID,"r");
	if(!fStock)
	{
		printf("Error reading from file\n");
		exit(104);
	}
	while((fscanf(fStock,"%d %s %f %d %f",&stock1.code, stock1.name, &stock1.amount, &stock1.shares, &stock1.c_price))==1)
	{
		searchList(pList, &pPre, &pCur, stock1.code);
		pList = insertStock(pList, pPre, stock1);
	}
	return pList;
}

STOCK* insertStock(STOCK* pList, STOCK* pPre, DATA item)
{
	STOCK* pNew = NULL;

	pNew = (STOCK*) malloc( sizeof( STOCK ));
	if(!(pNew = (STOCK*)malloc(sizeof(STOCK))))
	{
		printf("\aMemory overflow in insert\n");
		exit(100);
	}
	pNew->stock = item;
	
	if(pPre == NULL)
	{
		pNew->link = pList;
		pList = pNew;
	}
	else
	{
		pNew->link = pPre->link;
		pPre->link = pNew;
	}
	return pList;
}

void modifyList(STOCK* pList,int pre_code, int code, char name, float amount, int shares, float c_price)
{
	STOCK* pCur = NULL;
	STOCK* pPre = NULL;
	if ((searchList(pList,&pCur,&pPre,pre_code)) == true)
	{
		pCur->stock.code = code;
		pCur->stock.name = name;
		pCur->stock.amount = amount;
		pCur->stock.shares = shares;
		pCur->stock.c_price = c_price;
	}
	else
	{
		printf("No such data!!\n");
	}
	return;
}

bool searchList(STOCK* pList, STOCK** pPre, STOCK** pCur, int target)
{
	bool found = false;
	*pCur = pList;
	*pPre = NULL;
	
	while(pCur != NULL && target > (*pCur)->stock.code)
	{
		*pPre = *pCur;
		*pCur = (*pCur)->link;
	}

	if(*pCur && target == (*pCur)->stock.code)
		found = true;

	return found;
}

void printList(STOCK* pList)
{
	STOCK* pWalker;

	pWalker = pList;
	printf("Stock contains\n");
	printf("Code\tName\tAmount\tShares\tCurrent Price\n");
	printf("====\t====\t======\t======\t=============\n");
	while(pWalker)
	{
		printf("%d\t%s\t%.2f\t%d\t%.2f\n",pWalker->stock.code, pWalker->stock.name, pWalker->stock.amount, pWalker->stock.shares, pWalker->stock.c_price);
		pWalker = pWalker->link;
	}
	printf("\n");
	return;
}

void fprintList(STOCK* pList)
{
	STOCK* pWalker;
	FILE* flist;

	flist = fopen("Summary.txt", "w");

	if (!flist)
	{
		printf("Error in creating file\n");
		exit (106);
	}

	pWalker = pList;
	fprintf(flist,"Stock contains\n");
	fprintf(flist,"Code\tName\tAmount\tShares\tCurrent Price\n");
	fprintf(flist,"====\t====\t======\t======\t=============\n");
	while(pWalker)
	{
		fprintf(flist,"%d\t%s\t%.2f\t%d\t%.2f\n",pWalker->stock.code, pWalker->stock.name, pWalker->stock.amount, pWalker->stock.shares, pWalker->stock.c_price);
		pWalker = pWalker->link;
	}
	fprintf(flist,"\n");
	return;
}

void display_List(STOCK* pList, STOCK** pPre, STOCK** pCur, int target)
{
	*pCur = pList;
	*pPre = NULL;
	while(pCur != NULL && target > (*pCur)->stock.code)
	{
		*pPre = *pCur;
		*pCur = (*pCur)->link;
	}
	if(*pCur && target == (*pCur)->stock.code)
	{
		printf("Code\tName\tAmount\tShares\tCurrent Price\n");
		printf("====\t====\t======\t======\t=============\n");
		printf("&d\t%s\t%.2f\t%d\t%.2f\n",(*pCur)->stock.code, (*pCur)->stock.name, (*pCur)->stock.amount, (*pCur)->stock.shares, (*pCur)->stock.c_price);
	}
	return;
}

i can't read the data in my file.. COmpiled no error.. but, can't display output...

Hoping someone could tell me the problem... thanks in advance...=)

Recommended Answers

All 6 Replies

Nice details of the problem. I understand exactly what and where based on you extensive explanation.

Answer:
Maybe you coded it wrong. But I'm not searching through 262 lines of code for the one that might be wrong. Maybe you could help by giving us details? Line numbers? What actually happens?

you said cant read the data from file, so the actual problem lies in reading a file or displaying output.
Can you just explain your problem.

Walt: i am havin my problem in the buildList function...
somehow, the codes compiles with no error... but, once i start wthout debugging, a pop out comes in front of the command window telling my project has found a problem bla bla bla... It stops at the place where i called the buildList function... so, yeah... Thanks for replyin bro..=)

Tajendra: kinda havin problem to build the link list in the first place...


buildList function : Line 118 - 139

calling function buildList: line 40

Some suggestions
1. Print the contents that you read from the files. In line 133
2. Why are you not considering the value returned by the searchList function- Line 135
3. Also when you call the insert function. What parameters are you sending into the function
4. Google for "insert a node in a lined list"

At line 10, the struct has only one character reserved for the name
which does not make sense, especially with the terminating null character.
Line 35 creates an integer called "name", and line 74 puts an arbitrary number of characters into the address pointed to by that integer. This should crash.

Try using array of char: char name[101];
and then write the code to put no more than 100 characters in it.

thanks uncle..=)
code solved after array in and a few adjustments of myself....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.