繁体   English   中英

如何在 C++ 中定义和调用函数?

[英]How to define and call functions in c++?

我是 C++ 新手,我想知道如何正确定义函数?

现在我收到以下错误:

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

我试图将函数放在“int main ()”之外,但没有效果......
 #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; }

有没有什么办法解决这一问题?

更新:这是我将函数放在“main()”之外时的代码......有没有办法从“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; }

在这种情况下,您正在尝试在函数内部定义函数main ,您不能这样做。 正确的方法如下

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM