0

I am new to c++ and I am wodering how to properly define functions?

Right now I am receiving the following errors:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0065   expected a ';'  ConsoleMath D:\C++\ConsoleMath\ConsoleMath\ConsoleMath.cpp  18  
Error   C2601   'Basic': local function definitions are illegal ConsoleMath D:\C++\ConsoleMath\ConsoleMath\ConsoleMath.cpp  18

I've tried to place the function outside of "int main ()", but there was no effect...
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    // Initialization
    string command;
    bool loop = true;

    float number1 = 0, number2 = 0;

    // Start-up
    cout << "Welcome to Console Math v1.0.0\n\nThis software can be used for a variety of mathematical operations.\n\nFor list of commands; type \"help\"\nTo exit the program; type \"exit\"\n"
         << endl;

    // Commands
    void Basic()
    {
        cout << "     >";
        // Code here
    }

    // Main software loop
    while (loop == true)
    {
        cout << "> ";
        cin >> command;

        cout << endl
             << endl;

        if (command == "help")
        {
            cout << "Command layout (\">\" symbolizes new input after entering the previous one):\n> DatabaseName > operationName > detail name\n\n> basic\n     > add\n     > subtract\n     > multiply\n     > divide\n> advanced\n     > potentiation\n     > squareRoot\n     > findPrimeNumbers\n     > logarithm\n     > factorial\n > function\n     > intersection\n          > linear-linear\n          > linear-parabola\n > geometry\n     > rectangle\n          > perimeter\n          > area\n          > visualize\n     > triangle\n          > perimeter\n          > area\n               > basic\n               > hemmonDefinedS\n               > hemmonDefineS\n               > coordinate\n          > height\n     > circle\n          > area\n          > circumference\n> trigonometry\n     > sin\n     > cos\n     > tg\n     > ctg\n> degrees\n     > add\n     > subtract\n     > multiply\n     > divide\n > other\n     > random\n     > randomRange";
        }
        else if (command == "basic")
        {
            Basic();
        }
        else if (command == "exit")
        {
            loop = false;
        }
        else
        {
            cout << "\"" << command << "\" is not a valid command. If you're having trouble please type \"help\"";
        }

        cout << endl
             << endl;
    }

    return 0;
}

Is there any way to fix this?

Update: Here is the code when I place the function outside of "main()"... Is there a way to call it from "main()".

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;

void Basic()
{
    cout << "     >";
    // Code here
}

