Reputation: 1
I've been trying to fix several errors I'm getting while compiling this program, but can't seem to find any solutions. These are the following errors that occur followed by the actual code. Most of the errors are the 'unary * ' problems, although I thought that is the correct way of using it. If anyone could point me in the right direction, would be very helpful.
testReadLinkedList.c:13: warning: no semicolon at end of struct or union
testReadLinkedList.c: In function ‘insertListElement’:
testReadLinkedList.c:31: error: incompatible types in assignment
testReadLinkedList.c: In function ‘readFile’:
testReadLinkedList.c:43: warning: comparison between pointer and integer
testReadLinkedList.c: In function ‘countList’:
testReadLinkedList.c:51: error: invalid initializer
testReadLinkedList.c:54: error: invalid operands to binary !=
testReadLinkedList.c:56: error: invalid type argument of ‘unary *’
testReadLinkedList.c: In function ‘printList’:
testReadLinkedList.c:65: error: invalid initializer
testReadLinkedList.c:70: error: invalid type argument of ‘unary *’
testReadLinkedList.c:71: error: invalid type argument of ‘unary *’
testReadLinkedList.c:72: error: invalid type argument of ‘unary *’
testReadLinkedList.c:73: error: invalid type argument of ‘unary *’
testReadLinkedList.c:81: error: invalid type argument of ‘unary *’
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
typedef struct LinkedListNode {
int red;
int green;
int blue;
char function[128];
struct LinkedListNode* next
} LinkedListNode;
typedef struct {
LinkedListNode* head;
} Linkedlist;
void initLinkedList(Linkedlist* listIn) {
listIn = (Linkedlist*)malloc(sizeof(Linkedlist));
(*listIn).head = NULL;
}
void insertListElement(Linkedlist* listIn, int redIn, int greenIn, int blueIn, char* functionIn) {
LinkedListNode* newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
(*newNode).red = redIn;
(*newNode).green = greenIn;
(*newNode).blue = blueIn;
(*newNode).function = functionIn;
(*newNode).next = (*listIn).head;
(*listIn).head = newNode;
}
void readFile(FILE* fileIn, Linkedlist* listIn) {
char tempLine[128];
int tempRed, tempGreen, tempBlue;
char tempFunction[128];
while(fgets(tempLine, 128, fileIn) != EOF) {
sscanf(tempLine, "%d %d %d %[^\n]", &tempRed, &tempGreen, &tempBlue, tempFunction);
insertListElement(listIn, tempRed, tempGreen, tempBlue, tempFunction);
}
}
int countList(Linkedlist* listIn) {
LinkedListNode currNode = (*listIn).head;
int size = 0;
while(currNode != NULL) {
size++;
currNode = (*currNode).next;
}
return size;
}
void printList(Linkedlist* listIn) {
int i, tempRed, tempGreen, tempBlue;
char tempFunction[128];
LinkedListNode currNode = (*listIn).head;
int listSize = countList(listIn);
for(i = 0;i < listSize; i++) {
tempRed = (*currNode).red;
tempGreen = (*currNode).green;
tempBlue = (*currNode).blue;
tempFunction = (*currNode).function;
printf("Red: %d\n",tempRed);
printf("Green: %d\n",tempGreen);
printf("Green: %d\n",tempBlue);
printf("Function: %s\n",tempFunction);
print("t\n");
currNode = (*currNode).next;
}
}
int main(int argc, char** argv) {
FILE* f1;
Linkedlist* list1;
f1 = fopen(argv[1], "r");
if(f1 == NULL) {
printf("Error: could not open file");
} else {
initLinkedList(list1);
readFile(f1, list1);
}
}
Upvotes: 0
Views: 151
Reputation: 1
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
struct node
{
int itemno;
node *next;
};
node *temp=NULL,*top=NULL;
int e;
void push()
{
temp=new node;
temp->next=NULL;
if(temp==NULL)
{
cout<<"stack overflow";
return;
}
cout<<"enter the itemno"<<endl;
cin>>temp->itemno;
//temp->itemno=e;
if(top==NULL)
{
top=temp;
}
else
{
top=temp->next;
top=temp;
}
}
void pop()
{
if(top==NULL)
cout<<"stack underflow";
else
{
top=temp;
cout<<temp->itemno;
top=top->next;
}
delete temp;
}
void display()
{
if(top==NULL)
{
cout<<"stack underflow";
return;
}
else
{
temp=top;
while(temp!=NULL)
{
cout<<temp->itemno;
cout<<endl;
temp=temp->next;
}
}
}
int main()
{ char choice1;
int choice;
while(1)
{
cout<<"menu"<<endl;
cout<<"1.push"<<endl;
cout<<"2.pop"<<endl;
cout<<"e.displAY"<<endl;
cout<<"4.exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:{
do
{ push();
cout<<"enter y to add or n to cancel"<<endl;
cin>>choice1;
}while(choice1=='y');
break;}
case 2:{
do
{
cout<<"enter y to delete or n to cancel"<<endl;
cin>>choice1;
if(choice1=='y')
pop();
}while(choice1=='y');
break;}
case 3:display();
break;
case 4:return 0;
default:break;
}
}
getch();
return 0;
} can anybody point out the mistake in this.
Upvotes: 0
Reputation: 134286
struct LinkedListNode* next
--> missing semicolon.
(*newNode).function = functionIn;
change to strcpy(newNode->function,functionIn)
.
char * fgets ( char * str, int num, FILE * stream );
if not successful (If the end-of-file is encountered while attempting to read a character), it will return NULL
(not EOF).
are a few hints. There are lot of other errors. You might want to read basic C pointer concepts once again.
Upvotes: 1
Reputation: 97918
Here:
(*newNode).function = functionIn;
you need to strcpy
functionIn to (*newNode).function
, you can't simply assign to it. Also, on this line:
while(fgets(tempLine, 128, fileIn) != EOF)
fgets
does not return EOF but NULL on error, so you need to do:
while (fgets(tempLine, 128, fileIn))
and on this line:
LinkedListNode currNode = (*listIn).head;
head
is a pointer, you need to fix the type of currNone
:
LinkedListNode *currNode = listIn->head;
Finally, you need to change your initialization function:
void initLinkedList(Linkedlist **listIn) {
*listIn = (Linkedlist *)malloc(sizeof(Linkedlist));
(*listIn)->head = NULL;
}
and the call:
initLinkedList(&list1);
Upvotes: 3