crashing problem

this program keeps crashing and I'm not sure why.
I'm not getting any errors or warning from it and I've looked it over myself several times and I just don't see why it's crashing.

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
  #include <iostream>
#include <iomanip>
using namespace std;

int Divisor(int Top, int Bottom);
void InputData(int &Num1, int &Num2, int &Den1, int &Den2);
void CalculatedAnswers(int Num1, int Num2, int Den1, int Den2, int &Top, int &Bottom);
void DisplayAnswer(int Num1, int Num2, int Den1, int Den2, int Top, int Bottom);
void main()
{

	int Numerator1=0, Numerator2=0, Denominator1=0, Denominator2=0, TopAnswer=0, BottomAnswer=0, GCD;
	char Continue;

	do
	{
		InputData(Numerator1, Numerator2, Denominator1, Denominator2);
		CalculatedAnswers(Numerator1, Numerator2, Denominator1, Denominator2, TopAnswer, BottomAnswer);
		GCD=Divisor(TopAnswer, BottomAnswer);
		TopAnswer=TopAnswer/GCD, BottomAnswer=BottomAnswer/GCD;
		DisplayAnswer(Numerator1, Numerator2, Denominator1, Denominator2, TopAnswer, BottomAnswer);
		cout<<"Please enter Y to continue: "<<endl;
		cin>>Continue;
	}
	while (Continue=='Y'||Continue=='y');
}
int Divisor(int Top, int Bottom)
{
	int Temp;
	
	while( Bottom!= 0) 
	{
	Temp = Top % Bottom;
	Top=Bottom;
	Bottom=Temp; 
	}
	return Temp;
}
void InputData(int &Num1, int &Num2, int &Den1, int &Den2)
{
	cout<<"Please enter the first Numerator: "<<endl;
	cin>>Num1;
	cout<<"Please enter the first Denominator: "<<endl;
	cin>>Den1;
	cout<<"Please enter the second Numerator: "<<endl;
	cin>>Num2;
	cout<<"Please enter the second Denominator: "<<endl;
	cin>>Den2;
}
void CalculatedAnswers(int Num1, int Num2, int Den1, int Den2, int &Top, int &Bottom)
{
	Top=Num1*Den2+Num2*Den1;
	Bottom=Den1*Den2;
}
void DisplayAnswer(int Num1, int Num2, int Den1, int Den2, int Top, int Bottom)
{
	cout<<setw(4)<<Num1<<"     "<<setw(4)<<Num2<<"     "<<Top<<endl;
	cout<<"----  +  ----  =  ------"<<endl;
	cout<<setw(4)<<Den1<<"     "<<setw(4)<<Den2<<"     "<<Bottom<<endl;
}
Not getting ANY errors or warning? You NEED to get a different compiler that is up to standard!
I'm using visual studios 2012, as recommended by my professor (and paid for by my school) are you saying that there's an obvious error that the compiler should have recognized?
Last edited on
Main has to return an int. This is a part of the standard and any compiler that thinks otherwise should put itself in a waffle iron. Change main to return int and have it return 0, please. >_>

The runtime error is somewhere in your Divisor function. The exception is thrown because GCD becomes 0.

That's sort-of inevitable the way it's coded... at the end of that loop, bottom is assigned temp's value, and the loop's condition is that bottom != 0. Meaning that when the loop exits, that means that temp was also zero, meaning that the function returns 0.

-Albatross
Last edited on
I actually have been wondering about that. I'm taking C101 right now, literally just beginning to learn how to code, and the prof has us using void main for assignments, can you explain why it's better to have main return a 0 rather than just void it?

also, I see now, I should be returning Top, not Temp, from that function.
Main returns an int to inform the OS how it did, 0 being the code for "everything's good".

It's not just better for main to return an int, it's the C++ standard. Your code is not true C++ if main is void, and I'm pretty sure nobody cares that Microsoft thinks about this matter.

-Albatross
Topic archived. No new replies allowed.