int main()
{
    // Initialization
    string command;
    bool loop = true;

    float number1 = 0, number2 = 0;

    // Start-up
    cout << "Welcome to Console Math v1.0.0\n\nThis software can be used for a variety of mathematical operations.\n\nFor list of commands; type \"help\"\nTo exit the program; type \"exit\"\n"
         << endl;

    // Commands

    // Main software loop
    while (loop == true)
    {
        cout << "> ";
        cin >> command;

        cout << endl
             << endl;

        if (command == "help")
        {
            cout << "Command layout (\">\" symbolizes new input after entering the previous one):\n> DatabaseName > operationName > detail name\n\n> basic\n     > add\n     > subtract\n     > multiply\n     > divide\n> advanced\n     > potentiation\n     > squareRoot\n     > findPrimeNumbers\n     > logarithm\n     > factorial\n > function\n     > intersection\n          > linear-linear\n          > linear-parabola\n > geometry\n     > rectangle\n          > perimeter\n          > area\n          > visualize\n     > triangle\n          > perimeter\n          > area\n               > basic\n               > hemmonDefinedS\n               > hemmonDefineS\n               > coordinate\n          > height\n     > circle\n          > area\n          > circumference\n> trigonometry\n     > sin\n     > cos\n     > tg\n     > ctg\n> degrees\n     > add\n     > subtract\n     > multiply\n     > divide\n > other\n     > random\n     > randomRange";
        }
        else if (command == "basic")
        {
            Basic();
        }
        else if (command == "exit")
        {
            loop = false;
        }
        else
        {
            cout << "\"" << command << "\" is not a valid command. If you're having trouble please type \"help\"";
        }

        cout << endl
             << endl;
    }

    return 0;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Looks like you are trying to define functions within other functions. That's a no-can-do. You'll have to move them outside. – user4581301 May 14 '22 at 14:58
  • You define the `Basic` function *inside* the `main` function. That's called a *nested* function. And C++ doesn't allow them. You must define your functions outside any other functions. – Some programmer dude May 14 '22 at 14:59
  • 1
    `I've tried to place the function outside of "int main ()", but there was no effect...` post this attempt, because it is the right solution – Iłya Bursov May 14 '22 at 15:00
  • Ok, but how do I call that function from the loop in the main() function? – adkjhawdiuhwaiudaw May 14 '22 at 15:01
  • 2
    If you're that new to this language, get a *good* book, and learn your debugger tools well. And yes, it can be fixed. – WhozCraig May 14 '22 at 15:01
  • 2
    your 2nd code example compiles without errors – Iłya Bursov May 14 '22 at 15:03
  • Read error messages carefully. They may say the similar things, but the details often really matter. You may have moved the function outside properly, but had forgotten a semicolon or something and still got a *expected a ';'*. – user4581301 May 14 '22 at 15:04
  • "your 2nd code example compiles without errors" - For me it doesn't. It says that Basic() is not defined. – adkjhawdiuhwaiudaw May 14 '22 at 15:14
  • 3
    It seems you could need to invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn the basics. – Some programmer dude May 14 '22 at 15:19
  • 3
    Then you forgot to hit "save" – Goswin von Brederlow May 14 '22 at 15:20
  • Here is your code in Matt Godbolt's Compiler Explorer, a tool you can use to see how various compilers (and compiler options) interpret code: https://godbolt.org/z/qo98qK4Ws . I've set it to show GCC and Visual Studio, both with and a reasonably high level of inspection and diagnostics. All they complain about is a couple unused variables. – user4581301 May 14 '22 at 15:29

1 Answers1

0

you are trying to define function inside a function in this case main , you can't do that. the correct way is following

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;

// Commands
void Basic () {
    cout << "     >";
    // Code here
}

int main () {
    // Initialization
    string command;
    bool loop = true;

float number1 = 0, number2 = 0;

// Start-up
cout << "Welcome to Console Math v1.0.0\n\nThis software can be used for a variety of mathematical operations.\n\nFor list of commands; type \"help\"\nTo exit the program; type \"exit\"\n" << endl;



// Main software loop
while (loop == true) {
    cout << "> ";
    cin >> command;
    
    cout << endl << endl;

    if (command == "help") {
        cout << "Command layout (\">\" symbolizes new input after entering the previous one):\n> DatabaseName > operationName > detail name\n\n> basic\n     > add\n     > subtract\n     > multiply\n     > divide\n> advanced\n     > potentiation\n     > squareRoot\n     > findPrimeNumbers\n     > logarithm\n     > factorial\n > function\n     > intersection\n          > linear-linear\n          > linear-parabola\n > geometry\n     > rectangle\n          > perimeter\n          > area\n          > visualize\n     > triangle\n          > perimeter\n          > area\n               > basic\n               > hemmonDefinedS\n               > hemmonDefineS\n               > coordinate\n          > height\n     > circle\n          > area\n          > circumference\n> trigonometry\n     > sin\n     > cos\n     > tg\n     > ctg\n> degrees\n     > add\n     > subtract\n     > multiply\n     > divide\n > other\n     > random\n     > randomRange";
    } else if (command == "basic") {
        Basic ();
    } else if (command == "exit") {
        loop = false;
    } else {
        cout << "\"" << command << "\" is not a valid command. If you're having trouble please type \"help\"";
    }

    cout << endl << endl;
}

return 0;
}
Abrar Malek
  • 231
  • 1
  • 5