Double to Int conversion issue

Hey, just wondering if someone could help me remedy the conversion issue I'm having. 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
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
//Steve Osborne, Using the Taylor Series

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

void Input(double& Prec,int& code);
int Factorial(int Number);
void Summation(double Prec,double& e,int& Iter);
int SetPrecision(double Prec);

int main(){
	double Prec;
	int code=1;
	double e=1;
	int DD=1;
	int Iter;
	cout<<"Steve Osborne, Program 12"
		<<"\nEuler's Number by Maclaurin Series";
	Input(Prec,code);
	while(code==1){
		double e=1;
		Summation(Prec,e,Iter);
		DD=SetPrecision(Prec);
		cout<<fixed<<setprecision(DD)<<e<<"     "<<Iter;
		Input(Prec,code);
	}
	return 0;
}
void Input(double& Prec,int& code){
	cout<<"\n\nPlease enter the precision of e in this format: 1E-9"
		<<"\nEnter the precision of e or 0 to quit: ";
	cin>>Prec;
	if(Prec==0){
		cout<<"\n\nGoodbye\n\n";
		code=0;
	}
	else{
		code=1;
	}
	return;
}
void Summation(double Prec,double& e,int& Iter){
	int N=1;
	double Term=1;
	Iter=0;
	while(Term>Prec&&Iter<1000){
		e+=Term;
		N++;
		Term=1./Factorial(N);
		Iter++;
	}
	return;
}

int Factorial(int Number){
	int Fact=1;
	for(int N=Number;N>1;N--){
		Fact*=N;
	}
	return Fact;
}
int SetPrecision(double Prec){
	int SetPrecision=1;
	SetPrecision=(-log10(Prec));//Here is where the problem is occurring, I have to have Prec as a double because of the required input format so I'm not sure how to fix the warning about the conversion issue.
	return SetPrecision;
}
You can ignore the warning, if you want. It's just the compiler reminding you that you're implicitly converting a double to an int. You can make it go away by making the conversion explicit:
SetPrecision=-(int)log10(Prec);

On a different subject: this is just convention, but it's best not to name variables with the first letter capitalized, except when its a single capital letter (e.g. 'A'). This is because most people expect that a capitalized identifier will be anything except a variable/object. Likewise, fully capitalized identifiers are assumed to be preprocessor macros or sometimes enums.
And absolutely never give a variable the same name as a function. I find lines 66 and 67 utterly jarring, even though I know what they really mean.
Thanks for the tips, especially the variable=function name, not sure why I did that lol. The (int) worked, I was wondering if you could explain why that solves the warning? And this is a homework assignment, I have to hand it in with no warnings or I get points taken off.
By making the conversion explicit, you're telling the compiler "I know that this conversion will lose data and I'm okay with that. Don't remind me".

I have to hand it in with no warnings or I get points taken off
That's not a very fair system. There's no way of knowing what compiler or compiler settings the other person will be using. What produces warnings in one compiler-compiler settings configuration may not produce warnings in a different configuration.
Ya, I brought that up to him at one point and was told "Well I'm using the same compiler as you, lose the warnings".

I had one more quick question, I'm trying to create an escape to my summation loop that will terminate the program, I have it all worked out in my code except for the fact that it's not actually terminating the program. I was hoping you could give me a tip on how to do so.

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
//Steve Osborne, Using the Taylor Series

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

void Header();
void Input(double& prec,int& code);
int Factorial(int number);
void Summation(double prec,double& e,int& iter,bool& escape);
int SetPrecision(double prec);
void OutResults(double e,double iter,int decimals);
void LoopEscape();

int main(){
	double prec;
	int code=1;
	bool escape=1;
	int iter;
	int decimals=1;
	Header();
	Input(prec,code);
	while(code==1){
		if(escape!=0){
			double e=1;
			Summation(prec,e,iter,escape);
			if(escape!=0){
				decimals=SetPrecision(prec);
				OutResults(e,iter,decimals);
				Input(prec,code);
			}	
		}
	}
	return 0;
}
void Header(){
	cout<<"Steve Osborne, Program 12"
		<<"\nEuler's Number by Maclaurin Series"
		<<"\nPlease enter the precision of e in this format: 1E-9";
	return;
}
void Input(double& prec,int& code){
	cout<<"\n\nEnter the precision of e or 0 to quit: ";
	cin>>prec;
	if(prec==0){
		cout<<"\n\nGoodbye\n\n";
		code=0;
	}
	else{
		code=1;
	}
	return;
}
int Factorial(int number){
	int fact=1;
	for(int n=number;n>1;n--){
		fact*=n;
	}
	return fact;
}
void Summation(double prec,double& e,int& iter,bool& escape){
	int n=1;
	double term=1;
	iter=0;
	while(term>prec&&iter<1000){
		if(iter<=999){
			e+=term;
			n++;
			term=1./Factorial(n);
			iter++;
			escape=1;
		}
		if(iter>999){
			LoopEscape();
                        escape=0; //This should terminate the program in my eyes, 
                                         //changes escape to a 0 which kicks out of the loop in main 
                                        //and should go to my return 0; statement, it's not doing it though.
		}
	}
	return;
}
int SetPrecision(double prec){
	int precision=1;
	precision=-(int)log10(prec);
	return precision;
}
void OutResults(double e,double iter,int decimals){
	cout<<"e = "<<fixed<<setprecision(decimals)<<e
		<<" (with "<<fixed<<setprecision(0)<<iter
		<<" iterations required)";
	return;
}
void LoopEscape(){
	cout<<"\n\nThe maximum number of iterations has been reached,"
		<<"\n(1000+) this will terminate the program."
		<<"\n\nGoodbye.\n\n";
	return;
}



Topic archived. No new replies allowed.