3

I have to make my homework. It is console application which uses an array of structs that keep information about a computer(brand, year of manufactoring, weight and inventory number). So I wrote a completely working program, but I want to use a dynamic array, because I dont know how many records the user will input.

Is there way to do this. To add new records in array until the user say n/N? Any suggestions?

This is my version of program:

#include "stdafx.h"
#include <iostream>

using namespace std;

struct ComputerInfo
{
    char computerMark[20], invertarNumber[6];
    unsigned int year;
    float weight;
};

ComputerInfo computerArray[300];

ComputerInfo AddComputers(ComputerInfo compterArray[], int counter)
{
    cout << "Enter mark of the computer: ";
    cin >> computerArray[counter].computerMark;

    cout << "Enter year of establish: ";
    cin>> computerArray[counter].year;

    while ((computerArray[counter].year < 1973)
        || (computerArray[counter].year > 2013))
    {
        cout << "INVALID YEAR!!!" << endl;

        cout << "Enter year of establish: ";
        cin>> computerArray[counter].year;
    }

    cout << "Enter computer weidth: ";
    cin >> computerArray[counter].weight;

    cout << "Enter computer invertar number(up to six digits): ";
    cin >> computerArray[counter].invertarNumber;

    return computerArray[counter];
}

void ShowRecords()
{
    int counter = 0;

    while (computerArray[counter].year != 0)
    {
        cout << "Mark: " << computerArray[counter].computerMark << endl;
        cout << "Year: " << computerArray[counter].year << endl;
        cout << "Weidth: " << computerArray[counter].weight << endl;
        cout << "Inv. number: " << computerArray[counter].invertarNumber << endl << endl;

        counter++;
    }
}

void MoreThanTenYearsOld(ComputerInfo computerArray[])
{
    int counter = 0;
    float counterOldComputers = 0;
    float computerPer = 0;

    while (computerArray[counter].year == 0)
    {
        if (computerArray[counter].year <= 2003)
        {
            counterOldComputers++;
        }

        counter++;
    }

    computerPer = counterOldComputers / 3;

    cout << endl;
    cout << "Percantage of old computers is: " << computerPer << endl;
}

int main()
{
    int counter = 0;
    float computerPer = 0;
    char answer = 'y';

    for (int i = 0; i <= 299; i++)
    {
        strcpy(computerArray[i].computerMark,"");
    }

    while((answer == 'Y') || (answer == 'y'))
    {
        computerArray[counter] = AddComputers(computerArray, counter);
        cout << endl;
        cout << "Do you want to enter more records (Y/N): ";
        cin >> answer;
        cout << endl;
        counter++;
    }

    MoreThanTenYearsOld(computerArray);

    return 0;
}
2
  • 2
    Stop using arrays, start using std::vector. Commented Sep 10, 2012 at 13:33
  • Just wonder: Why 1973..2013 range? Commented Sep 10, 2012 at 14:48

2 Answers 2

1

Yes. Instead of your array, use

std::vector<ComputerInfo> computerArray;

and you can add as many objects as you want:

ComputerInfo c;
// read the data
computerArray.push_back(c);

now, computerArray[0] will have the info in c.

You'll need to #include <vector>.

Also, instead of char computerMark[20] you can use a std::string.

Sign up to request clarification or add additional context in comments.

12 Comments

And use std::string inside the vector.
@honk you mean inside ComputerInfo?
Yes, exactly, to help the autogen'ed copy constructor. But you knew that.
This answer doesn't address the fact that the OP clearly stated he is doing homework and possibly isn't allowed to use STL classes.
@Code-Guru there is nothing in the question to suggest any such restrictions. In the absence of more information, I think giving the best solution, as is done here, is the right approach.
|
0

You have two options:

1) Use std::vector instead of an array. This is a very powerful tool and certainly worth learning how to use.

2) Dynamically allocate the array and resize it as you add more items. Basically this means writing your own version of std::vector. This is a good way to strengthen your programming skills. You will learn what goes into writing standard classes and functions. However, I advise using std::vector in more serious programming because it has already been thoroughly tested and debugged.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.