Trying to make a program to solve an equation for me

This is a very odd post, but I am making a program in order to predict how many games it would take to reach a certain goal in a video game.

I am trying to get a user to input numbers which will then be put into a formula and calculate the results of said formula automatically.

The formula is (((a*c)/b)-c)/(1-(d/b)) where the user inputs the letters

The code I came up with is below, but it does not want to output a number when I input numbers...

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

using namespace std;


int main()
{
	int c;
	cout << "Please enter the total amount of battles you have fought:";
	cin >> c;
	int a;
	cout << "Please enter your overall WN8:";
	cin >> a;
	int d;
	cout << "Please enter your recent WN8 (in past 60 days):";
	cin >> d;
	int b;
	cout << "Please enter your target WN8:";
	cin >> b;
	cout << "It will take you approximaxtely this amount of battles to reach your target:";
	int answer = (((a*c)/b)-c)/(1-(d/b));
	cout << answer << endl;

	return 0;
}


This is the code I came up with - just a warning, I have never coded before and I can imagine it is horrendous for all of you...

Any help is appreciated!
Last edited on
integer division truncates in c++, so (a*c)/b is an integer, and if B > A*C then it will give zero. You should use the 'double' type instead of int here, I believe.

does that work better?
Wait so if I've understood correctly do I just replace int with double?

I have given this a shot but for some reason it does not work - might help if I send the equation in its proper form.

a*b
( ----- ) - c
b
-------------------
d
(1 - --- )
b

Didn't format very well... but the a*b should be over the b all in brackets

And the d should be over the b
Last edited on
"does not work" is a useless problem description.

Please describe clearly, completely and precisely what the problem is. What behaviour are you seeing, and how does it differ from the behaviour you expect to see?
Well essentially it asks me to input the values a,b,c,d but then it does not output an answer:

It is blank after the statement:

"It will take you approximaxtely this amount of battles to reach your target:"

and then it shuts the program off.

I have changed it up a bit but this was to no avail:


#include <iostream>
#include <math.h>

using namespace std;


int main()
{
	double a, b, c, d;

	cout << "Please enter the total amount of battles you have fought:  ";
	cin >> c;
	cout << "Please enter your overall WN8:  ";
	cin >> a;
	cout << "Please enter your recent WN8 (in past 60 days):  ";
	cin >> d;
	cout << "Please enter your target WN8:  ";
	cin >> b;

	double x1 = (((a*c)/b)-c)/(1-(d/b));
	cout << "It will take you approximaxtely this amount of battles to reach your target :" << x1 << endl;
}


Essentially I am just trying to get it to output the answer of the formula I posted above when the user inputs the numbers. However, I have next to no knowledge of coding, so explaining things is a bit tricky as you might be able to tell...
http://www.cplusplus.com/forum/beginner/1988/#msg7263

Ask for another dummy input at the end and it will keep the window open until you've supplied one.
it puts out an answer for me.
are you running it in a window that vanishes after the program ends?
if so, add another cin statement at the end and it will pause until you enter a value there. It is better to run the program from an already open terminal/console window than a pop up instance so you can see your output. This requires having basic unix or windows command line skills, able to change to a folder and execute a program and simple tasks. On windows that means run 'cmd' from start->run and then cd folder will change directory, navigate to where your .exe file is from your build, then type programname (the .exe is implied) and it will run there.


use <cmath> not <math.h>. Math.h is a C header and it works but is not technically correct. Basic math does not require it anyway, its there for trig, logs, powers, etc sorts of things.
Last edited on
Thanks guys, managed to fix it and change <math.h> to <cmath> !

The final code I ended up with was:


#include <iostream>
#include <cmath>

using namespace std;


int main()
{
	double a, b, c, d;

	cout << "Please enter the total amount of battles you have fought:  ";
	cin >> c;
	cout << "Please enter your overall WN8:  ";
	cin >> a;
	cout << "Please enter your recent WN8 (in past 60 days):  ";
	cin >> d;
	cout << "Please enter your target WN8:  ";
	cin >> b;

	double x1 = (((a * c) / b) - c) / (1 - (d / b));
	cout << "It will take you approximaxtely this amount of battles to reach your target : " << x1 << endl;

	cin.get();
	cin.get();
	return 0;
}


Ran the numbers through a calculator and it works :)
You don't need cmath or math.h as the program doesn't use any math functions provided by these.
Ah right right, that makes sense my bad.
Topic archived. No new replies allowed.