Hello all!
I am a completly newbie at C and I dont have good basis from Java so I cant get the linked lists part from C. I am trying to run the code from the function that is written at the end of this message but it has run-time errors so its hard to find the bugs. I really know that my logic is not so good since I cant understand that bit so well. I am using dm compiler. Could somebody possibly tell me what my mistakes are.
Thank you very much,
Vanessa. -
/**********header file******/
-
-
/* Header files. */
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <time.h>
-
-
/* Constants. */
-
#define TITLE_LEN 40
-
#define AUTHOR_LEN 15
-
#define REPORT_FILE "report.txt"
-
#define RESTOCK_LEVEL 5
-
#define SIZE 100
-
-
-
typedef struct saleStruct {
-
int year, month, day;
-
int quantity;
-
float price;
-
struct saleStruct* next;
-
} SaleType;
-
-
typedef struct bookStruct {
-
int id;
-
char title[TITLE_LEN + 1];
-
char author[AUTHOR_LEN + 1];
-
int year;
-
float price;
-
int count;
-
int saleCount;
-
SaleType* head;
-
struct bookStruct* next;
-
} BookType;
-
-
typedef struct bookListStruct {
-
BookType* head;
-
int count;
-
} BookListType;
-
-
/*******function to load data***************/
-
-
void loadData(BookListType* booklist, char* booksFile, char* salesFile) {
-
-
FILE *bp,*sp;
-
char line[SIZE],*date,*title,*author,*result=NULL,*result2=NULL,delims[]=":";
-
float bprice,sprice;
-
int i,id,year,count,y,m,d,quantity;
-
-
BookType *newBook,*preB,*currB,*current;
-
SaleType *newSale,*currSale;
-
-
if((bp=fopen(booksFile,"r"))==NULL){
-
printf("Error openning %s\n - Exiting...",booksFile);
-
exit(0);
-
-
}
-
while((fgets(line, SIZE-1, bp))!= NULL ) {
-
-
line[strlen(line) - 1] = '\0';
-
// printf("Strlen(line):%d\n",strlen(line));
-
// printf("%s\n",line);
-
-
result=strtok(line,delims);
-
id = atoi(result);
-
result=strtok(NULL,delims);
-
title = result;
-
result=strtok(NULL,delims);
-
author = result;
-
result=strtok(NULL,delims);
-
year = atoi(result);
-
result=strtok(NULL,delims);
-
bprice = atof(result);
-
result=strtok(NULL,delims);
-
count= atoi(result);
-
result=strtok(NULL,delims);
-
-
// printf("%d\t %s\t%s",id,title,author);
-
// printf("%d %f %d\n",year,bprice,count);
-
-
-
if((newBook=malloc(sizeof(BookType)))==NULL) {
-
printf("malloc unsuccesfull for BookType - Exiting\n");
-
exit(0);
-
}
-
-
-
newBook->id = id;
-
strcpy(newBook->title, title);
-
strcpy(newBook->author, author);
-
newBook->year=year;
-
newBook->price=bprice;
-
newBook->count=count;
-
-
/* newBook->next=booklist->head;
-
booklist->head=newBook;
-
booklist->count+=1;*/
-
-
-
preB=NULL;
-
currB=booklist->head;
-
while(currB != NULL) {
-
preB = currB;
-
currB = currB->next;
-
}
-
if(preB == NULL) {
-
booklist->head = newBook;
-
}
-
else {
-
preB->next = newBook;
-
}
-
-
}
-
fclose(bp);
-
-
if((sp=fopen(salesFile,"r"))==NULL){
-
printf("Error openning %s\n - Exiting...",salesFile);
-
exit(0);
-
}
-
-
while((fgets(line,SIZE-1,sp))!=NULL){
-
-
line[strlen(line) - 1] = '\0';
-
-
result=strtok(line,delims);
-
id = atoi(result);
-
result=strtok(NULL,delims);
-
date=result;
-
result=strtok(NULL,delims);
-
sprice = atof(result);
-
result=strtok(NULL,delims);
-
quantity =atoi(result);
-
result=strtok(NULL,delims);
-
-
result2=strtok(date,"/");
-
d=atoi(result2);
-
result2=strtok(NULL,"/");
-
m=atoi(result2);
-
result2=strtok(NULL,"/");
-
y=atoi(result2);
-
result2=strtok(NULL,"/");
-
-
-
// printf("id: %d\nd: %d\nm: %d\ny: %d\nprice: %f\nquantity: %d",id,d,m,y,sprice,quantity);
-
-
-
if((newSale=malloc(sizeof(SaleType)))==NULL) {
-
printf("malloc unsuccesfull for SaleType - Exiting\n");
-
exit(0);
-
}
-
-
-
current=booklist->head;
-
-
// while(current!=NULL){
-
-
if(current->id==id){
-
newSale->year=y;
-
newSale->month=m;
-
newSale->day=d;
-
newSale->price=sprice;
-
newSale->quantity=quantity;
-
-
newSale->next=currB->head;
-
current->head=newSale;
-
printf("Year of sale %d\tId %d\t%s\n",newBook->head->year,id,newBook->title);
-
current->saleCount+=1;
-
booklist->count+=1;
-
printf("Sale count %d\n",current->saleCount);
-
-
}
-
current=current->next;
-
//}
-
-
-
}fclose(sp);
-
-
}
1 1712
I'm betting your compiler already told you where the mistakes were. The output is cryptic, but usually useful. Can you post the compiler's error messages?
PS - Please use code tags in the future - especially on large blocks of code like that.
Thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tim Henderson |
last post by:
Hi i have some errors in my code that i can't find the reason for here
is the error
i have 3 classes:
-Song
-Album
-Artist
Song has 3 pieces of data with getter and setter meathods:
-Name...
|
by: George M Jempty |
last post by:
Is there a good linked list implementation in the public domain? Or
that someone on this group has already written that they'd like to put
in the public domain, right here on this newsgroup? I've...
|
by: Darryl B |
last post by:
I can not get anywhere on this project I'm tryin to do. I'm not
expecting any major help with this but any would be appreciated. The
assignment is attached. The problem I'm having is trying to set...
|
by: Eugen J. Sobchenko |
last post by:
Hi!
I'm writing function which swaps two arbitrary elements
of double-linked list. References to the next element of list
must be unique or NULL (even during swap procedure), the same condition...
|
by: farhanb |
last post by:
hello,
I am writing a simple linked list implementation. When I use function
insert1 I must allocate to head in my main to get it to work otherwise
list stays empty but when I use function...
|
by: FBM |
last post by:
Hi,
I am working on a program that simulates one of the elements of ATM.
The simulation stores events which occurs every some milliseconds for a
certain amount of time. Every time that an event...
|
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort...
|
by: william |
last post by:
When implementing Linked list, stack, or trees, we always use pointers
to 'link' the nodes.
And every node is always defined as:
struct node
{
type data; //data this node contains
...
node *...
|
by: Jeff Bown |
last post by:
Consider implementing a (doubly) linked list.
The simplest strategy is to provide functions
item_t add_item(item_t predecessor);
void delete_item(item_t item);
where add_item allocates memory...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |