簡體   English   中英

如何在cpp的main()中調用這個函數

[英]How to call this function in main() in cpp

我正在嘗試在 main() 函數中使用以下 remove() 函數。 請讓我知道我當前的代碼有什么問題。

void remove(float a[], int& n, int i);
int main() {
    float a[]={56.8, 14.2, 0.2, 22.3, 3.3, 54.02, 543.33, 456,333, 1.1};
    int size, del;
    size =sizeof(a)/sizeof(a[0]);
    cout<<"Enter the element to be deleted!";
    cin >>del;
    cout << "Removed= "<< remove(a, size, del) << "\n";
}

void remove(float a[], int& n, int i){
    for (int j=i+1; j<n; j++)
        a[j-1] = a[j];
     --n;
    }
float remove(float a[], int& n, int i);
int main() {
    float a[]={56.8, 14.2, 0.2, 22.3, 3.3, 54.02, 543.33, 456,333, 1.1};
    int size, del;
    size =sizeof(a)/sizeof(a[0]);
    cout<<"Which element do you want to remove: ";
    cin >>del;
    remove (a, size, del);
}

float remove(float a[], int& n, int i){
    cout << "Removed= "<< a[i] << "\n";
    for (int j=i+1; j<n; j++)
        a[j-1] = a[j];
        --n;
    return i;
    }

所以你的問題是

a) 讓我知道我的代碼有什么問題

b) 沒有其他方法可以在不改變功能代碼的情況下做同樣的事情

到 a)

  1. 我最近學會了盡可能不要使用命名空間,因為實際上經常發生的變量命名很有可能導致可避免的錯誤 - 因此建議using namespace std; std::而不是using namespace std;

  2. sizeof()可能會在內部使用 double 而不是 float,所以在它旁邊我的警告: implicit conversion loses floating-point precision: 'double' to 'float'

  3. 正文中多於一行的 for - 斜率應該寫在括號中,否則我的警告:這個“for”子句不保護... [-Wmisleading-indentation] 20 | for (int j=i+1; j

  4. 如果你使用std::cin總是應該檢查錯誤的輸入,例如像我對input()函數所做的那樣。

  5. 由於int main(){return 0;}是一個函數並且“int”表示返回值,因此始終使用“return 0”關閉函數以向您的操作系統發出停止信號。

  6. 在開頭而不是在行之間聲明變量使代碼更具可讀性

  7. 另外,如果您得到正確答案,請不要忘記檢查您的問題是否已解決,以避免它出現在 stackoverflow.com 的“未解決”部分;)

b)

是的,它是 - 除了使用 { } - 像這樣的括號:

#include <iostream> //std, std::cin
#include <limits> //std::numeric_limits<std::streamsize>::max()

int input(int size); //int signals return
void remove(double a[], int& n, int i); //void signals no return

int main() {

    int size, del;
    double rem;

    double a[]={56.8, 14.2, 0.2, 22.3, 3.3, 54.02, 543.33, 456,333, 1.1};
    size =sizeof(a)/sizeof(a[0]);

    std::cout<<"Which element do you want to remove: ";
    del = input(size); //check the input

    rem = a[del]; //filling rem to print out the value to be removed to not change the function like you asked for
    std::cout << "Removed= " << rem << std::endl; 
    remove(a, size, del); 

return 0;
}

void remove(double a[], int& n, int i)
{
    for (int j=i+1; j<n; j++)
       {
        a[j-1] = a[j];
        --n;
       }
}

int input(int size)
{
    int input;

    while (!(std::cin >> input) || input < 0 || input >= size) {
         std::cin.clear();
         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
         std::cout << "invalid input ignored; try again and enter a valid move between -1 and " << size << "\n";
        }

    return input;
}

我的目的是讓包括我自己在內的每個人都能從看似簡單的問題中學習。 我希望我為你達到了我的目標:)

float remove(float a[], int& n, int i){
    float deleted = a[i]; 
    for (int j=i+1; j<n; j++)
        a[j-1] = a[j];
     --n;
    return deleted;
}

在 remove 函數中添加 return 語句並相應地更改返回類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM