-nan(ind) error message

i was just trynna make a test prog that accepts parameters from main(), but when i run the prog, test with below input:

test_cpp calc 4 * 3


it display error message:
-nan(ind)


here's the code:
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
#include <iostream>
#include <string>
#include <cmath>

std::string say_num(int);
double calc(double, char, double);
double pow_num(double);
std::string rvrse(char*);
std::string origin(std::string);

int main (int argc, char* argv[]) {
	if (argc > 2) {
		std::string u_inpt;
		u_inpt = argv[1];

		if (u_inpt == "calc") {
			std::cout << calc(atof(argv[2]), (char)argv[3], atof(argv[4])) << std::endl;
		}
	}
	std::cin.clear();
	std::cout << "press ENTER to close program.";
	std::cin.get();
	return 0;
}

double calc(double n1, char opr, double n2) {
	switch(opr) {
	case '+': {
		return (n1 + n2);
		break;
	}
	case '-': {
		return (n1 - n2);
		break;
	}
	case '/': {
		if (n2 == 0) {
			std::cout << "DIVISION BY 0!" << std::endl;
			return 0;
			break;
		}

		else {
			return (n1 / n2);
			break;
		}
	}
	case '*': {
		return (n1 * n2);
		break;
	}
	default: {
		std::cout << "Enter valid calculation...";
		break;
	}
	}
}

std::string say_num(int n) {
	//...
	return "";
}


anyone can explain what is that?
Last edited on
 
std::cout << calc(atof(argv[2]), (char)argv[3][0], atof(argv[4])) << std::endl;


argv[i] is a null-terminated string, so argv[3] is a pointer to a string so to get the entered * as a char you need to get the first char of the string.

Last edited on
seeplus wrote:
std::cout << calc(atof(argv[2]), (char)argv[3][0], atof(argv[4])) << std::endl;


argv[i] is a null-terminated string, so argv[3] is a pointer to a string so to get the entered * as a char you need to get the first char of the string.


thanks, it works!!
Last edited on
Topic archived. No new replies allowed.