I am trying to call a void function named correction(original,corrected) and when I'm trying to compile it I get a couple of errors of the form:
error: expected primary-expression before ‘}’ token
Here is my complete code:
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void correction(ifstream& original,ofstream& corrected);
int main ()
{
ifstream original;
ofstream corrected;
char filename[16];
cout<<"Enter filename:\n";
cin>>filename;
original.open(filename);
corrected.open("corrected.txt");
if (original.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
if (corrected.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
else
{
correction(original,corrected);
}
return 0;
}
void correction(ifstream& original,ofstream& corrected)
{
char symbol;
while(! original.eof())
{
original.get(symbol);
if (symbol== 'c')
{
corrected.put(symbol);
original.get(symbol);
if (symbol=='i')
{
corrected.put(symbol);
original.get(symbol);
if (symbol=='n')
{
corrected.put(symbol);
original.get(symbol);
while (symbol==' ')
{
original.get(symbol);
}
for (symbol=='<')
{
corrected.put('>');
original.get(symbol);
}
corrected.put(symbol);
}
}
if (symbol=='o')
{
corrected.put(symbol);
original.get(symbol);
if (symbol=='u')
{
corrected.put(symbol);
original.get(symbol);
if (symbol=='t')
{
corrected.put(symbol);
original.get(symbol);
while (symbol==' ')
{
original.get(symbol);
}
for (symbol=='>')
{
corrected.put('<');
original.get(symbol);
}
corrected.put(symbol);
}
}
}
}
else
corrected.put(symbol);
}
return;
}
When I try to compile this code I get the following errors:
operators.cpp:59:23: error: expected ‘;’ before ‘)’ token
for (symbol=='<')
^ operators.cpp:65:5: error: expected primary-expression before ‘}’ token
}
^ operators.cpp:65:5: error: expected ‘)’ before ‘}’ token > >operators.cpp:65:5: error: expected primary-expression before ‘}’
token
operators.cpp:65:5: error: expected ‘;’ before ‘}’ token
operators.cpp:83:20: error: expected ‘;’ before ‘)’ token
for (symbol=='>')
^ operators.cpp:89:9: error: expected primary-expression before ‘}’ token
}
^ operators.cpp:89:9: error: expected ‘)’ before ‘}’ token operators.cpp:89:9: error: expected primary-expression before ‘}’
token operators.cpp:89:9: error: expected ‘;’ before ‘}’ token
I am kind of new to programming and have spent a couple of hours trying to fix it but I don't understand what the problem is.
Any kind of help is greatly appreciated!
Added later after looking closely at #liup's answer:
Ouch, my bad, the OP probably indeed meant to use a loop here! Then, you can use the while loop like so:
while (symbol=='<')
and
while (symbol=='>')
Old answer:
Here:
if (symbol=='<')
and here:
if (symbol=='>')
Instead of for (symbol=='<') and for (symbol=='>').
The error stems from the fact that in a for loop, you must have all the three clauses, even if they are empty, hence two semicolons ; are obligatory. You only provided one didn't provide a single semicolon in each of the two incorrect for statements.
For reference, see for loop description at cppreference
Both for (symbol=='<') and for (symbol=='>') for loop syntax is wrong. In this scenario you can use for loop as follows.
for (; symbol == '<';)
{
corrected.put('>');
original.get(symbol);
}
for (; symbol == '>';)
{
corrected.put('<');
original.get(symbol);
}
OR
for (; symbol == '<';original.get(symbol))
{
corrected.put('>');
}
for (; symbol == '>';original.get(symbol))
{
corrected.put('<');
}
Related
I am implementing calculator using flex and bison, but double values are interpreted as integers, then I was looking for answers on the internet and I realized the mistake was probably that bison interprets numbers as integers, so if I have input "1.2" in *yylval = atof(yytext) is 1 instead of 1.2. So I tried adding #define YYSTYPE double in parser.ypp but I'm getting compiling errors.
I know there are several similar questions, but none of them really helped me.
Here are my lex and ypp files and compiling errors I get.
Thanks in advance.
lexer.lex:
%option noyywrap
%option noinput
%option nounput
%{
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include "parser.tab.hpp"
%}
%%
[0-9]+([.][0-9]+)? {
*yylval = atof(yytext);
return NUMBER;
}
sin {
return SIN;
}
cos {
return COS;
}
tan {
return TG;
}
tg {
return TG;
}
ctan {
return CTG;
}
ctg {
return CTG;
}
asin {
return ASIN;
}
acos {
return ACOS;
}
atan {
return ATG;
}
actan {
return ACTG;
}
ln {
return LN;
}
log {
return LOG;
}
exp {
return EXP;
}
sqrt {
return SQRT;
}
abs {
return ABS;
}
mod {
return MOD;
}
[a-z] {
return VARIABLE;
}
[-+*/^()%!,] {
return *yytext;
}
[ \t\n] ;
. {
}
parser.ypp:
%{
#define YYSTYPE double
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
bool indicator_calculating_value ;
extern int yylex();
void yyerror(double *return_value, std::string s);
%}
%parse-param { double *return_value}
%left '+' '-'
%left '*' '/' '%'
%left SIN COS TG CTG ASIN ACOS ATG ACTG LN LOG MOD
%left UMINUS
%left '^' EXP SQRT
%left ABS
%left '!'
%type <double> E
%token <double> NUMBER
%token <char> VARIABLE;
%start pocetak
%%
pocetak
: E {
*return_value = $1;
};
E
: E '+' E {
if (indicator_calculating_value) {
$$ = $1 + $3;
}
}
| E '*' E {
if (indicator_calculating_value) {
$$ = $1 * $3;
}
}
| E '-' E {
if (indicator_calculating_value) {
$$ = $1 - $3;
}
}
| E '/' E {
if(indicator_calculating_value) {
if ($3 == 0) {
yyerror(0, "divide by zero");
}
$$ = $1 / $3;
}
}
| MOD '(' E ',' E ')' {
if(indicator_calculating_value) {
if ($5 == 0) {
yyerror(0, "divide by zero");
}
$$ = static_cast<int>($3) % static_cast<int>($5);
}
}
| SIN '(' E ')' {
if(indicator_calculating_value) {
$$ = sin($3);
}
}
| COS '(' E ')' {
if(indicator_calculating_value) {
$$ = cos($3);
}
}
| TG '(' E ')' {
if(indicator_calculating_value) {
$$ = tan($3);
}
}
| CTG '(' E ')' {
if(indicator_calculating_value) {
$$ = 1 / tan($3);
}
}
| ASIN '(' E ')' {
if(indicator_calculating_value) {
$$ = asin($3);
}
}
| ACOS '(' E ')' {
if(indicator_calculating_value) {
$$ = acos($3);
}
}
| ATG '(' E ')' {
if(indicator_calculating_value) {
$$ = atan($3);
}
}
| ACTG '(' E ')' {
if(indicator_calculating_value) {
$$ = 1 / atan($3);
}
}
| LN '(' E ')' {
if(indicator_calculating_value) {
$$ = log($3);
}
}
| LOG '(' E ',' E ')' {
if(indicator_calculating_value) {
$$ = log($5) / log($3);
}
}
| EXP '(' E ')' {
if(indicator_calculating_value) {
$$ = exp($3);
}
}
| SQRT '(' E ')' {
if(indicator_calculating_value) {
$$ = sqrt($3);
}
}
| E '^' E {
if(indicator_calculating_value) {
$$ = pow($1, $3);
}
}
| '-' E %prec UMINUS {
if(indicator_calculating_value) {
$$ = -$2;
}
}
| ABS '(' E ')' {
if(indicator_calculating_value) {
$$ = fabs($3);
}
}
| E '!' {
if(indicator_calculating_value) {
$$ = 1;
for (int i = 1; i <= static_cast<int>($1); i++) {
$$ = $$ * i;
}
}
}
| '(' E ')' {
if(indicator_calculating_value) {
$$ = $2;
}
}
| NUMBER {
if(indicator_calculating_value) {
$$ = $1;
}
}
| VARIABLE {
}
;
%%
void yyerror(double *return_value, std::string s)
{
std::cout << s << std::endl;
}
int main() {
indicator_calculating_value = true;
double value = 0.0;
yyparse(&value);
std::cout << value << std::endl;
return 0;
}
errors:
make
bison -d -v parser.ypp
g++ -Wall -L/usr/local/lib -lmgl-qt5 -lmgl -lm -c -o parser.tab.o parser.tab.cpp
parser.ypp: In function ‘int yyparse(double*)’:
parser.ypp:42:34: error: expected unqualified-id before ‘double’
*return_value = $1;
^
parser.ypp:42:34: error: expected ‘)’ before ‘double’
parser.ypp:49:20: error: expected unqualified-id before ‘double’
$$ = $1 + $3;
^~~~~~
parser.ypp:49:20: error: expected ‘)’ before ‘double’
parser.ypp:55:20: error: expected unqualified-id before ‘double’
$$ = $1 * $3;
^~~~~~
parser.ypp:55:20: error: expected ‘)’ before ‘double’
parser.ypp:60:20: error: expected unqualified-id before ‘double’
$$ = $1 - $3;
^~~~~~
parser.ypp:60:20: error: expected ‘)’ before ‘double’
parser.ypp:66:27: error: expected unqualified-id before ‘double’
if ($3 == 0) {
^
parser.ypp:66:27: error: expected ‘)’ before ‘double’
parser.ypp:69:20: error: expected unqualified-id before ‘double’
$$ = $1 / $3;
^~~~~~
parser.ypp:69:41: error: expected unqualified-id before ‘double’
$$ = $1 / $3;
^
parser.ypp:69:41: error: expected ‘)’ before ‘double’
parser.ypp:69:68: error: expected ‘)’ before ‘;’ token
$$ = $1 / $3;
^
parser.ypp:74:28: error: expected unqualified-id before ‘double’
if ($5 == 0) {
^
parser.ypp:74:28: error: expected ‘)’ before ‘double’
parser.ypp:77:20: error: expected unqualified-id before ‘double’
$$ = static_cast<int>($3) % static_cast<int>($5);
^~~~~~
parser.ypp:77:58: error: expected unqualified-id before ‘double’
$$ = static_cast<int>($3) % static_cast<int>($5);
^~~~
parser.ypp:77:58: error: expected ‘)’ before ‘double’
parser.ypp:77:105: error: expected ‘)’ before ‘;’ token
$$ = static_cast<int>($3) % static_cast<int>($5);
^
parser.ypp:77:105: error: expected ‘)’ before ‘;’ token
parser.ypp:82:20: error: expected unqualified-id before ‘double’
$$ = sin($3);
^~~~~~
parser.ypp:82:20: error: expected ‘)’ before ‘double’
parser.ypp:87:20: error: expected unqualified-id before ‘double’
$$ = cos($3);
^~~~~~
parser.ypp:87:20: error: expected ‘)’ before ‘double’
parser.ypp:92:20: error: expected unqualified-id before ‘double’
$$ = tan($3);
^~~~~~
parser.ypp:92:20: error: expected ‘)’ before ‘double’
parser.ypp:97:20: error: expected unqualified-id before ‘double’
$$ = 1 / tan($3);
^~~~~~
parser.ypp:97:20: error: expected ‘)’ before ‘double’
parser.ypp:102:20: error: expected unqualified-id before ‘double’
$$ = asin($3);
^~~~~~
parser.ypp:102:20: error: expected ‘)’ before ‘double’
parser.ypp:107:20: error: expected unqualified-id before ‘double’
$$ = acos($3);
^~~~~~
parser.ypp:107:20: error: expected ‘)’ before ‘double’
parser.ypp:112:20: error: expected unqualified-id before ‘double’
$$ = atan($3);
^~~~~~
parser.ypp:112:20: error: expected ‘)’ before ‘double’
parser.ypp:117:20: error: expected unqualified-id before ‘double’
$$ = 1 / atan($3);
^~~~~~
parser.ypp:117:20: error: expected ‘)’ before ‘double’
parser.ypp:122:20: error: expected unqualified-id before ‘double’
$$ = log($3);
^~~~~~
parser.ypp:122:20: error: expected ‘)’ before ‘double’
parser.ypp:127:20: error: expected unqualified-id before ‘double’
$$ = log($5) / log($3);
^~~~~~
parser.ypp:127:20: error: expected ‘)’ before ‘double’
parser.ypp:132:20: error: expected unqualified-id before ‘double’
$$ = exp($3);
^~~~~~
parser.ypp:132:20: error: expected ‘)’ before ‘double’
parser.ypp:137:20: error: expected unqualified-id before ‘double’
$$ = sqrt($3);
^~~~~~
parser.ypp:137:20: error: expected ‘)’ before ‘double’
parser.ypp:142:20: error: expected unqualified-id before ‘double’
$$ = pow($1, $3);
^~~~~~
parser.ypp:142:20: error: expected ‘)’ before ‘double’
parser.ypp:147:20: error: expected unqualified-id before ‘double’
$$ = -$2;
^~
parser.ypp:147:20: error: expected ‘)’ before ‘double’
parser.ypp:152:20: error: expected unqualified-id before ‘double’
$$ = fabs($3);
^~~~~~
parser.ypp:152:20: error: expected ‘)’ before ‘double’
parser.ypp:157:20: error: expected unqualified-id before ‘double’
$$ = 1;
^
parser.ypp:157:20: error: expected ‘)’ before ‘double’
parser.ypp:158:62: error: expected unqualified-id before ‘double’
for (int i = 1; i <= static_cast<int>($1); i++) {
^
parser.ypp:158:62: error: expected ‘)’ before ‘double’
parser.ypp:158:70: error: expected ‘)’ before ‘;’ token
for (int i = 1; i <= static_cast<int>($1); i++) {
^
parser.ypp:159:24: error: expected unqualified-id before ‘double’
$$ = $$ * i;
^~~~~
parser.ypp:159:24: error: expected ‘)’ before ‘double’
parser.ypp:165:20: error: expected unqualified-id before ‘double’
$$ = $2;
^
parser.ypp:165:20: error: expected ‘)’ before ‘double’
parser.ypp:170:20: error: expected unqualified-id before ‘double’
$$ = $1;
^
parser.ypp:170:20: error: expected ‘)’ before ‘double’
Makefile:15: recipe for target 'parser.tab.o' failed
make: *** [parser.tab.o] Error 1
Your bison file defines symbols wirh two different types:
%type <double> E
%token <double> NUMBER
%token <char> VARIABLE;
(The semicolon at the end of the third line there is incorrect, although I think bison will just ignore it.)
As discussed in the bison manual, the most common way of accomplishing that is with a %union bison declaration, which will declare YYSTYPE to be a C union. That's not compatible with using the preprocessor to #define YYSTYPE as a single type.
If you use a %union declaration, you specify a C type and a tag for each variant of YYSTYPE. What you then place between the angle brackets is the tag, not the type. For example,
%union {
double number;
char id;
}
%token <number> NUMBER
With that declaration, bison will declare a union type with two members named number and id. And it will automatically make $n refer to the correct member (if the symbol $n refers to has a known type) by appending a member selector. In other words, if $1 refers to a NUMBER, bison will replace it in the generated code with something like stack[top - 2 + 1].number. (Without the %union and type declaration, the replacement would have been something like stack[top - 2 + 1].)
Bison doesn't require you to use a %union. If you don't and you still declare types for your tokens and non-terminals, bison will assume you know what you're doing and that you have somehow declared YYSTYPE as a composite type. It will do exactly the same replacement, adding a member selector to the references to stack values with known types.
So in your case, you've declared %type <double> E, so bison will replace a $1 in an action where the $1 refers to the symbol E with something like stack[top - 4 + 1].double.
Of course, that will cause the compiler to choke. double is a reserved word and cannot be used as a composite member name, and anyway YYSTYPE is a scalar (a double) because that's the way you defined it.
You haven't yet tried to compile your scanner, but you will encounter similar problems there. Obviously, YYSTYPE must be the same type in both the scanner and the parser, which are separate C translation units. If you had used a %union declaration, bison would have put the C union into tje generated header, which you #include in the generated scanner. That guarantees consistency. If you don't use %union, then you have to make sure that the scanner has the right definition.
Since you don't do anything in your flex file to declare YYSTYPE, the generated code will fallback to int. It does this by including a block like this:
#ifndef YYSTYPE
#define YYSTYPE int
#endif
The bison manual contains lots of simple examples. There are a lot more options than I have presented here, and you would be well-advised to spend a bit of time reading the manual.
This is the error and program. I gives me this error, and I cant find any mispelling in main.cpp
ERROR
main.cpp: In member function ‘int GPIO::unexport_gpio()’:
main.cpp:27:45: error: conversion from ‘const char*’ to non-scalar type ‘std::ofstream {aka std::basic_ofstream<char>}’ requested
ofstream unexportgpio = (unexport_str.c_str());
~~~~~~~~~~~~~~~~~~~^~~
main.cpp:28:7: error: ‘exportgpio’ was not declared in this scope
if(!(exportgpio.is_open())
^~~~~~~~~~
main.cpp:29:3: error: expected ‘)’ before ‘return’
return -1;
^~~~~~
main.cpp:30:2: error: ‘exportgpio’ was not declared in this scope
exportgpio<<this->gpionum;
^~~~~~~~~~
main.cpp: In member function ‘int GPIO::setdir_gpio(std::__cxx11::string)’:
main.cpp:36:64: warning: statement has no effect [-Wunused-value]
std::string setdir_str = "/sys/class/gpio/gpio"+this->gpionum;+"/direction";
^~~~~~~~~~~~~
main.cpp:37:41: error: conversion from ‘const char*’ to non-scalar type ‘std::ofstream {aka std::basic_ofstream<char>}’ requested
ofstream setdirgpio = (setdir_str.c_str());
~~~~~~~~~~~~~~~~~^~~
main.cpp: At global scope:
main.cpp:48:2: error: ‘setdirgpio’ does not name a type
setdirgpio.close();
^~~~~~~~~~
main.cpp:49:2: error: expected unqualified-id before ‘return’
return 0;
^~~~~~
main.cpp:50:1: error: expected declaration before ‘}’ token
}
^
main.cpp: In member function ‘int GPIO::setdir_gpio(std::__cxx11::string)’:
main.cpp:47:2: warning: control reaches end of non-void function [-Wreturn-type]
}
^
main.cpp
#include "main.h"
GPIO::GPIO()
{
this->gpionum = "4";
}
GPIO::GPIO(std::string gnum)
{
this->gpionum = gnum;
}
int GPIO::export_gpio()
{
string export_str = "/sys/class/gpio/export";
ofstream exportgpio(export_str.c_str());
if(!(exportgpio.is_open()))
return -1;
exportgpio << this->gpionum;
exportgpio.close();
return 0;
}
int GPIO::unexport_gpio()
{
std::string unexport_str = "/sys/class/gpio/unexport";
ofstream unexportgpio = (unexport_str.c_str());
if(!(exportgpio.is_open())
return -1;
exportgpio<<this->gpionum;
return 0;
}
int GPIO::setdir_gpio(std::string x)
{
std::string setdir_str = "/sys/class/gpio/gpio"+this->gpionum;+"/direction";
ofstream setdirgpio = (setdir_str.c_str());
if(!(setdirgpio.is_open()))
return -1;
if(x == "0")
{
setdirgpio<<"in";
}
if(x == "1")
setdirgpio<<"out";
}
setdirgpio.close();
return 0;
}
int GPIO::setval_gpio(std::string x)
{
string setval_str = "/sys/class/gpio/gpio"+this->gpionum+"/value";
ofstream setvalgpio = (setval_str.c_str());
if(!(setvalgpio.is_open()))
return -1;
if(x == "0")
{
setvalgpio<<"0";
setvalgpio.close();
}
if(x == "1")
setvalgpio<<"1";
setvalgpio.close();
}
return 0;
}
Im trying to make a program that switches the state on my RPI3.
Im used to it on the Arduino, but not on RPI3.
If you know how to fix, then don't hesitate to write.
It is supposed to drive my garten sprinkler
ofstream unexportgpio = (unexport_str.c_str());
should be
ofstream unexportgpio(unexport_str.c_str());
Incidentally this is an odd piece of code
std::string unexport_str = "/sys/class/gpio/unexport";
ofstream unexportgpio(unexport_str.c_str());
Why not do it the simpler way?
ofstream unexportgpio("/sys/class/gpio/unexport");
There's no need to make a std::string from a C string literal, only to get the C string back again.
I am trying to create a function that looks at a '-' sign and checks whether is a minus sign or a negative sign based on if it has a ' ' (space) in front of it. I am doing so by comparing the current char I have (infix[x]) and comparing to infix[x+1]; I keep getting error but im unsure if its because im not passing correctly or something else?
for(unsigned x = 0; x < infix.length(); ++x)
{
// place numbers (standard, decimal, & negative)
// numbers onto the 'postfix' string
if((isdigit(infix[x])) || (infix[x] == '.'))
{
postfix += infix[x];
}
else if ((infix[x] == '-'))
{
if(checkfornegative(infix[x], infix)== 1)) // error: expected primary-expression before ‘)’ token
if(checkfornegative(infix[x], infix)== 1))
{
postfix+= " ";
}
else if(checkfornegative(infix[x], infix)== 0)) //error: expected primary-expression before ‘)’ token
if(checkfornegative(infix[x], infix)== 1))
{
postfix += infix[x];
}
}
// This is the function in using
bool checkfornegative(char C, string& QQ)
{
bool status;
if((C == '-') && (QQ[C+1] == ' '))
{
status = true;
}
else if((C == '-') && (QQ[C+1] != ' '))
{
status = false;
}
return status;
}
You are getting compilation errors because you have extra closing parenthesis in the if conditions.
if(checkfornegative(infix[x], infix)== 1)) // error: expected primary-expression before ‘)’ token
Remove the last closing parenthesis from the condition. Same goes for the second condition also.
However, there are several issues in your code but they are not compilation errors.
Im trying to use an online marker for a c++ programming site. The code runs fine on my computer. However, the site is repeatedly having a compilation error, spitting out these syntax errors:
/data/grader/2/81322/compile/source.cc: In function �int main()�:
/data/grader/2/81322/compile/source.cc:16: error: expected initializer before �:� token
/data/grader/2/81322/compile/source.cc:45: error: expected primary-expression at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected �;� at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected primary-expression at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected �)� at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected statement at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected �}� at end of input
/data/grader/2/81322/compile/source.cc:45: error: expected �}� at end of input
#include <iostream>
#include <string>
using namespace std;
int main()
{
char vowels[] = { 'a', 'e', 'i', 'o', 'u' };
string word;
string newWord;
getline(cin, word);
for (int i = 0; i < (int)word.length(); i++) {
bool vowel = false;
char closest = 'z';
int close = 200;
for (char &n : vowels) {
if (n == word[i]) {
vowel = true;
break;
}
else if (abs(n-word[i]) < close) {
closest = n;
close = abs(n - word[i]);
}
}
newWord += word[i];
if (!vowel) {
newWord += closest;
if (word[i] == 'z') {
newWord += 'z';
break;
}
char next = word[i]+1;
for (char n : vowels) {
if (n == next) {
vowel = true;
break;
}
}
newWord += ((vowel) ? word[i] + 2:next);
}
}
cout << newWord << endl;
//system("pause");//remove
}
Being new to C++, I cant seem to make sense of this.
Thanks in advance.
I get the following error when I compile:
convert.cpp: In function 'std::string convert()':
convert.cpp:62: error: expected primary-expression before 'else'
convert.cpp:62: error: expected `;' before 'else'
convert.cpp:108: error: expected `}' at end of input
convert.cpp:108: error: expected `}' at end of input
convert.cpp:108: error: expected `}' at end of input
convert.cpp:108: error: expected `}' at end of input
convert.cpp:108: error: expected `}' at end of input
Code:
#include<iostream>
#include<stack>
#include<string>
using namespace std;
string infix; // infix expression string
string operand;
string operate;
stack <string> mystack; // this stack is used to push the operator
string postfix; // postfix string where the operand is appended
//....................................................................
// this function read the infix expression from user
string input()
{
cout<<" Enter the damn infix expression: "<<endl;
getline(cin, infix);
return infix;
}
//......................................................................
// this function checks for operator precedence in the stack
int precedence(string e)
{
int f;
if(e == "*" || e== "/" || e =="%")
f = 2;
else
{
if(e == "+" || e == "-")
f = 1;
}
if(e=="."){
f=0;
}
return f;
}
//....................................................................
// This function converts infix to postfix
string convert()
{
for(int i=0; i<infix.length(); i++)
{
switch(infix[i]){
// operate case start
case '+': case'-': case'*': case'/': case'^': case '(': case ')': case'.':
operate=infix[i];
{
if(mystack.empty() || precedence(operate)>= precedence(mystack.top()))
{
mystack.push(operate);
else
{
while(precedence(operate)<= precedence(mystack.top()))
{
postfix.append(mystack.top());
mystack.pop();
}
mystack.push(operate);
}
}
}//operate case closed
default: //when operand string char is parsed
{
operand=infix[i];
postfix.append(operand);
break;
} // default case closed
}//switch closed
}
while(!mystack.empty())
{
postfix.append(mystack.top())
mystack.pop();
}
return postfix;
cout<<postfix;
}
//...................................................................
int main()
{
input();
convert();
cout<<"postfix is "<<postfix<<endl;
}
It appears that your code is just missing some closing braces. That's all.
For example, look at this:
operate=infix[i];
{
if(mystack.empty() || precedence(operate)>= precedence(mystack.top()))
{
mystack.push(operate);
else
{
while(precedence(operate)<= precedence(mystack.top()))
Where is the closing } before the else statement. Just go through your code and fix these silly mistakes.
It would be a lot easier to rid yourself of these things if you made your spacing and indentation a little neater.
The "expected primary expression before else" diagnostic is the compiler-author's way of reporting you rudely foisted on him an "else" without either a preceding "if" or (what amounts to the same thing) "else if". Alexander Rafferty correctly points out that this is because the code has ...
if (condition) {
// ...
else { }
... when perhaps what you meant was ...
if (condition) {
// ...
}
else {
}
... though maybe you deleted a whole bunch of stuff by accident, and were lucky enough that the deletion resulted in unparseable code, so you will realize that something is wrong.
Look at these lines:
if(mystack.empty() || precedence(operate)>= precedence(mystack.top()))
{
mystack.push(operate);
else