FIXED - I'm getting a parse error somewhere in this section of code before the else and I can't see it. Please help someone.

NEED HELP HERE -
Okay, I'm apparently getting a parse error somewhere in this code. Any help?

All the errors are listed as "Parse error before '.'" and are happening in the main function at the very end of the code. The first is inside the while statement.

// This is the implementation file for class List

#include <iostream>
#include <fstream>
#include <stddef.h>		// to access NULL
#include "List.h"
using namespace std;

typedef NodeType* NodePtr;

struct NodeType
{
    ItemType item;
    NodePtr  next;
};

List::List()
// Post:  listPtr is set to NULL.
{
    listPtr = NULL;
}

//***********************************************************
List::List(const List&  otherList)
// Copy-constructor for List.
{
    NodeType* ptr1;
	NodeType* ptr2;

	if (otherList.listPtr == NULL)
		{ listPtr = NULL; }
	else
	{
		listPtr = new NodeType;
		listPtr->item = otherList.listPtr->item;
		ptr1 = otherList.listPtr->next;
		ptr2 = listPtr;
		while (ptr1 != NULL)
		{ ptr2->next = new NodeType;
		  ptr2 = ptr2->next;
		  ptr2->item = ptr1->item;
		  ptr1 = ptr1->next;
		}
	ptr2->next = NULL;
	}
}

//***********************************************************
bool  List::IsThere(ItemType  item) const
// Post: If item is in the list IsThere is
//       True; False, otherwise.
{
	NodeType* location = listPtr;

    while (location != NULL)
          { if (item == listPtr->item )
               { return true; } }
	return false;
}

//***********************************************************
void  List::Insert(ItemType  item)
// Pre:  item is not already in the list.
// Post: item is the first item in the list.
{
	NodeType* location;

	if (IsThere(item) == false)
	{ location = new NodeType;
	  location->item = item;
	  location->next = listPtr;
	  listPtr = location;
	}
}

//***********************************************************
void  List::Delete(ItemType  item)
// Pre:  item is in the list.
// Post: item is no longer in the list.
{
	NodeType* location = listPtr;
	NodeType* tempLocation;

	if (IsThere(item) == true)
		{ tempLocation = location->next;
		  listPtr = listPtr->next;
		}
	else
	    {
		  while (IsThere(item) == true)
			     { location = location->next; }

		  tempLocation = location->next;
		  location->next = (location->next)->next;
	    }
	delete tempLocation;
}

//***********************************************************
void  List::Print() const
// Post: Items on the list are printed on the screen.
{
	NodeType* location = listPtr;

	while (location != NULL)
		  { cout << location->item << endl;
	        location = location->next; }
}

//***********************************************************
int  List::Length() const
// Post: Number of items have been counted; result returned.
{
    NodeType* location = listPtr;
	int length = 0;

	while (location != NULL)
		  { location = location->next;
			length++; }
	return length;
}

//***********************************************************
List::~List()
// Post: All the components are deleted.
{
    NodeType* tempPtr;

	while (listPtr != NULL)
	{
		tempPtr = listPtr;
		listPtr = listPtr->next;
		delete tempPtr;
	}
}



int main ()
{
	ifstream InData;
	InData.open ("int.dat");
	while (InData)
	{ List.Insert(InData); }
    int length = List.Length();
	cout << "There are " << length << " many items in this list." << endl;
	List.Print();

	system ("pause");
	return 0;
}

Recommended Answers

All 5 Replies

Your else is outside your while loop .. it might help to format your code more cleanly

while (location != NULL){ 
  if (item == listPtr->item ){ 
    return true; 
  } 
} // while ends 
// now we have an else without an if 
// statement 
else{ 
  return false; 
}

Yeah, I realized that. Now I have another issue of more parse errors later in my program. I thought that was causing the rest, like a missing semi-colon or something. But apparently not.

All the errors are happening in the main function at the very end. The first is inside the while statement.

// This is the implementation file for class List

#include <iostream>
#include <fstream>
#include <stddef.h>		// to access NULL
#include "List.h"
using namespace std;

typedef NodeType* NodePtr;

struct NodeType
{
    ItemType item;
    NodePtr  next;
};

List::List()
// Post:  listPtr is set to NULL.
{
    listPtr = NULL;
}

//***********************************************************
List::List(const List&  otherList)
// Copy-constructor for List.
{
    NodeType* ptr1;
	NodeType* ptr2;

	if (otherList.listPtr == NULL)
		{ listPtr = NULL; }
	else
	{
		listPtr = new NodeType;
		listPtr->item = otherList.listPtr->item;
		ptr1 = otherList.listPtr->next;
		ptr2 = listPtr;
		while (ptr1 != NULL)
		{ ptr2->next = new NodeType;
		  ptr2 = ptr2->next;
		  ptr2->item = ptr1->item;
		  ptr1 = ptr1->next;
		}
	ptr2->next = NULL;
	}
}

//***********************************************************
bool  List::IsThere(ItemType  item) const
// Post: If item is in the list IsThere is
//       True; False, otherwise.
{
	NodeType* location = listPtr;

    while (location != NULL)
          { if (item == listPtr->item )
               { return true; } }
	return false;
}

//***********************************************************
void  List::Insert(ItemType  item)
// Pre:  item is not already in the list.
// Post: item is the first item in the list.
{
	NodeType* location;

	if (IsThere(item) == false)
	{ location = new NodeType;
	  location->item = item;
	  location->next = listPtr;
	  listPtr = location;
	}
}

//***********************************************************
void  List::Delete(ItemType  item)
// Pre:  item is in the list.
// Post: item is no longer in the list.
{
	NodeType* location = listPtr;
	NodeType* tempLocation;

	if (IsThere(item) == true)
		{ tempLocation = location->next;
		  listPtr = listPtr->next;
		}
	else
	    {
		  while (IsThere(item) == true)
			     { location = location->next; }

		  tempLocation = location->next;
		  location->next = (location->next)->next;
	    }
	delete tempLocation;
}

//***********************************************************
void  List::Print() const
// Post: Items on the list are printed on the screen.
{
	NodeType* location = listPtr;

	while (location != NULL)
		  { cout << location->item << endl;
	        location = location->next; }
}

//***********************************************************
int  List::Length() const
// Post: Number of items have been counted; result returned.
{
    NodeType* location = listPtr;
	int length = 0;

	while (location != NULL)
		  { location = location->next;
			length++; }
	return length;
}

//***********************************************************
List::~List()
// Post: All the components are deleted.
{
    NodeType* tempPtr;

	while (listPtr != NULL)
	{
		tempPtr = listPtr;
		listPtr = listPtr->next;
		delete tempPtr;
	}
}


int main ()
{
	ifstream InData;
	InData.open ("int.dat");
	while (InData)
	{ List.Insert(InData); }
    int length = List.Length();
	cout << "There are " << length << " many items in this list." << endl;
	List.Print();

	system ("pause");
	return 0;
}

why are you passing an argument of type ifstream to List.insert() when it obviously expects an argument of the type "itemType" ?

I understand you have your items in a file, but do you expect the file to read itself ? You need to write some code to read the contents of the file and then pass the correct arguments to the insert function.

Okay, that's how I was told to write that part of the code so that's what I did. So, how should the code look then? I'm lost.

ok I am not sure what your itemType is so lets assume its a basic data type like int or string and your file has those in it.

so

itemType element; 
  
  ifstream InData;
  InData.open ("int.dat");
  if (inData.isopen()){
    while (InData >> element){    
      List.Insert(element); 
    }
  }
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.