debug error in c++ pointer code

so i have this assignment that tells the user to input two numbers and lets him choose some basic operations(like a calculator) from a menu. The goal of this exercise is to help us get more familiar with pointers/pointers to functions/dynamic allocation...The professor prefers that we use the "malloc" and "calloc" commands.

There is a part I'm stuck on, the part of displaying the history. It works for the first execution, but gives a debugging error as the code proceeds. Is there anyone who can help me solve the issue?

c++ code:

#include <iostream>
using namespace std;
//structure for the history array
struct Operation{
float result;
int choice;
};


float add(float, float);
float sub(float, float);
float mult(float, float);
float divi(float, float);



int main(){
//size1=for 1st history array, size2=for 2nd history array, t=to check if an operation was done at least once
int choice,size1=0,t=0,i,size2=0;
//pointer to func
int(*operation)(int, int);

float a, b,result;
//pointers set to NULL
Operation *history1 = NULL, *history1i = NULL, *history2 = NULL, *history2i = NULL;

cout << "Enter 2 numbers a & b: ";
cin >> a >> b;
cout << "What would you like to do?" << endl << "1=Addition" << endl << "2=Subtraction" << endl << "3=Division" << endl;
cout << "4=Multiplication" << endl << "5=Display History" << endl << "6=Exit" << endl;

do{
cin >> choice;
//initialize the size of 1st array if a choice is valid
if (choice < 5 && choice >0)
size1++;
//initialize 1st array
history1 = (Operation*)calloc(size1, sizeof(Operation));
if (history1 == NULL){
cout << "History allocation failed."<<endl;
exit(0);
}
//menu
switch (choice){
case 1: result = (add)(a, b);
cout << endl << "The addition of " << a << " & " << b << " gives: " << result << endl;
break;
case 2:result = (sub)(a, b);
cout << endl << "The subtraction of " << a << " & " << b << " gives: " << result << endl;
break;
case 3:if (b != 0){
result = (divi)(a, b);
cout << endl << "The division of " << a << " & " << b << " gives: " << result << endl;
}
else
cout << "Can't perform the divison.";
break;
case 4:result = (mult)(a, b);
cout << endl << "The multiplication of " << a << " & " << b << " gives: " << result << endl;
break;
//display history
case 5:if (t == 1){
cout << endl << "Your history is: " << endl;
for (history2i = history2,i=1; (history2i - history2) < size2; history2i++,i++){
cout << "\t"<<i<<"- " << a;

if (history2i->choice == 1)
cout << " + ";
else if (history2i->choice == 2)
cout << " - ";
else if (history2i->choice == 3)
cout << " / ";
else
cout << " * ";

cout << b << " = " << history2i->result<<endl;
}
}
else
cout <<endl<< "You haven't done any calcualtions yet."<<endl;
break;
case 6:break;
default:cout << endl << "The option you entered is not valid." << endl;
break;
}
//if choice is valid
if (choice < 5 && choice >0){
//if at least one opertion executed copy the elements of history2 in history1
if (t == 1){
for (history1i = history1, history2i = history2; (history1i - history1) < size2; history1i++, history2i++){
history1i->result = history2i->result;
history1i->choice = history2i->choice;
}
history1i++;//in order to enter the new operation
}
//if no operation done
else{
history1i = history1;
}
history1i->result = result;
history1i->choice = choice;
}
//if at least one operation done=>history exists and not NULL
if (t == 1){
free(history2);//in order to create a new array with the new elements
history2 = NULL;
}
//if not operations done before increment t
if (choice < 5 && choice >0 && t == 0){
t++;
}
//if valid choice create history 2
if (choice < 5 && choice >0){
size2 = size1;
history2 = (Operation*)calloc(size2, sizeof(Operation));
if (history2 == NULL){
cout << "History allocation failed." << endl;
exit(0);
}
//store all the previous history including the new one
for (history2i = history2, history1i = history1; (history2i - history2) < size2; history2i++, history1i++){
history2i->result = history1i->result;
history2i->choice = history1i->choice;
}
}

free(history1);
history1 = NULL;

if (choice != 6)
cout << endl << "Choose another option: ";


} while (choice != 6);
return 0;
}

float add(float a, float b){
return a + b;
}
float sub(float a, float b){
return a - b;
}
float mult(float a, float b){
return a*b;
}
float divi(float a, float b){
return a / b;
}
Hi,

The main reason your code only displays history once because its logic supports that only..

instead of having so much of messy code you can use functions and stack data structure to manage history...

ps : please use code tags

pps: please use code indentations to make your code friendly for others

WELCOME TO cplusplus :)
Topic archived. No new replies allowed.