Parse Error Before 'else'

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 185
Reputation: DemonGal711 is an unknown quantity at this point 
Solved Threads: 10
DemonGal711 DemonGal711 is offline Offline
Junior Poster

Parse Error Before 'else'

 
0
  #1
Oct 8th, 2008
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.

  1. // This is the implementation file for class List
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <stddef.h> // to access NULL
  6. #include "List.h"
  7. using namespace std;
  8.  
  9. typedef NodeType* NodePtr;
  10.  
  11. struct NodeType
  12. {
  13. ItemType item;
  14. NodePtr next;
  15. };
  16.  
  17. List::List()
  18. // Post: listPtr is set to NULL.
  19. {
  20. listPtr = NULL;
  21. }
  22.  
  23. //***********************************************************
  24. List::List(const List& otherList)
  25. // Copy-constructor for List.
  26. {
  27. NodeType* ptr1;
  28. NodeType* ptr2;
  29.  
  30. if (otherList.listPtr == NULL)
  31. { listPtr = NULL; }
  32. else
  33. {
  34. listPtr = new NodeType;
  35. listPtr->item = otherList.listPtr->item;
  36. ptr1 = otherList.listPtr->next;
  37. ptr2 = listPtr;
  38. while (ptr1 != NULL)
  39. { ptr2->next = new NodeType;
  40. ptr2 = ptr2->next;
  41. ptr2->item = ptr1->item;
  42. ptr1 = ptr1->next;
  43. }
  44. ptr2->next = NULL;
  45. }
  46. }
  47.  
  48. //***********************************************************
  49. bool List::IsThere(ItemType item) const
  50. // Post: If item is in the list IsThere is
  51. // True; False, otherwise.
  52. {
  53. NodeType* location = listPtr;
  54.  
  55. while (location != NULL)
  56. { if (item == listPtr->item )
  57. { return true; } }
  58. return false;
  59. }
  60.  
  61. //***********************************************************
  62. void List::Insert(ItemType item)
  63. // Pre: item is not already in the list.
  64. // Post: item is the first item in the list.
  65. {
  66. NodeType* location;
  67.  
  68. if (IsThere(item) == false)
  69. { location = new NodeType;
  70. location->item = item;
  71. location->next = listPtr;
  72. listPtr = location;
  73. }
  74. }
  75.  
  76. //***********************************************************
  77. void List::Delete(ItemType item)
  78. // Pre: item is in the list.
  79. // Post: item is no longer in the list.
  80. {
  81. NodeType* location = listPtr;
  82. NodeType* tempLocation;
  83.  
  84. if (IsThere(item) == true)
  85. { tempLocation = location->next;
  86. listPtr = listPtr->next;
  87. }
  88. else
  89. {
  90. while (IsThere(item) == true)
  91. { location = location->next; }
  92.  
  93. tempLocation = location->next;
  94. location->next = (location->next)->next;
  95. }
  96. delete tempLocation;
  97. }
  98.  
  99. //***********************************************************
  100. void List::Print() const
  101. // Post: Items on the list are printed on the screen.
  102. {
  103. NodeType* location = listPtr;
  104.  
  105. while (location != NULL)
  106. { cout << location->item << endl;
  107. location = location->next; }
  108. }
  109.  
  110. //***********************************************************
  111. int List::Length() const
  112. // Post: Number of items have been counted; result returned.
  113. {
  114. NodeType* location = listPtr;
  115. int length = 0;
  116.  
  117. while (location != NULL)
  118. { location = location->next;
  119. length++; }
  120. return length;
  121. }
  122.  
  123. //***********************************************************
  124. List::~List()
  125. // Post: All the components are deleted.
  126. {
  127. NodeType* tempPtr;
  128.  
  129. while (listPtr != NULL)
  130. {
  131. tempPtr = listPtr;
  132. listPtr = listPtr->next;
  133. delete tempPtr;
  134. }
  135. }
  136.  
  137.  
  138.  
  139. int main ()
  140. {
  141. ifstream InData;
  142. InData.open ("int.dat");
  143. while (InData)
  144. { List.Insert(InData); }
  145. int length = List.Length();
  146. cout << "There are " << length << " many items in this list." << endl;
  147. List.Print();
  148.  
  149. system ("pause");
  150. return 0;
  151. }
Last edited by DemonGal711; Oct 8th, 2008 at 1:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Parse Error Before 'else'

 
0
  #2
