I need to fill in the blanks on pieces of code I've already been given. The problem is this:
The following class (long_number
) represents a number of any length from 1
to 60
. The default constructor generates the number 0
. Need a print function, which prints the long_number
. A constructor that turns an array of chars into a long_number
.
I've gotten a few of the pieces started. Still trying to work out some of the later stuff. A lot of it is not quite making sense.
The places I put in code are:
the 1 in the statement
int number [];
long_number::long_number
in the no-arg constructor1 in size =
the
0
and++
in the first for loop in the constructorthe
i
in thenumber[i] = 0;
statementchar and the
::long_number
for the parameter in the 2nd constructor
_size
in the statement size =the greater than or equal to in the for statement
void long_number::
at the start of the print functionthe
-1
aftermax_digits
and thei
, in the for loopfor (int i = max_digits - 1; i < max_digits; i++)
number and
i
in thecout
statement
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class long_number {
private:
static const int max_digits = 60;
int number[1];
int size;
public:
long_number();
long_number(char a[], int _size);
void print();
};
long_number::long_number() {
size = 1;
for (int i = 0; i < max_digits; i++) {
number[i] = 0;
}
}
long_number::long_number(char input[], int _size) {
size = _size;
for (int i = 1; i <= size; i++) {
number[size -i] = (char) input[size - 1] - (int)'?';
}
void long_number::print() {
for (int i = max_digits - 1; i < max_digits; i++) {
cout << number[i];
}
cout << endl;
}
int main()
{
long_number n1;
n1.print();
cin.get();
return 0;
}
I question how I did the for loop in the print function and I need help with the constructor with parameters. The ?
is one place I'm not sure what to put. There isn't anything similar in the problems I see in the textbook and he didn't show us any examples like this in class.