Math problems, Pi and other things.

Hello, im new to C++ and am just trying to create a simple calculator. im having problems with my CapImp function. whenever its called it crashes the program, and since i put it in sometimes it will peform the main function continuosly without regards of waiting for the return value. any help on this? (im using microsoft visual C++ 2008 express). also any help on getting a better value of pi would be useful. thanks very much in advance.


note: btw, the formula im using is X = 1/(2Pi * Frequency * Capacitance)

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
#include <iostream>

void CapImp( int freq, int cap) //Calculates capacitance impedance
{
	double Pi=3.141592;
	int step1= (2 * Pi);
	int step2= (step1 * freq * cap);
	int step3= (1 / step2);
	std::cout << "Impedance = " << step3;
}
int CallMenu() //Draws the calculator menu.
{
	using namespace std;
	int cOpt;
	cout << "Please Select a calculator:\n";
	cout << "1. Capacitor Impedance (In ohms)\n";
	cout << "2. Inductor Impedance (In ohms)\n";
	cout << "3. Parallel Resistance (In ohms)\n";
	cout << "4. Series Capacitance (In Farads)\n";
	cout << "5. Series Inductance (In Henrys)\n";
	cout << "6. Quit\n";
	cout << "\n";
	cin >> cOpt;
	cout << "\n";
	if (cOpt == 1)
		{
			int freq, cap;
			cout << "Capacitor Impedance Calculator\n";
			cout << "Frequency= ";
			cin >> freq;
			cout << "Capacitance= ";
			cin >> cap;
			cout << "\n";
			CapImp(freq,cap);
			return 0;
		}
	if (cOpt == 2)
		{
			cout << "Inductor Impedance Calculator\n";
			return 0;
		}
	if (cOpt == 3)
		{
			cout << "Parallel Resistance Calculator\n";
			return 0;
		}
	if (cOpt == 4)
		{
			cout << "Series Capacitance Calculator\n";
			return 0;
		}
	if (cOpt == 5)
		{
			cout << "Series Inductance Calculator\n";
			return 0;
		}
	if (cOpt == 6)
		{
			return 1;
		}
	return 0;
}
int main()
{															//////////////////////////////////////
	std::cout << "Electronic Engineering Super Calc!!!!\n";	//Electronic Engineering Super Calc	//
	std::cout << "(for the lazy ones among us =P)";			//Courtesy of Mr. James Dardry =)	//
	std::cout << "Courtesy of Mr. James Dardry =)";			//////////////////////////////////////
	int qTrue=0;
	
	while (qTrue != 1) //repeats the application until the user chooses to exit. (when qTrue equals an int of 1)
	{
		std::cout << "\n\n";

		//////////////////////////////////////////////////////////////////////////////////////(DOWN)//
		//This function displays the option menu.													//
		//It also tells the program wether or not the user has chosen to quit.						//
		//if the user has chosen to quit then it will return an int value of 1, if not it returns 0.//
		/////////////////////////////////////////////////////////////////////////////////////////////
		qTrue=CallMenu();
	}
	return 0;
}
Last edited on
This function could crash only if it divides by 0 at some point, I think.
You're using a lot of ints there. Try using floats instead, since ints always round down.
changing them to floats seem to have stopped the crashing, however, when i use a decimal the program decides to repeatedly perform the while statement in the main() funct except it ignores all the if statements in CallMenu(), ive changed the return value of CallMenu() to return 1 when the if statements are ignored, so the program doesnt go into an infinite loop, however i cannot figure out why it does this, any suggestions?

