Home > OS >  I can't for the life of me solve "error: expected primary-expression before ‘}’ token"
I can't for the life of me solve "error: expected primary-expression before ‘}’ token"

Time:11-07

This is occurring on line 158 (The last line). What could be causing this??

I've double checked the brackets, and those seem to be alright. I thought it could be regarding the goto statements, but those seem to be declared correctly too. Also the {} notation for cases within the switch seems to be a non-issue.

The "if (true) statement;" is also correct notation and not needing of a bracket, no?

int main(int argc, char* argv[]) {

//Preliminary Error if not enough arguments are provided
if (argc < 2) {
    cerr << "Error in program " << argv[0] << " - More than 0 input required: Incomplete range of integers";
    return -1;
}

int arg1 = 0;
int arg2 = 99;

bool firstPrint = false;
bool secondPrint = false;
bool rangeFlag = false;
bool outputDelimFlag = false;

string outputDelim = ",";

bool announceFile = false;

//**Potentially do case to do -r00-11c"example"?

//Processing for bundled options and 'r' and 'c' options with space
//Assumes any arguments for r and c are at the end of a flag.
for (int i = 1; i < argc; i  ) {
    string argument = argv[i];

    //To skip processing case of XX-YY range
    if (argument == "-r") {
        if ((argc - 1) - (i   1) < 0) {
            cerr << "Error in program " << argv[0] << "No range provided for r flag.  Program will now exit.\n";
            return -1;
        }
          i;
        continue;
    }

    //Flag is single option, defer to getopt loop
    if (argument.length() <= 2) continue;

    //Argument is a filename
    if (argument[0] != '-') continue;

    //String is potentially a bundled option, or and r or a c
    //Start processing at second character where 1st in a flag would be a '-'
    for (int j = 1; j < static_cast<int>(argument.length()); j  ) {
        switch (argument[j]) {
        case 'r': {
            //This is a single -r flag, process it in getopt.  To get here it implies that this -r has no space
            if (j == 1) goto nextArg;

            rangeFlag = true;
            //There is space
            if ((argument.length() - 1) - j == 0) {
                if ((argc - 1) - (i   1) < 0) {
                    cerr << "Error in program " << argv[0] << "No range provided for r flag.  Program will now exit.\n";
                    return -1;
                }
                istringstream iss(argv[i   1]);
                if (iss >> arg1 >> arg2) {
                    if (arg1 > arg2 || arg1 == arg2) {
                        cerr << "Error in program " << argv[0] << " - Range specified in -r flag has incorrect bounds.  Exiting program.\n";
                        return -1;
                    }
                }
                else {
                    cerr << "Error in program " << argv[0] << " - Cannot convert given input range" << argument[i   1] << " using -r option.  Exiting program.\n";
                    return -1;
                }
                  i;
                goto nextnextArg;
                break;
            }

            string rangeStr = argument.substr(j   1, argument.length() - (j   1));
            istringstream iss(rangeStr);
            if (iss >> arg1 >> arg2) {
                if (arg1 > arg2 || arg1 == arg2) {
                    cerr << "Error in program " << argv[0] << " - Range specified in -r flag has incorrect bounds.  Exiting program.\n";
                    return -1;
                }
            }
            else {
                cerr << "Error in program " << argv[0] << " - Cannot convert given input range" << rangeStr << " using -r option.  Exiting program.\n";
                return -1;
            }

            goto nextArg;
            break;
        }
        case 'f': {
            firstPrint = true;
            break;
        }
        case 's': {
            secondPrint = true;
            break;
        }
        case 'c': {
            //This is a single -c flag, process it in getopt.  To get here it implies that this -c has no space
            if (j == 1) goto nextArg;

            outputDelimFlag = true;
            //There is space
            if ((argument.length() - 1) - j == 0) {
                if ((argc - 1) - (i   1) < 0) {
                    cerr << "Error in program " << argv[0] << "No text provided for c flag.  Program will now exit.\n";
                    return -1;
                }
                outputDelim = argv[i   1];
                  i;
                goto nextnextArg;
                break;
            }

            outputDelim = argument.substr(j   1, argument.length() - (j   1));
            goto nextArg;
            break;
        }
        case 'v': {
            announceFile = true;
            break;
        }
            //Test whether we're being given a file or an incorrect argument
        default: {
            cerr << "Error in program  " << argv[0] << " - optional flag " << argument[j] << " is invalid.  Only r, f, s, c, or v are supported.  Program will now exit.\n";
            return -1;
        }
        }
    }
nextArg:
nextnextArg:
}

CodePudding user response:

You need a ; after your label. A label needs a statement after it, and since you're not providing any, putting an empty one after the label (writing nextnextArg:;) fixes the problem

CodePudding user response:

You need at least one valid statement after your goto labels. At least that's what makes Visual Studio happy.

Also, you are missing a final curly brace.

Instead of this;

nextArg:
nextnextArg:
}

This:

    nextArg:
    nextnextArg:
        continue;
    }
}

You could also replace the continue statement I suggest above with pretty much anything.

  • Related