-nan

Hello, I'm getting -nan at the end of the console, even tho all doubles are 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
26
27
28
29
30
31
#include <iostream>
using namespace std;

int main()
{
	double m, n, p, q, r, s, t;

	double f = m + n / (p + q / (r + s / t));
	cout << "Please enter value for M: ";
	cin >> m;
	cout << "\nPlease enter value for N: ";
	cin >> n;
	cout << "\nPlease enter value for P: ";
	cin >> p;
	cout << "\nPlease enter value for Q: ";
	cin >> q;
	cout << "\nPlease enter value for R: ";
	cin >> r;
	cout << "\nPlease enter value for S: ";
	cin >> s;
	cout << "\nPlease enter value for T: ";
	cin >> t;
	cout << endl;

	(t==0, (r + s / t)==0, (p + q / (r + s / t))==0)? cout << "There was a division by 0. Please put different values.":
		cout << f << endl;
	

	return 0;
}
double m, n, p, q, r, s, t; Value of all those nimbers is undefined
double f = m + n / (p + q / (r + s / t)); Value of result is undefined because all operands are undefined.

Move line 8 -> after line 23.

By the way: Line 25 is not the way to determine a division by 0 (the damage is already done).

Are you aware what the comma operators are doing?
Eh, I guess not lol, and how do I fix line 25? It really doesn't work,
Correct me if I'm wrong ( and I probably am ) but would it be better to replace all ==0 to !=0 and switch the places of the true/false cout-s ?
Edit: (t == 0 || (r + s / t) == 0 || (p + q / (r + s / t)) Will this fix the coma issue ?
No. Use if instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool is_div_0 = false;
if(t != 0)
{
  const auto d = (r + s / t);
  if(d != 0)
    f = (p + q / d);
  else
    is_div_0 = true;
}
else
  is_div_0 = true;
if(is_div_0)
  cout << "There was a division by 0. Please put different values.";
else
  cout << f << endl;
Topic archived. No new replies allowed.