使用 C++ 中的函数从一个数组中提取元素并将其放入另一个数组中

问题描述 投票:0回答:1

我有一个任务要从文件“numbs.txt”中提取整数列表。这些数字是 1-200 之间的随机数。然后,我需要将它们分成 25 个范围(0-24、25-49...),并报告每个数字范围内的 man 整数。当我试图取回整数列表时,我迷路了。我试图将它们输入到另一个数组中,希望我可以计算新数组中的整数。我刚刚学习 C++ 中的函数和数组,因此非常感谢您的帮助。

从“int”到“int*”的无效转换[-fpermissive]是我尝试调用函数时收到的消息。

#include<iostream>
#include<array>
#include<fstream>   // open, read, and close files
    

using namespace std;

ifstream grades;
string grade_file = "numbs.txt";
const int SCORE_SIZE = 26;
int scores[SCORE_SIZE];
void getNumbs();
int * score_count(int s_count[]);
//void getNumbs(int scores[], int scoreSize);


int main() {

getNumbs();
cout << "The following array positions are scores below 24 file: " << endl;
int s_count[] = score_count(scores[a]); << invalid conversion from 'int' to 'int*' [-fpermissive]
return 0;
}


void getNumbs(){
ifstream grades;    // input file variable
string grade_file = "numbs.txt";    // inpute file name variable
cout << "Opening File." << endl;    // update
grades.open(grade_file.c_str());    // opening file
if (grades.is_open()){              // Verifying file is open
    cout << grade_file << " Is Open." << endl;
    for(int i = 0; i < SCORE_SIZE; i++){    // If file is open iterate through the numbers and enter them into string array
        grades >> scores[i];
        cout << scores[i] << " ";
    }
    }
    else {
    cout << "No file found: " << grade_file << endl;
}

    grades.close(); // Close file
    cout << "\nFile is closed." << endl;
    cout << endl;
}
// function to separate the numbers between 0 - 24.

int * score_count(int s_count[]){
for (int a = 0; a < SCORE_SIZE; a++){
    if (scores[a] <= 24){
        cin >> s_count[];
        }
    return s_count[];
}
}
c++ arrays function return
1个回答
0
投票

您的程序似乎存在以下一些问题:

  1. 您的问题陈述指出您需要将其分为 25 组,其中数字范围为 0 - 200。您创建一个函数将其传递到 getNumbs 数组中,但在读取文件时使用 SCORE_SIZE 作为 26。这只会读取文件中的前 26 个字符。这是根据问题陈述吗?
  2. 在函数 Score_count 中,您的返回类型和实际返回的内容并不相等。您需要为其提供一个数组,该数组是 s_count 而不是 s_count[]。

请解决这些问题。

© www.soinside.com 2019 - 2024. All rights reserved.