#include <iostream>
usingnamespace 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...
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.
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...
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.
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 :)