Converting ints and doubles and floats.

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>

using namespace std;

template <class T>
T funnyfunc(T val1, T val2, T val3);
template <class T>
T Harmonic(T val1, T val2, T val3);
template <class T>
T Arithmetic(T val1, T val2, T val3);
template <class T>
T Geometric(T val1, T val2, T val3);
void testvalue(int value, short unsigned int code);
void drawmenu(void);
void drawmenu1(void);

int main(void)
{
	double dnum1, dnum2,dnum3;
	int inum1, inum2,inum3;
	float fnum1, fnum2,fnum3;
	short unsigned int choice = 0; 
	while(choice!=4)
	{
		cout<<"please select from the following menu ";
		drawmenu();
		cin>>choice;
		try
		{
			testvalue(choice, 3);
			break;
		}

		catch (char* eString)
		{
			cout<< eString <<endl;
			cout<<" please re enter your menu choice ";
		}
	}

	if (choice == 4) 
	{
		cout <<"the program is over";
	}
	else if (choice == 1)
	{
		cout<<"please enter your first numeber ";
		cin>>inum1;
		dnum1 = inum1;
		fnum1 = inum1;
		cout<<"please enter your second numeber ";
		cin>>inum2;
		dnum2 = inum2;
		fnum2 = inum2;
		cout<<"please enter your third numeber ";
		cin>>inum3;
		dnum3 = inum3;
		fnum3 = inum3;
		cout<<endl;
	}
		
	else if (choice == 2)
	{
		cout<<"please enter your first numeber ";
		cin>>fnum1;
		dnum1 = fnum1;
		inum1 = fnum1;
		cout<<"please enter your second numeber ";
		cin>>fnum2;
		dnum2 = fnum2;
		inum2 = fnum2;
		cout<<"please enter your third numeber ";
		cin>>fnum3;
		dnum3 = fnum3;
		inum3 = fnum3;
		cout<<endl;
	}
		
	else if (choice == 3)
	{
		cout<<"please enter your first numeber ";
		cin>>dnum1;
		fnum1 = dnum1;
		inum1 = dnum1;
		cout<<"please enter your second numeber ";
		cin>>dnum2;
		fnum2 = dnum2;
		inum2 = dnum2;
		cout<<"please enter your third numeber ";
		cin>>dnum3;
		fnum3 = dnum3;
		inum3 = dnum3;
		cout<<endl;
	}
	enum IFDCAL {INTEGER = 1, FLOAT, DOUBLE};
	IFDCAL myOps = INTEGER;

		myOps  = IFDCAL(choice);
	switch(myOps)
	{
	case INTEGER:
		cout<<"the result of interger type value is " <<funnyfunc(inum1, inum2, inum3)<<endl;
		break;
	case FLOAT:
		cout<<"the result of float type value is " <<funnyfunc(fnum1, fnum2, fnum3)<<endl;
		break;
	case DOUBLE:
		cout<<"the result of double type value is " <<funnyfunc(dnum1, dnum2, dnum3)<<endl;
		break;

	default:
		cout<<"thank you for using !"<<endl;
	}

	while(choice!=4)
	{
		cout<<"please select what type of means you want ";
		drawmenu1();
		cin>>choice;
		try
		{
			testvalue(choice, 3);
			break;
		}

		catch (char* eString)
		{
			cout<< eString <<endl;
			cout<<" please re enter your menu choice ";
		}
	}

	enum MEAN {HARMONIC = 1, ARITHMETIC,GEOMETRIC};
	MEAN myOps1 = HARMONIC;

		myOps  = IFDCAL(choice);
	switch(myOps)
	{
	case HARMONIC:
		cout<<"the result of Harmonic mean is  " <<Harmonic(dnum1, dnum2, dnum3)<<endl;
		break;
	case ARITHMETIC:
		cout<<"the result of arithmetic mean is " <<Arithmetic(fnum1, fnum2, fnum3)<<endl;
		break;
	case GEOMETRIC:
		cout<<"the result of geometric mean is " <<Geometric(dnum1, dnum2, dnum3)<<endl;
		break;

	default:
		cout<<"thank you for using !"<<endl;
	}





}

template <class T>
T funnyfunc(T val1, T val2, T val3)
{
	return ((val1+val2)*val3);
}

template <class T>
T Harmonic(T val1, T val2, T val3)
{
	return (3/((1/val1)+(1/val2)+(1/val3)));
}

template <class T>
T Arithmetic(T val1, T val2, T val3)
{
	return ((val1 + val2 + val3)/3);
}

template <class T>
T Geometric(T val1, T val2, T val3)
{
	return ((val1*val2*vale3*)^(.3333));
}


void testvalue(int value, short unsigned int code)

{
	switch(code)
	{
	case 1:
		if(value < 0 )
			throw "error - value cannot be negative \n";
			break;
	case 2:
		if(value==0)
			throw "error you can't divided by zero \n";
		break;
	case 3:
		if (value < 1|| value > 4)
			throw "error - invalid menu selection";
		break;
	default:
		cout<<" unhandled exception";
		cout<<"program will stop";
		exit(1);
	}
}

