Assertion failure
Sep 23, 2018 at 7:18pm UTC
Hey again,
Been awhile since I posted here but I am back again with another question. I have an assertion failure in a bigint project that I cannot figure out. It
happens when I try to assert anything from int[] over size 2 at line 20. I have no idea on why this is happening but I thought I would ask some people on here for suggestions. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// bigint Test Program
//
// Tests: int constructor, uses ==
//
#include "Header.h"
//===========================================================================
int main() {
{
// Setup
bigint bi(10);
// Test
int digit = bi[0];
// Verify
assert(bi == 10);
assert(digit == 10);
std::cout << bi << "_____" << digit << std::endl;
}
Header File:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#pragma once
#ifndef bigint_h
#define bigint_h
#include <iostream>
#include <cassert>
#include <fstream>
#include <cstdlib>
const int CAPACITY = 3;
class bigint {
public :
void initialize();
bigint(); // default
bigint(int x); // int to bigint
bigint(const char char_value[]); // char to bigint
//void debugPrint(std::ostream&) const;
friend std::ostream &operator <<(std::ostream &out, const bigint &rhs);
friend std::istream &operator >>(std::istream &in, bigint &rhs);
//friend std::istream &operator>>(std::istream &stm, const bigint &b_int);
bool operator ==(const bigint &rhs); // compare bigints
bigint operator +(const bigint)const ;
int operator [](int rhs)const ;
private :
int value[CAPACITY];
};
// default constructor
bigint::bigint() {
initialize();
}
// initializes to zero
void bigint::initialize() {
for (int i = 0; i < CAPACITY; ++i) {
value[i] = 0;
}
}
// saves value of int to bigint
bigint::bigint(int x) {
initialize();
int my_int = 0;
my_int = x;
for (int i = 0; i < CAPACITY; ++i) {
value[i] = my_int % 10;
my_int /= 10;
}
}
// saves value of char to bigint
bigint::bigint(const char char_value[]) {
initialize();
int len = 0;
int k = 0;
do {
++len;
} while (char_value[len] != '\0' );
for (int j = len - 1; j >= 0; j--, k++) {
value[k] = int (char_value[j]) - int ('0' );
}
}
// overloaded << operator
std::ostream &operator <<(std::ostream &out, const bigint &rhs) {
int j = CAPACITY - 1;
//int tmp[CAPACITY] = rhs.value[i];
for (int i = CAPACITY - 1; i >= j; --i, --j) {
if (j == -1)
std::cout << '0' ;
else {
if (rhs.value[i] != 0)
break ;
else
continue ;
}
}
for (int i = j; i >= 0; --i) {
out << rhs.value[i];
}
return out;
}
std::istream &operator >>(std::istream &in, bigint &rhs) {
char container[CAPACITY] = { 0 };
char j = 0;
in >> j;
for (int i = 0; i < CAPACITY; ++i) {
if (j == ';' )
continue ;
else {
container[i] = j;
in >> j;
}
}
rhs = bigint(container);
return in;
}
// overloaded == operator compares if values are the same
bool bigint::operator ==(const bigint &rhs) {
for (int i = 0; i < CAPACITY; ++i) {
if (value[i] != rhs.value[i])
return false ;
}
return true ;
}
bigint bigint::operator +(bigint rhs)const { // Overload the addition operator
int carry = 0;
int temp = 0;
for (int i = 0; i < CAPACITY; ++i) {
carry = value[i] + rhs.value[i] + carry;
temp = carry % 10;
carry /= 10;
rhs.value[i] = temp;
}
return rhs;
}
//overload the [] operator, return each i'th up to CAPACITY
inline int bigint::operator [](int rhs)const {
for (rhs; rhs < CAPACITY; ++rhs){
return value[rhs];
}
}
#endif
Last edited on Sep 23, 2018 at 7:26pm UTC
Sep 23, 2018 at 7:25pm UTC
int digit = bi[0];
So that's setting the digit to
bi.value[0]
bi.value[0]
is zero, so that's setting digit to zero.
assert(digit == 10);
digit is not ten, so this assert will trigger.
This function here:
1 2 3 4 5 6
//overload the [] operator
inline int bigint::operator [](int rhs)const {
for (rhs; rhs < CAPACITY; ++rhs){
return value[rhs];
}
}
makes no sense at all. WHat is that loop for? This funtion returns the very first time round the loop. It is the same as:
1 2 3 4 5
//overload the [] operator
inline int bigint::operator [](int rhs)const
{
return value[rhs];
}
Last edited on Sep 23, 2018 at 7:31pm UTC
Sep 23, 2018 at 7:25pm UTC
Also, it is a debug error so maybe something with memory allocation?
-talemache
Last edited on Sep 23, 2018 at 7:28pm UTC
Sep 23, 2018 at 7:34pm UTC
No, it's not anything to do with memory. It's because you set value[0] to 0, and then you assert that it's 10.
Sep 23, 2018 at 7:57pm UTC
For the operator[] I am trying to return each array element from the rhs up to capacity, I thought a for loop would cause that. On changing bi[10], isn't that just changing the array index of digit to 10?
Sep 23, 2018 at 8:03pm UTC
For the operator[] I am trying to return each array element from the rhs up to capacity
A function can only return once.
On changing bi[10], isn't that just changing the array index of digit to 10?
I don't understand.
digit is an int.
digit doesn't have an "array index".
Sep 23, 2018 at 8:13pm UTC
Here is an example of code that works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{
// Setup
bigint bi(5);
// Test
int digit = bi[0];
// Verify
assert(bi == 5);
assert(digit == 5);
std::cout << bi << "_____" << digit << std::endl;
}
I see that it isn't an array, forget I said that.
Last edited on Sep 23, 2018 at 8:15pm UTC
Sep 23, 2018 at 8:28pm UTC
bigint bi(5);
After this, value[0] = 5%10 which is 5.
bigint bi(10);
After this, value[0] = 10%10 which is 0.
Sep 23, 2018 at 8:39pm UTC
I see, so it is giving me a remainder of 0. I will try to figure out how to change the code without messing up the previous part of the assignment. Will post later, thanks.
Talemache~
Topic archived. No new replies allowed.