![]() |
|
#1
|
|||
|
|||
char variable problemsHey there all,
I would just like to know if soomeone can help me with the char variable, im very new to c++ and every book i have shows u exactly examples of integer. But i cant get my char to work. What am i doeing wrong. Im trying to make a simple login sytem. Evertime i try to complie it gives me error. is there another text variable i can use. The error the compiler gives me, [Warning] comparison is always false due to limited range of data type [Warning] character constant too long for its type . I have tried aother wayswith not using the bracket after variable name but then it said i cant point to interger or something like that here is the code CPP / C++ / C Code:
Last edited by LuciWiz : 08-Aug-2005 at 08:36.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#2
|
||||
|
||||
|
hi, heres whats wrong, after u understand what i said, u shud be able to correct it on your own already.
CPP / C++ / C Code:
Hope that helps =) __________________
People should read the rules and regulation before posting! The Best is yet to be... |
|
#3
|
|||
|
|||
|
Is that really the only way, to go trough an array. darn. I am a vb.net devolper and its a bit easier to do that, so there is no other variable..
Ill check it out thanx |
|
#4
|
||||
|
||||
|
In addition to what alcedo pointed out about the left term of the condition:
CPP / C++ / C Code:
* A C-string is simply a null-terminated character array, meaning that the last character is the null character '\0'. If you say you are trying to learn C++, why not use the C++ std::string class? Then you could write the if-condition the way you tried to above, since std::string takes advantage of the operator overloading allowed in C++. |
|
#5
|
||||
|
||||
|
mm just to add, strcmp() is case sensitive. thus a user would have to input capital A, dministrator before it is correct.
i suggest convert the array temporarily into all caps first, and compare it with ADMINISTRATOR. u can use this: CPP / C++ / C Code:
but thats not standard or use this: CPP / C++ / C Code:
Note that the result iterator must specify a destination that is large enough to accept all the modified values; here it is not a problem, since we're putting them back in the same positions. The tolower function (along with toupper, isdigit, and other useful stuff) is in the <cctype> library sources: http://www.msoe.edu/eecs/cese/resources/stl/string.htm http://www.informit.com/guides/conte...lus&seqNum=236 __________________
People should read the rules and regulation before posting! The Best is yet to be... |
|
#6
|
|||
|
|||
|
The std:string namespace was indeed what i was looking for. Working perfect now and alot easier than using the other methods.
Thnx for the fast response. See the best programmer coming along lol cheers people |
|
#7
|
||||
|
||||
|
Ar, Glad those information above helps...
__________________
People should read the rules and regulation before posting! The Best is yet to be... |
|
#8
|
||||
|
||||
|
Hello.
The problem you are encountering has to do with the way you use the string notation. It should be "Administrator", not 'Administrator'. The single quotes are used for character data (as in "single character"). Now, you might be tented to change the if to something like: CPP / C++ / C Code:
This however is not correct - or rather doesn't behave as you would expect. The condition would actually test the addresses in memory of UName and the constant "Administrator" for equality. What you want is to test their values. So you should use the strcmp() function (or strncmp()): CPP / C++ / C Code:
Pay attention to the fact that in case the value entered by the user is longer than the lenght of the variable you assign it too, there will be a buffer overrun. You should check for this. Also, "Administrator" is 13 characters long; you will need an extra one for the string termination character, so make UName at least 14 characters long: CPP / C++ / C Code:
The example I gave checks for the exact same value; writing "administrator" (observe the first letter is in lower case) will yield a false check. If you want to ignore case (although you probably don't), you can use strcasecmp() (or strncasecmp()). These last 2 functions I mentioned are probably not standard, so maybe my suggestion isn't all that great But I see you are using C++-style input/output. So, if your program is C++, I would encourage you to drop that old C-style character arrays (I can see the flames coming CPP / C++ / C Code:
Of course, this does not handle the ignore-case I mentioned earlier. So, you will have to include 2 aditional header: CPP / C++ / C Code:
Then, I would make the "ADMINISTRATOR" a string variable, and change any input received from the user to all-upper. This way, you would be comparing the same-cased strings (and the user wouldn't even know it CPP / C++ / C Code:
Now, you could type "adMiniSTRatoR" and still get the equality test to return true! One more thing: system("PAUSE"); might not be such a good idea?. Use CPP / C++ / C Code:
I had fun writing this Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#9
|
||||
|
||||
|
Damn, it appears I took to long to write my response
Oh, well... maybe you can use something... __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#10
|
|||
|
|||
String helpThanx guys for all your help, why coudnt my book be this easy
I just want to ask one stupid question again, How do i make when the user enter data at command promt, that it shows password charaacters and not what is actually typed in. Thank you |
Recent GIDBlog
Developing GUIs with wxPython (Part 3) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 17:36 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 18:35 |
| Compiling Errors | ToddSAFM | C++ Forum | 22 | 18-Dec-2004 11:42 |
| some I/O problems...again | cameron | C++ Forum | 3 | 03-Mar-2004 21:39 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The