要求用户输入对象数量,将每个对象传递给数组,然后询问每个特定对象的其他信息?

时间:2019-02-06 05:58:35

标签: c++ arrays

我试图让用户输入多个对象,但是对象的数量必须在1到10之间,然后将每个对象添加到数组中。到目前为止,我已经知道了:

void numObjects (void){
    int i;
    int numObjectsArr[10];
    cout << "Enter number of objects, ranging from 1 to 10 objects: ";
}
int main (int argc, char** argv){
    numObjects();
}

我知道我需要使用for循环,但是有一种方法可以使每个对象(为简单起见,假设它们是球)是一个单独的东西,而我d是否可以在每个特定的球上添加更多信息?例如,如果我想这样做,可以问numObjectsArr [0]权重为多少?我知道如何向用户询问要放入数组中的整数,但是我不确定如何实现这一点。

2 个答案:

答案 0 :(得分:0)

如上所述,您可能应该阅读一些有关C ++的教程(特别是类和implementation 'com.github.bumptech.glide:glide:4.8.0')。但是让您知道这样的程序是什么样的:

std::vector

#include <iostream> #include <vector> class Ball { public: double weight; }; Ball create_ball() { std::cout << "Enter weight: "; double new_weight = 0.; std::cin >> new_weight; Ball b; b.weight = new_weight; return b; } std::vector<Ball> create_objects(int min = 1, int max = 10) { std::vector<Ball> balls; std::cout << "Enter number of balls: " << std::endl; int n_balls = 0; std::cin >> n_balls; if (n_balls < min || n_balls > max) { std::cout << "Invalid numer of balls!" << std::endl; // here should be a proper error handling; // preferably by throwing an exception // But for simplicity the empty vector is returned return balls; } for (int i=0; i<n_balls; i++) { Ball new_ball = create_ball(); // create a new Ball object balls.push_back(new_ball); // add the new object to the vector. } return balls; } int main() { std::vector<Ball> balls = create_objects(1, 10); for (int i=0; i<balls.size(); i++) { std::cout << "Ball " << i << ": weight: " << balls[i].weight << std::endl; } } 视为具有动态大小的数组,即,每次通过其方法std::vector将新对象添加到向量中时,向量的大小都会增加。因此,使用向量可使您的程序正确运行,以供有人要输入的任何数量的对象使用。您甚至可以取消对最小和最大对象数的检查。

答案 1 :(得分:0)

无法返回数组(它们会衰减到指针,并且数组超出范围并在任何人都可以使用返回的指针之前变为无效),并且如果可以返回数组,则无法分配给新的数组变量。这样会严重阻碍传递数组。

现代的解决方案是使用Library Containers,例如std::arraystd::vector。令人遗憾的是,许多入门课程都不允许学生使用std::vector的好坏,因此学生必须寻找其他方法来获得相同的效果。

所以...您不能返回数组,但是可以返回包含数组的结构。

struct arrayholder
{
    int numObjectsArr[10];
    int used = 0; // Book keeping for much of the array is in use
}

现在我们可以

arrayholder numObjects ()
{
    int i;
    arrayholder Arr;
    cout << "Enter number of objects, ranging from 1 to 10 objects: ";

    return Arr
}

这还没有做任何有用的事情,但是至少我们可以使数组脱离函数。有一个缺点,按值返回结构意味着将复制该结构。幸运的是,今天任何体面的编译器都将支持Copy Elision并以静默方式为您节省所有额外的开销。

我们如何将数据放入数组?

arrayholder numObjects ()
{
    int i;
    arrayholder Arr;
    cout << "Enter number of objects, ranging from 1 to 10 objects: ";
    while (!(cin >> Arr.used)) // keep asking for a number until we get a number
    {
        // didn't get a number. This sets an error flag that needs to be cleared
        cin.clear(); 
        //throw out the bad input
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    // note: the above is about the minimum you want to use for input validation.
    // there are a bunch pf potential problems with it, like allowing crap like "8foo foo"
    // add logic to keep nagging the users until they give a number from 1 to 10
    // I'm not writing this because it's the crux of the assignment.

    for (int index = 0; index < Arr.used; index++)
    {
        while (!(cin >> Arr.numObjectsArr[index])) 
        {
            cin.clear(); 
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    return Arr;
}

看到可怕的重复代码块了吗?用函数代替它是一个完美的选择。

int getNumber()
{
    int num;
    while (!(cin >> num)) 
    {
        cin.clear(); 
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    return num;
} 

arrayholder numObjects ()
{
    int i;
    arrayholder Arr;
    cout << "Enter number of objects, ranging from 1 to 10 objects: ";
    Arr.used = getNumber();
    // add logic to keep nagging the users until they give a number from 1 to 10
    // I'm still not writing this because it's the crux of the assignment, but 
    // getNumber just made it a lot easier to write.

    for (int index = 0; index < Arr.used; index++)
    {
        Arr.numObjectsArr[index] = getNumber();
    }
    return Arr;
}