debug is giving me this info:
'hello world.exe': Loaded 'C:\Users\James\Documents\Visual Studio 2008\Projects\hello world\Debug\hello world.exe', Symbols loaded.
'hello world.exe': Loaded 'C:\Windows\System32\ntdll.dll'
'hello world.exe': Loaded 'C:\Windows\System32\kernel32.dll'
'hello world.exe': Loaded 'C:\Windows\System32\KernelBase.dll'
'hello world.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcp90d.dll'
'hello world.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcr90d.dll'
'hello world.exe': Loaded 'C:\Program Files\CheckPoint\ZAForceField\Plugins\ISWSHEX.dll'
'hello world.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.4927_none_d08a205e442db5b5\msvcr80.dll'
'hello world.exe': Loaded 'C:\Windows\System32\msvcrt.dll'
'hello world.exe': Loaded 'C:\Windows\System32\user32.dll'
'hello world.exe': Loaded 'C:\Windows\System32\gdi32.dll'
'hello world.exe': Loaded 'C:\Windows\System32\lpk.dll'
'hello world.exe': Loaded 'C:\Windows\System32\usp10.dll'
'hello world.exe': Loaded 'C:\Windows\System32\advapi32.dll'
'hello world.exe': Loaded 'C:\Windows\System32\sechost.dll'
'hello world.exe': Loaded 'C:\Windows\System32\rpcrt4.dll'
'hello world.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.4927_none_d08a205e442db5b5\msvcp80.dll'
'hello world.exe': Loaded 'C:\Windows\System32\ole32.dll'
'hello world.exe': Loaded 'C:\Windows\System32\imm32.dll'
'hello world.exe': Loaded 'C:\Windows\System32\msctf.dll'
'hello world.exe': Loaded 'C:\Windows\System32\ntmarta.dll'
'hello world.exe': Loaded 'C:\Windows\System32\Wldap32.dll'
The program '[5336] hello world.exe: Native' has exited with code 0 (0x0).
Last edited on
@hamsterman,
I disagree. No need to use floats here (although dividing #.# by 0.0 doesn't cause a divide-by-zero) unless you want to print floats (which you seem not to).

"Use floats instead of ints because ints always round down"
Or you could just add .5 to the result each time and then store it in an int.

@omgBUNNY,
Let me give you an example. If you do 3 / 2 == 1.5, yes? Then if you were supposed to give an integer answer, the result (to a human) would be 2. But to a computer, it would be 1, because integer division is truncating (like hamsterman said).

So how do we solve this? We could use floats, but IMO they're too imprecise if you don't need them (some programs do; Scientific simulations and 3D graphics for example). Another way is to cast each number to a float (if you're dividing variables) or just add a .0 to each number, e.g. 3.0 / 2.0 and then and 0.5 to the result. Why? Well,
3 / 2 == 1
3.0 / 2.0 == 1.5 (but this will be truncated to 1 when you store it in an integer)
3.0 / 2.0 + 0.5 == 2 (which is an integer, so it won't be changed)
(float)3 / (float)2 + 0.5 == 2

As a working example (and the whole command line I entered into bash):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ cat >intdivision.cpp
#include <iostream>

int roundingDivision(int x, int y) {
    return ((float)x / (float)y + 0.5); /* No need to cast to int */
}

int main() {
    int x = 0, y = 0;
    std::cout << "X: ";
    std::cin >> x;
    std::cout << "\nY: ";
    std::cin >> y;

    std::cout << "X / Y (rounded) == " << roundingDivision(x, y) << std::endl;

    return 0;
}

$ g++ -o intdivision intdivision.cpp
$ ./intdivision 
X: 3

Y: 2
X / Y (rounded) == 2


changing them to floats seem to have stopped the crashing, however, when i use a decimal the program decides to repeatedly perform the while statement in the main() funct except it ignores all the if statements in CallMenu(), ive changed the return value of CallMenu() to return 1 when the if statements are ignored, so the program doesnt go into an infinite loop, however i cannot figure out why it does this, any suggestions?

Even though I used it, don't use std::cin because it doesn't do type-checking. Use a stringstream: http://www.cplusplus.com/reference/iostream/stringstream/
Last edited on
the problem with that solution is that adding 0.5 wont help. this calculator needs to be able to handle numbers as low as 1*10^-15 and has to be very accurate, if i were to add 0.5 to 1 when the number rounded down was 1.00000125 (e.g.12.5nF) it wouldnt be much use. are there any addition #include repositories or something that would allow greater control of so many decimal places?
you can use long double for such an accuracy

About your program.I copy-paste it on NetBeans and i changed freq, cap to double
in the CallMenu() and in the CapImp() function and it ran normally.

Entry values were freq = 0.88 and cap = 5.88 and the output was
Impedance = 0.0307582
AH, It works now! thank you people! you've been a great help!
you can use long double for such an accuracy


Really? How many place values are you trying to use.
a long double is too big in most cases...

Topic archived. No new replies allowed.