Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Errors with expected primary-expressions
    primarykey
    data
    text
    <p>There are errors that I am getting with my .cpp code that I just cannot figure out. I believe it is probably a miss-matched bracket or something, but I don't see it. Here is the code and the errors:</p> <p>Errors:</p> <pre><code>C:\Users\Tyler\Desktop\Program 2\p2.cpp:202: error: expected primary-expression before "else" C:\Users\Tyler\Desktop\Program 2\p2.cpp:202: error: expected `;' before "else" C:\Users\Tyler\Desktop\Program 2\p2.cpp:222: error: expected `while' before '(' token C:\Users\Tyler\Desktop\Program 2\p2.cpp:223: error: expected `;' before '{' token C:\Users\Tyler\Desktop\Program 2\p2.cpp:236: error: expected primary-expression before "else" C:\Users\Tyler\Desktop\Program 2\p2.cpp:236: error: expected `;' before "else" C:\Users\Tyler\Desktop\Program 2\p2.cpp: At global scope: C:\Users\Tyler\Desktop\Program 2\p2.cpp:250: error: expected unqualified-id before "while" C:\Users\Tyler\Desktop\Program 2\p2.cpp:250: error: expected `,' or `;' before "while" C:\Users\Tyler\Desktop\Program 2\p2.cpp:251: error: expected unqualified-id before "return" C:\Users\Tyler\Desktop\Program 2\p2.cpp:251: error: expected `,' or `;' before "return" C:\Users\Tyler\Desktop\Program 2\p2.cpp:252: error: expected declaration before '}' token </code></pre> <p>Code:</p> <pre><code>int main() { Stack s; //variable declarations StackItem *newItem; char token, nextChar, prevChar, response, check; int lineCount, apostCount; char filename[50]; bool insideComment = false, insideString = false, error, isMatch, delimError = false; fstream sourceFile; do //do while response is y { do //do while opening the source file fails { cout &lt;&lt; "Enter filename of source file: "; cin.getline (filename,51); sourceFile.open(filename); //opens the file with given filename if (sourceFile.fail()) cout &lt;&lt; "File could not be opened" &lt;&lt; endl; //error if can't open sourceFile.clear(); } while (sourceFile.fail()); //exits if source file doesn't fail sourceFile.clear(); lineCount = 0; //initializes line count to zero while (!sourceFile.eof()) //exits if end of source file is reached { sourceFile.get(nextChar); //gets the next character in file if (!sourceFile.eof()) //if not the end of source file { if ((int)nextChar == '\n') //if next character is an end line { lineCount++; //increments line count cout &lt;&lt; "Line count: " &lt;&lt; lineCount &lt;&lt; endl; //echoes line count } else if (nextChar == '{' || nextChar == '[' || nextChar == '(') { //continues if next char is an opening token newItem-&gt;token = nextChar; //sets newItem's token newItem-&gt;lineNumber = lineCount; //sets newItem's line count if (!s.isFull()) //continues if stack isn't full s.push(newItem); //pushes newItem onto the stack s.displayStack(); //displays the stack } else if (nextChar == '}' || nextChar == ']' || nextChar == ')') { //continues if next char is a closing token if (!s.isEmpty()) //continues if stack isn't empty { isMatch = s.matchStack(nextChar); //checks if token matches top if (isMatch == true) //if true, pops the top of the stack s.pop(); else //continues if match is false { newItem-&gt;token = nextChar; //sets newItem's token newItem-&gt;lineNumber = lineCount; //sets newITem's line count s.trailingItem(newItem); //calls the trailingItem function cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Process another file? (y/n): "; cin &gt;&gt; response; //asks user whether to continue while (response != 'y' &amp;&amp; response != 'n') //checks for input error { cout &lt;&lt; "Error! Must enter either y for yes or n for no." &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Process another file? (y/n): "; cin &gt;&gt; response; } break; //leaves the while loop } } else if (s.isEmpty()) //continues if stack is empty { newItem-&gt;token = nextChar; //sets newItem's token newItem-&gt;lineNumber = lineCount; //sets newItem's line count s.push(newItem); //pushes newItem onto the top of the stack s.processStack(); //processes the error cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Process another file? (y/n): "; cin &gt;&gt; response; //asks user whether to continue while (response != 'y' &amp;&amp; response != 'n') //checks for input error { cout &lt;&lt; "Error! Must enter either y for yes or n for no." &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Process another file? (y/n): "; cin &gt;&gt; response; //gets user input } break; //leaves the while loop } s.displayStack(); //displays the items on the stack } else if (nextChar == '/') //continues if next char is a / { sourceFile.get(nextChar); //gets the next character in file if (nextChar == '*') //continues if next char is a * { //at this point a comment has started because of / then * insideComment = true; //sets bool to true newItem-&gt;token = 'c'; //sets newItem's token to c for "comment" newItem-&gt;lineNumber = lineCount; //sets newItem's line count s.push(newItem); //pushes the item onto the stack while (insideComment == true &amp;&amp; !sourceFile.eof()) { //continues while inside a comment and end of file isn't reached sourceFile.get(nextChar); //gets the next character in file if (nextChar == '*') //continues if next char is * { sourceFile.get(nextChar); //gets the next character in file if (nextChar == '/') //continues if next char is / { //at this point the comment has ended because of * then / insideComment = false; //sets bool to false s.pop(); //pops off the c in the stack } } } } else if (nextChar == '/') //continues if next char is a / { //at this point a comment has started because of / then another / insideComment = true; //sets bool to true while (insideComment == true &amp;&amp; !sourceFile.eof()) { //continues while inside a comment and not at end of file sourceFile.get(nextChar); //gets the next character in file if (nextChar == '\n') //leaves comment if next line is entered insideComment = false; //sets bool to false } } } else if (nextChar == '\n') //continues if next char is an end line { lineCount++; //increments line count cout &lt;&lt; "Line count: " &lt;&lt; lineCount &lt;&lt; endl; //echoes line count } else if (nextChar == '\"' &amp;&amp; insideComment == false) { //continues if next char is a " and is not currently inside a comment insideString = true; //sets bool to true b/c " starts a string while (insideString == true &amp;&amp; !sourceFile.eof()) { sourceFile.get(nextChar); //gets the next character in file if (nextChar == '\"') insideString = false; else if (nextChar == '\n') { lineCount++; //increments line count cout &lt;&lt; "Line count: " &lt;&lt; lineCount &lt;&lt; endl; //echoes line count } } } else if (nextChar == '\'' &amp;&amp; insideComment == false &amp;&amp; insideString == false) { //continues if next char is a ' and not in a comment or string apostCount = 1; //initializes apostrophe count (chars after first apostrophe) while (nextChar != '\'' &amp;&amp; !sourceFile.eof()) { //continues while next char isn't another ' and not end of file sourceFile.get(nextChar); //gets the next character in file apostCount++; //increments apostrophe count if (nextChar == '\\') //continues if next char is a \ { sourceFile.get(nextChar); //gets the next character in file apostCount++; //increments apostrophe count if (nextChar == '\'' &amp;&amp; apostCount &lt;= 3) //must be less than/= 3 spaces nextChar = ' '; //sets next char to space if another apostrophe } else if (nextChar == '\n') //continues if next char is an end line { lineCount++; //increments line count cout &lt;&lt; "Line count: " &lt;&lt; lineCount &lt;&lt; endl; //echoes line count } } if (apostCount &gt;= 4) //if more than 3 chars come after apostrophe { //displays delimeter error cout &lt;&lt; "Character delimeter error on line " &lt;&lt; lineCount &lt;&lt; endl; delimError = true; break; //leaves while loop after displaying error } if (delimError == true) //continues if delimiter error occured break; //leaves 2nd while loop } } } //end of while loop if (s.isEmpty() == false) //continues if no errors within stack { //displays successful nesting structure to user cout &lt;&lt; "Source file of " &lt;&lt; lineCount &lt;&lt; " lines has proper nesting structure"; cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Process another file? (y/n): "; //asks to continue cin &gt;&gt; response; //gets user's response while (response != 'y' &amp;&amp; response != 'n') //checks for input error { cout &lt;&lt; "Error! Must enter either y for yes or n for no." &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Process another file? (y/n): "; cin &gt;&gt; response; //gets user input } } else //continues if errors are present { s.processStack(); //processes and displays the delimiter errors cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Process another file? (y/n): "; //asks to continue cin &gt;&gt; response; //gets user's response while (response != 'y' &amp;&amp; response != 'n') //checks for input error { cout &lt;&lt; "Error! Must enter either y for yes or n for no." &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Process another file? (y/n): "; cin &gt;&gt; response; //gets user input } } } while (response == 'y'); //opens another file if user entered y for yes return 0; //quits program } </code></pre> <p>Please Help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload