2015-04-23 220 views
-3

我新的C++,并在大学课程学习它,我们刚刚启动的功能,这是我与他们打交道的第一实验室。我在switch语句中一直收到每个case的这个错误,我不知道如何纠正它。错误:参数太少运作

error: too few arguments to function 'void addFraction(int, int, int, int)'

#include <iostream> 
#include <cstdlib> 

using namespace std; 

void clearScreen(); 
void printMenu(); 
void getFraction(int&, int&); 
void addFraction(int, int, int, int); 
void subFraction(int, int, int, int); 
void mulFraction(int, int, int, int); 
void divFraction(int, int, int, int); 
//void reducFraction(int &, int &); 

int main() 
{ 
    clearScreen(); 
    printMenu(); 
    return 0; 
} 

//Function clears the screen 
void clearScreen() 
{ 
    cout << string(50, '\n'); 
} 

//Function prints a menu to the screen 
void printMenu() 
{ 
    int menu; 

    cout << "Fraction Calculator\n"; 
    cout << "\n"; 
    cout << "1. Add Fraction\n"; 
    cout << "2. Subtract Fraction\n"; 
    cout << "3. Multiply Fraction\n"; 
    cout << "4. Divide Fraction\n"; 
    cout << "5. Quit\n"; 
    cout << ":"; 
    cin >> menu; 

    //Switch statement that drive the menu 
    switch (menu) 
    { 
     case '1': addFraction(); 
        break; 
     case '2': subFraction(); 
        break; 
     case '3': mulFraction(); 
        break; 
     case '4': divFraction(); 
        break; 
     case '5': exit(0); 
        break; 
     default: { 
        cout << endl << "Invalid choice\n" << endl; 
        printMenu(); 
        } 
    } 

} 

void addFraction(int f1n, int f1d, int f2n, int f2d) 
{ 
    clearScreen(); 
    getFraction(f1n, f1d); 
    getFraction(f2n, f2d); 
    int fan, fad; 
    fan = (f1n * f2d) + (f2n * f1d); 
    fad = (f1d * f2d); 
    //reducFraction(); 
    cout << fan << endl << "---" << endl << fad; 
} 

void subFraction(int f1n, int f1d, int f2n, int f2d) 
{ 
    clearScreen(); 
    getFraction(f1n, f1d); 
    getFraction(f2n, f2d); 
    int fan, fad; 
    fan = (f1n * f2d) - (f2n * f1d); 
    fad = (f1d * f2d); 
    //reducFraction(); 
    cout << fan << endl << "---" << endl << fad; 
} 

void mulFraction(int f1n, int f1d, int f2n, int f2d) 
{ 
    clearScreen(); 
    getFraction(f1n, f1d); 
    getFraction(f2n, f2d); 
    int fan, fad; 
    fan = (f1n * f2n); 
    fad = (f1d * f2d); 
    //reducFraction(); 
    cout << fan << endl << "---" << endl << fad; 
} 

void divFraction(int f1n, int f1d, int f2n, int f2d) 
{ 
    clearScreen(); 
    getFraction(f1n, f1d); 
    getFraction(f2n, f2d); 
    int fan, fad; 
    fan = (f1n * f2d); 
    fad = (f2n * f1d); 
    //reducFraction(); 
    cout << fan << endl << "---" << endl << fad; 
} 

void getFraction(int& numerator, int& denominator) 
{ 
    cout << "Please enter a numerator: "; 
    cin >> numerator; 

    cout << "Please enter a denominator: "; 
    cin >> denominator; 

    while(denominator == 0) 
    { 
     cout << "Invalid denominator! Enter a new one: "; 
     cin >> denominator; 
    } 
    clearScreen(); 
} 
+0

要调用的所有功能,而不给他们任何数字上 – user7

+1

工作在你的switch语句尝试调用的函数,而无需指定四个参数的函数需要:即你只需要调用'addFraction();'。你需要获得一些值作为参数传递...也许从键盘/'std :: cin',或硬编码的东西开始.... –

+0

我可以做什么像'addFraction(0,0,0,0)'? – dbs1crew

回答

1

尝试:

case '1': 
    addFraction(1, 2, 3, 4); 
    break; 

在你的代码下的评论中提到,你打电话与未声明的变量的方法。

0

调用函数时请输入参数值。 例子:

case '1': addFraction(1,2,3,4); 
       break; 
0

你已经宣布你addFraction功能,使得它有四个参数。

void addFraction(int f1n, int f1d, int f2n, int f2d) 

在switch语句中调用它时,您提供了0个参数。

addFraction(); 

您必须传递函数4参数。例如,下面是一个有效的电话:你使用

addFraction(1, 2, 3, 4); 
0

所有的功能都已经参数传递,而当你从交换机的情况下调用的函数,你不传递任何参数。

,你将不得不采取从用户的分数,然后再调用函数,其对分数进行操作。

  1. 询问了分数的用户。
  2. 调用时使用分数值进入函数。

案例'1':
addFraction(1,2,3,4);
break;

1

而函数声明为多个参数

void addFraction(int, int, int, int); 
void subFraction(int, int, int, int); 
void mulFraction(int, int, int, int); 
void divFraction(int, int, int, int); 

必须提供所需的功能,来电参数您调用的函数不带任何参数

switch (menu) 
{ 
    case '1': addFraction(); 
       break; 
    case '2': subFraction(); 
       break; 
    case '3': mulFraction(); 
       break; 
    case '4': divFraction(); 
       break; 
    case '5': exit(0); 
    //... 

事实上,你可以不带参数声明的功能。例如,

void addFraction() 
{ 
    int f1n, f1d; 
    int f2n, f2d; 

    clearScreen(); 

    getFraction(f1n, f1d); 
    getFraction(f2n, f2d); 

    int fan, fad; 

    fan = (f1n * f2d) + (f2n * f1d); 
    fad = (f1d * f2d); 

    //reducFraction(); 
    cout << fan << endl << "---" << endl << fad; 
} 

以同样的方式可以定义(和声明)其他函数。

+0

我最终不得不建立在此代码之上,因此我可能会废弃很多代码,并随您的建议去做。感谢您的输入! – dbs1crew

+0

@ dbs1crew您的每个函数都会自行询问需要的值。所以没有必要用参数声明它们。在我播种的每个函数中声明相应的局部变量就足够了。 –

相关问题