I am getting an error by the bool function 'expected unqualified - id before '{' toke

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RKVP123
    New Member
    • Mar 2015
    • 2

    I am getting an error by the bool function 'expected unqualified - id before '{' toke

    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <vector>

    using namespace std;

    //---Do not edit the following
    //StartProvided
    const int ERROR_CONVERSIO N = 1;
    /*
    Preconditions: Parameter is positive
    Output stream is not in an error state
    Postconditions: Ouput stream contains the lucas sequence up to given number of terms
    Output stream is not in an error state
    */
    void genLucasSequenc e(int intTerms);

    /*
    Preconditions: Parameter is a non-empty string
    Postconditions: Boolean value indicating validity is returned
    */
    bool isValidBinarySt ring(string strNumber);

    /*
    Preconditions: Parameter 2 and 3 are variable (non-literal, non const) integers
    Postconditions: Parameter 2 and 3 are modified to contain appropriate boolean values
    */
    void isPositiveAndEv en(int intValue, bool& blnPositive, bool& blnEven);

    /*
    Preconditions: Parameter is positive
    Postconditions: Vector containing terms of sequence is returned
    */
    vector<int> genCollatzSeque nce(int intValue);

    /*
    Preconditions: Output stream is not in an error state
    Postconditions: Converted value is returned if valid or program is terminated
    */
    int promptForNumber ();

    int main()
    {
    bool blnContinue = true; //Loop control variable
    do
    {
    while(cin.fail( )) //Purge stream if needed
    {
    cin.clear();
    string strJunk = "";
    getline(cin, strJunk);
    }
    cout << "a) Lucas Sequence" << endl
    << "b) Validate binary string" << endl
    << "c) Test for positive and even" << endl
    << "d) (Bonus) Generate Collatz sequence" << endl
    << "e) Quit" << endl
    << "Enter selection: ";
    char chChoice = '\0'; //Capture menu option
    cin >> chChoice;
    //Actual parameters for functions
    string strBinaryNumber = "";
    int intValue = 0;
    bool blnPositive = false;
    bool blnEven = false;
    //Variable to hold return values
    vector<int> vecSequence;
    switch(chChoice ) //Menu selection
    {
    case 'a':
    case 'A':
    intValue = promptForNumber (); //Prompt user
    genLucasSequenc e(intValue); //Generate sequence
    break;
    case 'b':
    case 'B':
    cout << "Enter binary string: ";
    getline(cin, strBinaryNumber ); //Prompt user
    if(isValidBinar yString(strBina ryNumber)) //Output based on result
    cout << endl << "Number is a binary string" <<endl;
    else
    cout << endl << "Number is not a binary string" << endl;
    break;
    case 'c':
    case 'C':
    intValue = promptForNumber ();
    isPositiveAndEv en(intValue, blnPositive, blnEven); //Pass by reference
    if(blnPositive) //Output based on result
    cout << "Number is positive" <<endl;
    else
    cout << "Number is not positive" << endl;
    if(blnEven)
    cout << "Number is even" <<endl;
    else
    cout << "Number is odd" << endl;
    break;
    case 'd':
    case 'D':
    {
    intValue = promptForNumber (); //Prompt user
    vecSequence = genCollatzSeque nce(intValue); //Generate sequence
    cout << "Collatz Sequence: ";
    for(int k = 0; k < vecSequence.siz e(); k++)
    cout << vecSequence[k] << endl;
    }
    break;
    case 'e':
    case 'E':
    blnContinue = false; //Quit by ending main loop
    break;
    default: //Inform user of invalid option being selected
    cerr << "Invalid option, try again" << endl;
    }

    } while(blnContin ue); //End of menu loop
    return 0;
    }

    //An example of a value returning function implementation
    int promptForNumber ()
    {
    int intValue = 0;
    cout << "Enter number: ";
    cin >> intValue;
    if(cin.fail())
    {
    cerr << "Invalid conversion" << endl;
    exit(ERROR_CONV ERSION);
    }
    return intValue;
    }

    //EndProvided

    //--Edit after this point.
    //Your code for the function implementations goes here
    void genLucasSequenc e(int intTerms)
    {
    int a = -1;
    int b = 2;
    intTerms = 0;
    cout << "Enter the number of terms" << endl;
    cin >> intTerms;

    for (int i=1; i<=intTerms; i=i+1)
    {
    int c = a;
    a = b;
    b = c + b;
    }
    cout << b << endl;
    }

    bool isValidBinarySt ring(string strNumber);
    { // this is where my error is.
    strNumber = " ";
    {
    if(strNumber = "0") | (strNumber = "1")
    return (strNumber true)
    else
    return (strNumber false)
    }
    }


    void isPositiveAndEv en(int intValue, bool& blnPositive, bool& blnEven);
    {
    intValue = 0;
    cout << "Enter a number" << endl;
    cin >> intValue;

    blnPositive = intValue > 0;
    blnEven = intValue / 2;

    }

    vector<int> genCollatzSeque nce(int intValue);
    {
    vector<int>vecS equence
    vecSequence.pus h_back(intValue );
    while(intValue> 1)
    {
    if (intValue%2==0) ;
    intValue = intValue / 2
    else
    intValue = intValue*3+1;
    vecSequence.pus h_back(intValue );
    }
    return vecSequence;
    }

    bool OptionE(bool blnContinue)
    {
    return (blnContinue false)
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Remove the semicolon from the line immediately above the comment "this is where my error is". The semicolon makes that line a function declaration (that is, a function prototype), but you want a function definition. This same mistake occurs at least twice more in subsequent lines .

    Comment

    • RKVP123
      New Member
      • Mar 2015
      • 2

      #3
      i am getting the same error

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        There weren't any changes in the error message or associated line number?

        Starting from your "this is where my error is" comment and focusing on syntax issues...
        1. Next line. I'm not familiar with C++ string type, but this line looks like what I would expect to be ok.
        2. Next line. This open brace is legal, but it doesn't do anything useful.
        3. Next line. Change each = into == then change | into || finally enclose entire condition in parentheses.
        4. Next line. I don't understand what (strNumber true) means, but even if that is ok, need to terminate this line with a semicolon.
        5. Next line. Ok
        6. Next line. I don't understand what (strNumber false) means, but even if that is ok, need to terminate this line with a semicolon.


        By the way, it would be a lot easier to refer to specific lines in your program if you enclosed the entire block of code in [CODE/] tags.

        Comment

        Working...