ok, here's the code. sorry for the excessive copy pasted functions at the end, but hey.
look for the line in intersection that has a comment by it saying that this line is the one causing problems. I commented it out and the intersection function will run without crashing the program, but when I put it back in it starts crashing again.
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
void menu();
void intersection();
void unionAB();
void differenceAB();
void differenceBA();
void cartesianAB();
void cartesianBA();
void editA();
void editB();
void viewAB();
int responseA=0;
int responseB=0;
int min=0;
void string_input_a();
void string_input_b();
void output_value_a(int a);
void output_value_b(int a);
void output_value_i(int a);
void output_value_u(int a);
void output_value_d(int a);
int array_a[30]={0};
int array_b[30]={0};
int string_length_a;
int string_length_b;
int i[60];//={0};
int u[60]={0};
int d[60]={0};
int main()
{
int a_number=0;
int b_number=0;
string_input_a();
string_input_b();
int exitA=0;
int exitB=0;
menu();
return 0;
}
void menu()
{
int choice=0;
int leave=0;
cout<<"1. \t The intersection of the sets A and B." <<endl;
cout<<"2. \t The union of sets A and B." <<endl;
cout<<"3. \t The difference of sets A and B (A-B)." <<endl;
cout<<"4. \t The difference of sets B and A (B-A)." <<endl;
cout<<"5. \t The Cartesian product AxB." <<endl;
cout<<"6. \t The Cartesian product BxA." <<endl;
cout<<"7. \t Edit set A." <<endl;
cout<<"8. \t Edit set B." <<endl;
cout<<"9. \t View sets A and B." <<endl;
cout<<"10. \t Exit the program." <<endl;
do
{
cout<<"Make your decision:" <<endl;
cin>>choice;
if (choice<1||choice>10)
{
cout<<"Please enter a valid choice!" <<endl;
cin>>choice;
if (choice>=1&&choice<=10)
{
leave=1;
}
}
else
{
leave=1;
}
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
{}
continue;
}
}
while (leave!=1);
if (choice>=1&&choice<=10)
{
if (choice==1)
{
intersection();
}
else if(choice==2)
{
unionAB();
}
else if(choice==3)
{
differenceAB();
}
else if(choice==4)
{
differenceBA();
}
else if(choice==5)
{
cartesianAB();
}
else if(choice==6)
{
cartesianBA();
}
else if(choice==7)
{
editA();
}
else if(choice==8)
{
editB();
}
else if(choice==9)
{
viewAB();
}
else if(choice==10)
{
exit(1);
}
}
else
{
cout<<"Please input a valid choice." <<endl;
cin>>choice;
}
}
void intersection()
{
int number=0;
int count=0;
int intersect=0;
do
{
i[count]=32000;
count++;
}
while (count<30);
count=0;
do
{
do
{
if (array_a[number]==array_b[count])
{
i[intersect]=array_a[number]; //this is the line that seems to be causing the problems
intersect++;
}
number++;
}
while (number<30);
number=0;
count++;
}
while (count<30);
number=0;
count=1;
do
{
do
{
if ((i[number]==i[count])&&(number!=count))
{
i[count]=99999;
count++;
}
else
{
count++;
}
}
while (count<30);
number++;
count=0;
}
while (number<30);
cout<<"The intersection of A and B is:" <<endl;
intersect=0;
if (i[intersect]==99999)
{
cout<<"null set" <<endl;
}
else
{
cout<<i[intersect];
intersect++;
}
do
{
if (i[intersect]!=99999)
{
cout<<", " <<i[intersect];
intersect++;
}
}
while (intersect<30&&i[intersect]!=99999);
cout<<endl;
system("pause");
cout<<endl;
menu();
}
void unionAB()
{
int number=0;
int count=0;
int b_number=30;
do
{
u[count]=99999;
count++;
}
while (count<60);
count=0;
do
{
u[number]=array_a[number];
number++;
}
while (number<30);
number=0;
do
{
u[b_number]=array_b[number];
number++;
b_number++;
}
while (number<30);
number=0;
cout<<endl;
number=0;
count=30;
do
{
do
{
if (u[number]==u[count])
{
u[count]=99999;
}
count++;
}
while (count<60&&u[number]!=99999);
count=30;
number++;
}
while (number<30);
cout<<"The union of A and B is:" <<endl;
number=0;
if (u[number]!=99999)
{
cout<<u[number];
number++;
}
else
{
number++;
}
do
{
if (u[number]!=99999)
{
cout<<", " <<u[number];
}
number++;
}
while (number<60);
cout<<endl;
system("pause");
cout<<endl;
menu();
}
void differenceAB()
{
int count=0;
int number=0;
int increment=0;
int exit=0;
do
{
d[count]=99999;
count++;
}
while (count<60);
count=0;
do
{
d[count]=array_a[count];
count++;
}
while (count<30);
do
{
d[count]=array_b[increment];
count++;
increment++;
}
while (increment<30);
do
{
count=30;
do
{
if (d[number]==d[count])
{
d[number]=99999;
}
count++;
}
while (count<60);
number++;
}
while (number<30);
cout<<"The difference of A-B is:" <<endl;
number=0;
do
{
if ((d[number]<32000)&&(d[number]>-32000))
{
cout<<d[number];
number++;
exit=1;
}
else
{
number++;
}
}
while (exit==0);
do
{
if ((d[number]<32000)&&(d[number]>-32000))
{
cout<<", " <<d[number] ;
number++;
}
else
{
number++;
}
}
while (number<30);
cout<<endl;
system("pause");
cout<<endl;
menu();
}
void differenceBA()
{
int count=0;
int number=0;
int increment=0;
int exit=0;
do
{
d[count]=99999;
count++;
}
while (count<60);
count=0;
do
{
d[count]=array_b[count];
count++;
}
while (count<30);
do
{
d[count]=array_a[increment];
count++;
increment++;
}
while (increment<30);
do
{
count=30;
do
{
if (d[number]==d[count])
{
d[number]=99999;
}
count++;
}
while (count<60);
number++;
}
while (number<30);
cout<<"The difference of B-A is:" <<endl;
increment=0;
do
{
if (d[increment]!=99999)
{
cout<<d[increment];
increment++;
exit=1;
}
else
{
increment++;
}
}
while (exit==0);
do
{
if (d[increment]!=99999)
{
cout<<", " <<d[increment];
increment++;
}
else
{
increment++;
}
}
while (increment<30);
cout<<endl;
system("pause");
cout<<endl;
menu();
}
void cartesianAB()
{
int increment=0;
int count=0;
int display_count=0;
cout<<"The Cartesian product of AxB is:" <<endl;
cout<<"{";
do
{
do
{
if (array_a[increment]!=99999&&array_b[count]!=99999)
{
cout<<" (" <<array_a[increment] <<" , " <<array_b[count] <<") ";
count++;
display_count++;
}
else
{
count++;
}
if (display_count==6)
{
cout<<endl;
display_count=0;
}
}
while (count<30);
increment++;
count=0;
}
while (increment<30);
cout<<"}" <<endl;
system("pause");
cout<<endl;
menu();
}
void cartesianBA()
{
int increment=0;
int count=0;
int display_count=0;
cout<<"The Cartesian product of BxA is:" <<endl;
cout<<"{";
do
{
do
{
if (array_a[count]!=99999&&array_b[increment]!=99999)
{
cout<<" (" <<array_b[increment] <<" , " <<array_a[count] <<") ";
count++;
display_count++;
}
else
{
count++;
}
if (display_count==6)
{
cout<<endl;
display_count=0;
}
}
while (count<30);
increment++;
count=0;
}
while (increment<30);
cout<<"}" <<endl;
system("pause");
cout<<endl;
menu();
}
void editA()
{
string_input_a();
system("pause");
cout<<endl;
menu();
}
void editB()
{
string_input_b();
system("pause");
cout<<endl;
menu();
}
void viewAB()
{
int a_number=0;
int b_number=0;
cout<<"Set A:" <<endl;
do
{
if (array_a[a_number]!=99999)
{
cout<<array_a[a_number] <<" ";
a_number++;
}
else
{
a_number++;
}
}
while (a_number<30);
cout<<endl;
cout<<"Set B:" <<endl;
do
{
if (array_b[b_number]!=99999)
{
cout<<array_b[b_number] <<" ";
b_number++;
}
else
{
b_number++;
}
}
while (b_number<30);
cout<<endl;
a_number=0;
b_number=0;
system("pause");
cout<<endl;
menu();
}
void string_input_a()
{
#include <stdio.h>
#include <ctype.h>
using namespace std;
char input_string[220];
int string_length=0;
int a=0;
int i=0;
char read_char=0;
bool neg_flag=0;
bool first_digit=0;
int case_int=0;
int number_entered=0;
int digit_entered=0;
char buffer[1];
int all_purpose_counter=0;
do
{
array_a[all_purpose_counter]=99999;
all_purpose_counter++;
}
while (all_purpose_counter<=30);
all_purpose_counter=0;
cout<<"Please enter set A in set notation, with possible values between -31,999 and 31,999 (maximum of 30 values)" <<endl;
cin.getline(input_string, 220);
string_length=strlen(input_string);
i=0;
do
{
read_char=input_string[i];
if (read_char=='{') case_int=1;
if ((isdigit(read_char)!=0)) case_int=2;
if (read_char==',') case_int=3;
if (read_char=='-') case_int=4;
if ((read_char=='}')||(read_char==']')) case_int=5;
switch(case_int)
{
case 1:
neg_flag=0;
i++;
break;
case 2:
// convert read_char to integer
if (!first_digit)
{
number_entered=0;
first_digit=1;
}
buffer[0]=read_char;
digit_entered=atoi(buffer);
number_entered=number_entered*10+digit_entered;
i++;
break;
case 3:
// copy # into array
first_digit=0;
if (neg_flag)
{
array_a[a]=number_entered*-1;
}
else
{
array_a[a]=number_entered;
}
a++;
i++;
neg_flag=0;
break;
case 4:
neg_flag=1;
i++;
break;
case 5:
if (neg_flag)
{
array_a[a]=number_entered*-1;
}
else
{
array_a[a]=number_entered;
}
i++;
a++;
break;
}
}
while (i<string_length);
i=0;
do
{
if (((array_a[i]<-31999)||(array_a[i]>31999))&&(array_a[i]!=99999))
{
cout<<"Please input a valid set consisting of integers between -31,999 and 31,999" <<endl;
string_input_a();
}
i++;
}
while (i<a);
string_length_a=a;
i=0;
}
void string_input_b()
{
#include <stdio.h>
#include <ctype.h>
using namespace std;
char input_string[220];
int string_length=0;
int a=0;
int i=0;
char read_char=0;
bool neg_flag=0;
bool first_digit=0;
int case_int=0;
int number_entered=0;
int digit_entered=0;
char buffer[1];
int all_purpose_counter=0;
do
{
array_b[all_purpose_counter]=99999;
all_purpose_counter++;
}
while (all_purpose_counter<=30);
all_purpose_counter=0;
cout<<"Please enter set B in set notation, with possible values between -31,999 and 31,999 (maximum of 30 values)" <<endl;
cin.getline(input_string, 220);
string_length=strlen(input_string);
i=0;
do
{
read_char=input_string[i];
if (read_char=='{') case_int=1;
if ((isdigit(read_char)!=0)) case_int=2;
if (read_char==',') case_int=3;
if (read_char=='-') case_int=4;
if ((read_char=='}')||(read_char==']')) case_int=5;
switch(case_int)
{
case 1:
neg_flag=0;
i++;
break;
case 2:
// convert read_char to integer
if (!first_digit)
{
number_entered=0;
first_digit=1;
}
buffer[0]=read_char;
digit_entered=atoi(buffer);
number_entered=number_entered*10+digit_entered;
i++;
break;
case 3:
// copy # into array
first_digit=0;
if (neg_flag)
{
array_b[a]=number_entered*-1;
}
else
{
array_b[a]=number_entered;
}
a++;
i++;
neg_flag=0;
break;
case 4:
neg_flag=1;
i++;
break;
case 5:
if (neg_flag)
{
array_b[a]=number_entered*-1;
}
else
{
array_b[a]=number_entered;
}
i++;
a++;
break;
}
}
while (i<string_length);
i=0;
do
{
if (((array_b[i]<-31999)||(array_b[i]>31999))&&(array_b[i]!=99999))
{
cout<<"Please input a valid set consisting of integers between -31,999 and 31,999" <<endl;
string_input_b();
}
i++;
}
while (i<a);
string_length_b=a;
}
void output_value_a(int a)
{
if ((array_a[a]<1000)&&(array_a[a]>=0)&&(array_a[a]!=99999))
{
cout<<array_a[a];
}
if (((array_a[a]>=1000)&&(array_a[a]!=99999)))
{
cout<<array_a[a]/1000 <<",";
if ((array_a[a]%1000<10)&&(array_a[a]%1000>=0))
{
cout<<"00" <<array_a[a]%1000;
}
if ((array_a[a]%1000<100)&&(array_a[a]%1000>10)&&(array_a[a]%1000>=0))
{
cout<<"0" <<array_a[a]%1000;
}
if ((array_a[a]%1000<1000)&&(array_a[a]%1000>100)&&(array_a[a]%1000>=0))
{
cout<<array_a[a]%1000;
}
}
if (((array_a[a]<=-1000)&&(array_a[a]!=99999)))
{
cout<<array_a[a]/1000 <<",";
if ((array_a[a]%1000>-10)&&(array_a[a]%1000<0))
{
cout<<"00" <<(array_a[a]%1000)*-1;
}
if ((array_a[a]%1000>-100)&&(array_a[a]%1000<-10)&&(array_a[a]%1000<0))
{
cout<<"0" <<(array_a[a]%1000)*-1;
}
if ((array_a[a]%1000>-1000)&&(array_a[a]%1000<-100)&&(array_a[a]%1000<0))
{
cout<<(array_a[a]%1000)*-1;
}
}
}
void output_value_b(int a)
{
if ((array_b[a]<1000)&&(array_b[a]>=0)&&(array_b[a]!=99999))
{
cout<<array_b[a];
}
if (((array_b[a]>=1000)&&(array_b[a]!=99999)))
{
cout<<array_b[a]/1000 <<",";
if ((array_b[a]%1000<10)&&(array_b[a]%1000>=0))
{
cout<<"00" <<array_b[a]%1000;
}
if ((array_b[a]%1000<100)&&(array_b[a]%1000>10)&&(array_b[a]%1000>=0))
{
cout<<"0" <<array_b[a]%1000;
}
if ((array_b[a]%1000<1000)&&(array_b[a]%1000>100)&&(array_b[a]%1000>=0))
{
cout<<array_b[a]%1000;
}
}
if (((array_b[a]<=-1000)&&(array_b[a]!=99999)))
{
cout<<array_b[a]/1000 <<",";
if ((array_b[a]%1000>-10)&&(array_b[a]%1000<0))
{
cout<<"00" <<(array_b[a]%1000)*-1;
}
if ((array_b[a]%1000>-100)&&(array_b[a]%1000<-10)&&(array_b[a]%1000<0))
{
cout<<"0" <<(array_b[a]%1000)*-1;
}
if ((array_b[a]%1000>-1000)&&(array_b[a]%1000<-100)&&(array_b[a]%1000<0))
{
cout<<(array_b[a]%1000)*-1;
}
}
}
void output_value_i(int a)
{
if ((i[a]<1000)&&(i[a]>=0)&&(i[a]!=99999))
{
cout<<i[a];
}
if (((i[a]>=1000)&&(i[a]!=99999)))
{
cout<<i[a]/1000 <<",";
if ((i[a]%1000<10)&&(i[a]%1000>=0))
{
cout<<"00" <<i[a]%1000;
}
if ((i[a]%1000<100)&&(i[a]%1000>10)&&(i[a]%1000>=0))
{
cout<<"0" <<i[a]%1000;
}
if ((i[a]%1000<1000)&&(i[a]%1000>100)&&(i[a]%1000>=0))
{
cout<<i[a]%1000;
}
}
if (((i[a]<=-1000)&&(i[a]!=99999)))
{
cout<<i[a]/1000 <<",";
if ((i[a]%1000>-10)&&(i[a]%1000<0))
{
cout<<"00" <<(i[a]%1000)*-1;
}
if ((i[a]%1000>-100)&&(i[a]%1000<-10)&&(i[a]%1000<0))
{
cout<<"0" <<(i[a]%1000)*-1;
}
if ((i[a]%1000>-1000)&&(i[a]%1000<-100)&&(i[a]%1000<0))
{
cout<<(i[a]%1000)*-1;
}
}
}
void output_value_u(int a)
{
if ((u[a]<1000)&&(u[a]>=0)&&(u[a]!=99999))
{
cout<<u[a];
}
if (((u[a]>=1000)&&(u[a]!=99999)))
{
cout<<u[a]/1000 <<",";
if ((u[a]%1000<10)&&(u[a]%1000>=0))
{
cout<<"00" <<u[a]%1000;
}
if ((u[a]%1000<100)&&(u[a]%1000>10)&&(u[a]%1000>=0))
{
cout<<"0" <<u[a]%1000;
}
if ((u[a]%1000<1000)&&(u[a]%1000>100)&&(u[a]%1000>=0))
{
cout<<u[a]%1000;
}
}
if (((u[a]<=-1000)&&(u[a]!=99999)))
{
cout<<u[a]/1000 <<",";
if ((u[a]%1000>-10)&&(u[a]%1000<0))
{
cout<<"00" <<(u[a]%1000)*-1;
}
if ((u[a]%1000>-100)&&(u[a]%1000<-10)&&(u[a]%1000<0))
{
cout<<"0" <<(u[a]%1000)*-1;
}
if ((u[a]%1000>-1000)&&(u[a]%1000<-100)&&(u[a]%1000<0))
{
cout<<(u[a]%1000)*-1;
}
}
}
void output_value_d(int a)
{
if ((d[a]<1000)&&(d[a]>=0)&&(d[a]!=99999))
{
cout<<d[a];
}
if (((d[a]>=1000)&&(d[a]!=99999)))
{
cout<<d[a]/1000 <<",";
if ((d[a]%1000<10)&&(d[a]%1000>=0))
{
cout<<"00" <<d[a]%1000;
}
if ((d[a]%1000<100)&&(d[a]%1000>10)&&(d[a]%1000>=0))
{
cout<<"0" <<d[a]%1000;
}
if ((d[a]%1000<1000)&&(d[a]%1000>100)&&(d[a]%1000>=0))
{
cout<<d[a]%1000;
}
}
if (((d[a]<=-1000)&&(d[a]!=99999)))
{
cout<<d[a]/1000 <<",";
if ((d[a]%1000>-10)&&(d[a]%1000<0))
{
cout<<"00" <<(d[a]%1000)*-1;
}
if ((d[a]%1000>-100)&&(d[a]%1000<-10)&&(d[a]%1000<0))
{
cout<<"0" <<(d[a]%1000)*-1;
}
if ((d[a]%1000>-1000)&&(d[a]%1000<-100)&&(d[a]%1000<0))
{
cout<<(d[a]%1000)*-1;
}
}
}