stuck on conversions

I'm doing an assignment, the assignment has us taking a string and converting it to a integer or double. We had to make a function that checks to make sure the number is valid, and I can't for the life of my figure out why mine never changes whether its valid or not.

Here is my header
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
#ifndef DOUBLE
#define DOUBLE

#include "Integer.h"

class Double
{

private:
	double data;
	bool isNaN(string s);
	bool NaN;

public:
	Double();
	Double(const Double &d);
	Double(double d);
	Double(const Integer &i);

	Double add(const Double &i);
	Double sub(const Double &i);
	Double mul(const Double &i);
	Double div(const Double &i);

	Double add(double d);
	Double sub(double d);
	Double mul(double d);
	Double div(double d);

	Double operator+(const Double &i);
	Double operator-(const Double &i);
	Double operator*(const Double &i);
	Double operator/(const Double &i);
	Double &operator=(const Double &i);
	bool operator==(const Double &i);
	bool operator!=(const Double &i);
	bool operator!=(double i);
	bool operator==(double i);
	Double &operator = (double i);

	double toDouble() const;
	void equals(double d);

	bool isNaN();
	void equals(string s);
	Double toString(const Double &i);
	Double &operator=(string s);
	Double(string s);
	void isDigit(string s);
};




#endif 


Here is my CPP file
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
void Double::equals(string s)
{
	NaN = this->isNaN(s);
	if (NaN == false)
	{
		stringstream ss;
		double i;
		ss << s;
		ss >> i;
		this->data = i;
	}

}
bool Double::isNaN()
{
	return this->NaN;
}

Double &Double::operator = (string s)
{
	stringstream ss;
	double i;
	ss << s;
	ss >> i;
	this->equals(i);
	return *this;
}

Double Double::toString(const Double &i)
{
	stringstream ss;
	ss.precision(6);
	ss << i.toDouble();
	double a;
	ss >> a;
	return a;
}

Double::Double(string s)
{
	this->equals(s);
}
bool Double::isNaN(string s)
{
	isDigit(s);
	if (NaN == false)
	{
		stringstream ss;
		double i;
		ss << s;
		ss >> i;
		this->data = i;
		return false;
	}
	else
	{
		data = 0;
		return true;
	}

}
void Double::isDigit(string s)
{
	for (int i = 0; i < s.length(); i++)
	{
		if (isdigit(s[i]) == false)
			NaN = true;
	}
}


Here is my main.cpp
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
int main()
{
	m.addMenu("1. Add doubles ", doubleAdd);
	m.addMenu("2. Subtract doubles ", doubleSub);
	m.addMenu("3. Multiply doubles ", doubleMul);
	m.addMenu("4. Divide doubles ", doubleDiv);
	m.addMenu("5. Exit", Exit);

	m.runMenu();

	return 0;
}
void doubleAdd()
{
	Double d1, d2;
	string a;
	cout << "Enter two strings with numbers" << endl;
	getline(cin, a);
	string b;
	getline(cin, b);
	d1.equals(a);
	d2.equals(b);
	if (d1.isNaN() == true)
	{
		cout << " bad" << endl;
	}
	else
	{
		cout << "good to go" << endl;
	}

	m.waitKey();



No matter what I input the NaN always is true.
It should return False if it's a number.
Last edited on
I don't see NaN being set to false anywhere in the code.
Wow Peter your right, gah I feel so stupid.
Hmm, if I change the isDigit function to
1
2
3
4
5
6
7
8
9
10
11
void Double::isDigit(string s)
{
	for (int i = 0; i < s.length(); i++)
	{
		if (isdigit(s[i]) == false)
			NaN = true;
		else
			NaN = false;
	}
	
}


It still always returns true.
Any other help?
Now the value of NaN only depends on the last character in the string. You want NaN to false only if all characters are digits.

But that doesn't explain why NaN is not false. You could print the string variables a and b to make sure they has the correct content. They should not contain any additional characters (not even spaces).
They both hold exactly whats entered, someone else mentioned that isdigit uses characters where as I'm passing it a string, and that may be whats wrong.
However I'm not quite sure how to pass it a character reference so I could use a for loop to iterate through to make sure every character is a number.
No, you are passing a character to isdigit. s[i] is the character at index i in s.
That's what I thought.
I'm so thoroughly lost on why this won't return correctly.
Hi,

And the decimal point in the input string, how will that go with std::isdigit ?

Would it not be better to see whether the stringstream fails or not? If it does, then it's not a double. Then it will accept doubles like +1.23e+2, and reduce your code :+)

Hope this helps you out a bit :+)
I'm not to sure what you mean IdeasMan.
The first part:

With Double::isDigit

if the input is 1.23 , the decimal point isn't a digit, is it?

The second part:

std::stringstream will fail if it's input is not a valid double. SO I am saying it's better to test that near line 50 of the class cpp file. Then you won't need Double::isDigit at all.

Look at the example near the end of this:

http://www.cplusplus.com/reference/ios/ios/good/


Btw, there is reference material, tutorials, articles at the top left of this page.
Topic archived. No new replies allowed.