1

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 constructor

1 in size =

the 0 and ++ in the first for loop in the constructor

the i in the number[i] = 0; statement

char 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 function

the -1 after max_digits and the i, in the for loop for (int i = max_digits - 1; i < max_digits; i++)

number and i in the cout 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.

Emma
  • 27,428
  • 11
  • 44
  • 69
Lisa
  • 201
  • 2
  • 8
  • 1
    Please do some [rubber duck debugging](https://www.google.com/search?q=rubber%20duck%20debugging) of that loop in the `print` function. And the code in general. For example, does it really make sense to have `int number[1];` when `max_digits` is equal to `60`? There are also some other things that might be helped by [reading a good book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), or going through your tutorial or class-notes a little more. – Some programmer dude Feb 19 '19 at 04:56
  • 2
    And what books would you suggest I read Mr. Not-Very-Helpful. I am taking a class and trying to use what the teacher has taught us and what's in am online text, which has very basic examples. I have tried looking things up online, but I also need to know the right search terms to find what I need that will be helpful. this also due really soon and I don't have a lot of time to find a book and read it. – Lisa Feb 19 '19 at 05:04
  • 1
    From maintenance aspect, I would prefer `int number[max_digits];`. Even if the value of `max_digits` is changed later this won't break the rest of your code. If you want to store one digit (0 ... 9) per element, you could use an even smaller element type. The smallest would be `char` (which is also an integral type like `int`). (Actually, you could even store two digits per `char` but this is, may be, a bit over-ambitious.) – Scheff's Cat Feb 19 '19 at 06:36
  • 1
    The encoding of characters (at least the first 128) is usually [ASCII](https://en.wikipedia.org/wiki/ASCII) code. The ASCII code grants that codes for `0` ... `9` have the same order like the corresponding digits. The ASCII code for `0` can be encoded in C/C++ simply by its character constant: `'0'` has the value of 48 which is the ASCII code of 0. However, calculations involving character constants are recommended. E.g. if `char c` holds any character for a (decimal) digit then `c - '0'` provides the value of its corresponding digit. The opposite works as well. – Scheff's Cat Feb 19 '19 at 06:41

1 Answers1

1

The ? is '0',the char array save the char '0' to '9'.And the function is save int to int number[].So need a change.

And others which need change is:

  1. number[] define

    int number[max_digits];

  2. the print function, i++ change to i--

上山老人
  • 412
  • 4
  • 12