void drawmenu(void)
{
	cout<<endl<<"\t1 - interger"<<endl;
	cout<<"\t2 - float" <<endl;
	cout<<"\t3 - double"<<endl;
	cout<<"\t4 - end this program"<<endl;

}

void drawmenu1(void)
{
	cout<<endl<<"\t1 - Harmonic"<<endl;
	cout<<"\t2 - Arithmetic" <<endl;
	cout<<"\t3 - Geometric"<<endl;
	cout<<"\t4 - end this program"<<endl;

}



Ok. so basically this code will ask you what data type you want to input, so for instance. I want double, Then i type in 1, 2, 4. Then i choose harmonic mean. so the answer should be 1.712 which is 12/7 yea? Well the problem is i cannot control what the harmonic mean function data type is going to take.

In short look at line 139 to 147. I hard code what data type the function is taking and that's not what i want.

E.G i want integer i type in 1, 2, 4. then i choose harmonic, i will display the answer in double version which is 1.712(since in line 140 i declared it) instead of the integer version of it which is what i want.

SO guys how to fix that?
You can explicitly indicate which version you want by putting it in the <angle brackets>. For example if you want the double version of Harmonic, you can just do Harmonic<double>(a,b,c);

That said... duplicating all your variables like this is kind of crazy. The whole point of templates is that you can avoid doing this. It's best to put all this variable-type stuff in a template and just invoke whichever one the user picks.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <typename T>
void runprogram()
{
  T val[3];

  cout << "please enter first number ";
  cin >> val[0];
  cout << "please enter second number ";
  cin >> val[1];
  cout << "please enter third number ";
  cin >> val[2];

  cout << "Harmonic:  " << Harmonic(val[0],val[1],val[2]);
}

int main()
{
  if(user_wants_integers)  runprogram<int>();
  if(user_wants_floats)   runprogram<float>();
  if(user_wants_doubles)  runprogram<double>();
}


Also, In your Geometric function -- ^ is not an exponent symbol. You'll need to use the pow() function instead. ^ has a completely different and unrelated meaning in C++


EDIT:

Also this is a poor use of exceptions. And you are throwing const char*s, not char*s.
Last edited on
No, that doesn't work. The harmonic is just one of the choice. In your previous example, i hardcore and the user cannot choose what mean they want to display.

The choice should be int, float, double

then after that drawmenu1() will let the user choose between
harmonic, geometric, algorithm.

other than that your example above is awesome, is there somehow i can make val[0] = a val[1] = b val[2] = C

then in the switch statement which is line 101 to 109 funnyfunc<int>(a,b,c)?
No, that doesn't work. The harmonic is just one of the choice.


It was just an example to show how to use a template. The point of that example was to show that you don't need to make different sets of variables for ints,floats,doubles. Instead you can just make a template.

If you want the user to select between harmonic/whatever then just have it go through another menu.

is there somehow i can make val[0] = a val[1] = b val[2] = C


Yeah, just rename them =P

I figured it was just easier to use an array but if you prefer individual vars then that's fine.


then in the switch statement which is line 101 to 109 funnyfunc<int>(a,b,c)?


If you want ints, then yeah.

But if you do this in a template like my above example, then you don't need to specify which funnyfunc you want because it will just use funnyfunc<T> (where T is whatever type the use chose).
Disch that's not the problem,
Val[0] [1] and [2] is in the runprogram() function , even if i rename it to a, b, c i cannot do funnyfunc<int>(a,b,c). Moreover, my main issue still present look here.
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
template <typename T>
void runprogram()
{
  T a,b,c;

  cout << "please enter first number ";
  cin >> a;
  cout << "please enter second number ";
  cin >> b;
  cout << "please enter third number ";
  cin >> c;

 
}

int main()
{

  darwmanu();
  if(user_wants_integers)  runprogram<int>();//I
  if(user_wants_floats)   runprogram<float>();
  if(user_wants_doubles)  runprogram<double>();
  drawmenu1();
  if(user_wants_Harmonic)  Harmonic<int>(a,b,c);
//t gives me an error because a, b , c is only declared in the function. 
// Assuming a, b, c works, this still isn't what i want, that is decease i am still hard coding and returning it an "int".
// I want it to be the data type that the user choose in drawmenu().
  if(user_wants_Geometric)   Geometric<float>();
  if(user_wants_Algorithm)  Algorithm<double>();
  


}


Last edited on
The idea of 'runprogram' is that you put the whole program in it (or most of it).

All that stuff that's in main that you need a, b, and c for -- put all of that in runprogram:

1
2
3
4
5
6
7
8
9
10
11
12

template <typename T>
void runprogram()
{
  T a, b, c;

  // .. blah blah, get a, b, c here

  drawmenu1();
  if(user_wants_harmonic)  Harmonic(a,b,c);  // will call float/int/double depending on what T is
  if(user_wants_geometric)  Geometric(a,b,c); // ditto
  // ... etc 
Last edited on
Topic archived. No new replies allowed.