2016-12-01 43 views
-6

下面你會發現我創建用戶自定義函數的慘淡嘗試。我正在做一項計算安裝不同形狀地毯的面積和成本的任務。我也想保持它們的總數。此外,該分配要求我使用一個使用過的已定義函數。現在它所做的只是接受1的輸入並詢問「邊的長度是多少:」。然後循環回選擇菜單。它並沒有計算出總計很少的跟蹤總數。我在創建用戶定義的函數時做了什麼錯誤,我如何合併它以保持運行總數直到它們退出?如何在C++中調用用戶定義的函數?

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string> 
#include <cmath> 

using namespace std; 

void square(double); 

const double UNIT_PRICE = 2.59; 
const double LABOR_COST = 32.5; 
const double PIE = 3.14; 
const double TAX = .0825; 

int main() { 

    int selection; 
    int sqrSide = 0; 

    // declare and initialize the variables for the shape 
    int sqrTot = 0; 

    do { 
    // get input from user as to what they want to do 
    cout << "Carpet Area Shape" << endl; 
    cout << "1. Square" << endl; 
    cout << "2. Rectangle" << endl; 
    cout << "3. Circle" << endl; 
    cout << "4. Triangle" << endl; 
    cout << "5. Done" << endl; 
    cout << "Type a number to continue: "; 
    cin >> selection; 
    cout << endl; 

    // loop through the solutions based on the user's selection 
    switch (selection) { 
    case 1: 
     cout << "What is the length of the side: "; 
     cin >> sqrSide; 

     square(sqrSide); 

     if (sqrTot > 0) { 
     cout << "Shape: Square" << endl; 
     cout << "Side: " << sqrSide << endl; 
     cout << "Area: " << sqrTot << endl; 
     } 

     cout << endl; 

     system("pause"); 

     break; 

    case 2: 
    case 3: 
    case 4: 
    case 5: // exit 

     system("cls"); 

     break; 

    default: 
     "You have made an invalid selection. Please choose a number from the " 
     "list."; 
     cout << endl; 
    } 

    // loop through if the user is still making a valid selection 
    } while (selection != 5); 

    system("pause"); 
    return 0; 
} 

void square(double) { 
    double sqrSide = 0; 
    double sqrTot = 0; 
    double sqrArea; 

    sqrArea = sqrSide * 4; 

    // get the total area and store it as a variable 
    sqrTot += sqrArea; 

    if (sqrTot > 0) { 
    cout << "Shape: Square" << endl; 
    cout << "Side: " << sqrSide << endl; 
    cout << "Area: " << sqrTot << endl; 
    } 
} 
+0

實際問題是什麼。錯誤消息?意外的行爲? –

+0

現在它所做的只是接受1的輸入並詢問「邊的長度是多少:」。然後循環回選擇菜單。它並沒有計算出總計很少的跟蹤總數。 – BWMustang13

+2

'void square(double)'你甚至不使用輸入參數? 'sqrTot'是'square()'函數的本地代碼,並且將針對每個調用重新計算。 –

回答

1

正如πάνταῥεῖ在評論中提到的那樣,您似乎對變量範圍,參數和返回值有一些誤解。讓我們看看我們是否無法消除其中的一些。

首先,讓我們談談範圍。當我們在由{}分隔的塊中聲明變量時,該變量只存在於該塊內。代碼塊後面的代碼不能訪問變量

所以,這是好的:

int a = 3; 
int b = 2; 

int c = a*b; 

但是,這不,因爲a和b的值不再可用:

{ 
int a = 3; 
int b = 2; 
} 
int c = a*b; 

接下來,讓我們來談談參數。這些是功能的輸入,功能將用於完成其任務。雖然他們的名字是無關緊要的,而且毫無意義,但它肯定會幫助你和你的其他人給他們有意義的名字。一些編程語言甚至某些學科的學生並沒有遵循這個準則,並且可能會產生難以遵循的代碼。在20年前的德州儀器計算器和物理學家發現的基本實施,我在看

考慮下面的函數,(他們的屍體,我ommitted爲了簡潔):

double calcArea(double a) 
{ 
... 
} 

double calcArea(double b) 
{ 
... 
} 

他們都吸。 a代表什麼,b怎麼樣? 一個更好的對可能類似於:

double calcArea(double radius) 
{ 
... 
} 

double calcArea(double sideLenOfSquare) 
{ 
... 
} 

最後,讓我們來談談返回值。在上述4個函數中,聲明以double開頭。這意味着我們可以期望從函數中獲得double類型的值。然而,這只是編碼 - 沒有魔法,因此我們需要讓編譯器知道這個值是什麼。擴展前面兩個功能,我們可以想出一些類似如下:

double calcArea(double radius) 
{ 
    return 3.1415926535 * (radius * radius); 
} 

double calcArea(double sideLenOfSquare) 
{ 
    return sideLenOfSquare * sideLenOfSquare; 
} 

現在事實證明 - 即使這兩個簡單的功能是不是所有他們破獲行動是。也就是說,第一個函數使用常量-π(Pi或3.141 ....)這已經存在(並且遠遠爲比我使用的精度更好)在math.h頭文件中。如果包含此文件,則可訪問#define d常數,M_PI。 接下來,這兩個函數具有相同的名稱,並採用相同類型的相同數量的參數。編譯器不可能知道你想調用哪一個。至少,他們應該有不同的名字。也許像calcCircleAreacalcSquareArea。現在,編譯器知道你所指的是哪一個函數,並且很樂意編譯這部分代碼。其他地方可能存在錯誤,但這些是另一回事。

function overloading的一點研究將提供資源,可以解釋功能相同的問題和解決方案,遠勝於我能夠傾向於嘗試的功能。 :)

+0

謝謝!這是一個非常有幫助的迴應! – BWMustang13

2

當您聲明函數的原型時,可以省略參數,但在實現中必須放置它。

變化:

void square(double) 
{ 
    double sqrSide = 0; 
    double sqrTot = 0; 
    double sqrArea; 

    sqrArea = sqrSide * 4; 

    //get the total area and store it as a variable 
    sqrTot += sqrArea; 

    if (sqrTot > 0) { 
     cout << "Shape: Square" << endl; 
     cout << "Side: " << sqrSide << endl; 
     cout << "Area: " << sqrTot << endl; 
    } 


} 

到:

void square(double sqrSide) 
{ 
    double sqrTot = 0; 
    double sqrArea; 

    sqrArea = sqrSide * 4; 

    //get the total area and store it as a variable 
    sqrTot += sqrArea; 

    if (sqrTot > 0) { 
     cout << "Shape: Square" << endl; 
     cout << "Side: " << sqrSide << endl; 
     cout << "Area: " << sqrTot << endl; 
    } 


} 

,改變:

case 1: 
       cout << "What is the length of the side: "; 
       cin >> sqrSide; 

       square(sqrSide); 

       if (sqrTot > 0) { 
        cout << "Shape: Square" << endl; 
        cout << "Side: " << sqrSide << endl; 
        cout << "Area: " << sqrTot << endl; 
       } 

       cout << endl; 

       system("pause"); 

       break; 

到:

case 1: 
       cout << "What is the length of the side: "; 
       cin >> sqrSide; 

       square(sqrSide); 

       system("pause"); 

       break; 
+0

您應該解釋這些更改,而不是僅僅傾銷代碼。 – Carcigenicate

+0

此外,這並不能解決從OP中出現的主要問題。 –

+0

@πάνταῥεῖ什麼是OP? – eyllanesc

相關問題