Oct 8th, 2008
Your else is outside your while loop .. it might help to format your code more cleanly

  1. while (location != NULL){
  2. if (item == listPtr->item ){
  3. return true;
  4. }
  5. } // while ends
  6. // now we have an else without an if
  7. // statement
  8. else{
  9. return false;
  10. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 185
Reputation: DemonGal711 is an unknown quantity at this point 
Solved Threads: 10
DemonGal711 DemonGal711 is offline Offline
Junior Poster

Re: Parse Error Before 'else'

 
0
  #3
Oct 8th, 2008
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.

  1. // This is the implementation file for class List
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <stddef.h> // to access NULL
  6. #include "List.h"
  7. using namespace std;
  8.  
  9. typedef NodeType* NodePtr;
  10.  
  11. struct NodeType
  12. {
  13. ItemType item;
  14. NodePtr next;
  15. };
  16.  
  17. List::List()
  18. // Post: listPtr is set to NULL.
  19. {
  20. listPtr = NULL;
  21. }
  22.  
  23. //***********************************************************
  24. List::List(const List& otherList)
  25. // Copy-constructor for List.
  26. {
  27. NodeType* ptr1;
  28. NodeType* ptr2;
  29.  
  30. if (otherList.listPtr == NULL)
  31. { listPtr = NULL; }
  32. else
  33. {
  34. listPtr = new NodeType;
  35. listPtr->item = otherList.listPtr->item;
  36. ptr1 = otherList.listPtr->next;
  37. ptr2 = listPtr;
  38. while (ptr1 != NULL)
  39. { ptr2->next = new NodeType;
  40. ptr2 = ptr2->next;
  41. ptr2->item = ptr1->item;
  42. ptr1 = ptr1->next;
  43. }
  44. ptr2->next = NULL;
  45. }
  46. }
  47.  
  48. //***********************************************************
  49. bool List::IsThere(ItemType item) const
  50. // Post: If item is in the list IsThere is
  51. // True; False, otherwise.
  52. {
  53. NodeType* location = listPtr;
  54.  
  55. while (location != NULL)
  56. { if (item == listPtr->item )
  57. { return true; } }
  58. return false;
  59. }
  60.  
  61. //***********************************************************
  62. void List::Insert(ItemType item)
  63. // Pre: item is not already in the list.
  64. // Post: item is the first item in the list.
  65. {
  66. NodeType* location;
  67.  
  68. if (IsThere(item) == false)
  69. { location = new NodeType;
  70. location->item = item;
  71. location->next = listPtr;
  72. listPtr = location;
  73. }
  74. }
  75.  
  76. //***********************************************************
  77. void List::Delete(ItemType item)
  78. // Pre: item is in the list.
  79. // Post: item is no longer in the list.
  80. {
  81. NodeType* location = listPtr;
  82. NodeType* tempLocation;
  83.  
  84. if (IsThere(item) == true)
  85. { tempLocation = location->next;
  86. listPtr = listPtr->next;
  87. }
  88. else
  89. {
  90. while (IsThere(item) == true)
  91. { location = location->next; }
  92.  
  93. tempLocation = location->next;
  94. location->next = (location->next)->next;
  95. }
  96. delete tempLocation;
  97. }
  98.  
  99. //***********************************************************
  100. void List::Print() const
  101. // Post: Items on the list are printed on the screen.
  102. {
  103. NodeType* location = listPtr;
  104.  
  105. while (location != NULL)
  106. { cout << location->item << endl;
  107. location = location->next; }
  108. }
  109.  
  110. //***********************************************************
  111. int List::Length() const
  112. // Post: Number of items have been counted; result returned.
  113. {
  114. NodeType* location = listPtr;
  115. int length = 0;
  116.  
  117. while (location != NULL)
  118. { location = location->next;
  119. length++; }
  120. return length;
  121. }
  122.  
  123. //***********************************************************
  124. List::~List()
  125. // Post: All the components are deleted.
  126. {
  127. NodeType* tempPtr;
  128.  
  129. while (listPtr != NULL)
  130. {
  131. tempPtr = listPtr;
  132. listPtr = listPtr->next;
  133. delete tempPtr;
  134. }
  135. }
  136.  
  137.  
  138. int main ()
  139. {
  140. ifstream InData;
  141. InData.open ("int.dat");
  142. while (InData)
  143. { List.Insert(InData); }
  144. int length = List.Length();
  145. cout << "There are " << length << " many items in this list." << endl;
  146. List.Print();
  147.  
  148. system ("pause");
  149. return 0;
  150. }
Last edited by DemonGal711; Oct 8th, 2008 at 1:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Parse Error Before 'else'

 
0
  #4
Oct 8th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 185
Reputation: DemonGal711 is an unknown quantity at this point 
Solved Threads: 10
DemonGal711 DemonGal711 is offline Offline
Junior Poster

Re: Parse Error Before 'else'

 
0
  #5
Oct 8th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Parse Error Before 'else'

 
0
  #6
Oct 8th, 2008
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
  1. itemType element;
  2.  
  3. ifstream InData;
  4. InData.open ("int.dat");
  5. if (inData.isopen()){
  6. while (InData >> element){
  7. List.Insert(element);
  8. }
  9. }
Last edited by stilllearning; Oct 8th, 2008 at 3:22 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC