You have to #include <limits> to use numeric_limits, or you can use any large number instead.

Validating character by character and using isalpha are extra work that isn't necessary when the functionality is built in to input streams.

01-26-2006

Ideswa

I already tried isdigit() but that only works on char. And thanks for all the help!

01-28-2006

Ideswa

My compiler is Borland. There's no error and I don't know what RTTI is :P I'm not far yet.

01-28-2006

CornedBee

Your main problem is that you think C++ has the same weak typing as PHP. But C++ has strong typing: a variable is declared with a type and keeps it forever.

You can't compare strings with ==, unless you use the std::string class.

cin doesn't read invalid values into a double in the first place - but it goes into an error state if it can't read correct input, which you have to solve.

01-28-2006

7stud

1) As Salem pointed out, what you are trying to do--even if it did work--would not properly validate the input. The type of a variable does not change, so checking its type does not in any way indicate what type of data the user entered.

2)

Quote:

I just started programming in C++ but I'm pretty experienced in PHP.

C++ ain't PHP.

Examine the output from this program:

Code:

#include <iostream>
using namespace std;

int main ()
{

//Pointer to an array of characters(an array of characters is known
//as a c-style string):

The reason for the different output has to do with the differences between pointers, character arrays, and how C++ handles string literals(i.e. anything between double quotes), like "double".

In C++, whenever you have a string literal, e.g. "double", on the right side of an equals sign, the compiler takes the string literal, slaps a '\0' character on the end of it and stores it in memory somewhere, and then returns its address. For instance, if you do this:

Code:

const char* myPointer = "double";

then myPointer is assigned the address in memory where the compiler stored the string "double\0". Additionally, the compiler only stores the string literal "double" in memory the first time it is encountered. Subsequently, when the compiler sees "double" again, it just returns the address where it first stored "double" in memory.

Now, depending on what's on the other side of the equals sign opposite the string literal, different things happen. When a char array is on the left side of the equals sign, e.g.

Code:

char str[] = "double";

The compiler goes to the memory location where "double\0" is stored and copies it character by character to a new memory location and names it "str". Then, if you try to compare str to "double" with ==, you are actually comparing the addresses of str and "double\0", and since they are at two different addresses in memory, the addresses aren't equal. To compare the values at those addresses, rather than the addresses themselves, you have to use the strcmp() function.

On the other hand, when you have a pointer variable on the left side of the equals sign opposite the string literal, e.g.

const char* myOtherPointer = "double";

the address of "double\0" is assigned to myOtherPointer. So, if you subsequently compare myOtherPointer to "double" using ==, you will once again compare addresses, but this time they will be equal.

Because of the "erroneous" output you are seeing in your program, it is apparent to me that the name() function must be returning a pointer to a char array containing "double" rather than a pointer directly to the string literal "double".

You will constantly get tripped up by things like that in C++ until you gain a lot more experience.

01-30-2006

Ideswa

I used daved's code and it works! :cool: , but 1 more thing: I have this code:

I expected system("cls"); would exit the program! But it doesn't: the while loop continues! How can I abort the program?

01-30-2006

Daved

If you are in main(), just return 0 or return 1 or whatever you want to return (0 usually means success, so you would use something non-zero, but most people don't really use their program's return value anyway).

If you are in a separate function, you can use exit(0) or exit(1) or whatever, or you can return from your function some value or exception to indicate that the function failed so that the calling code can handle it and exit the program correctly.