GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Aug-2005, 08:32
Swys Swys is offline
New Member
 
Join Date: Aug 2005
Posts: 4
Swys is on a distinguished road

char variable problems


Hey 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:
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
     char UName[13];
     char PWord[8];
    
    cout << "Please enter Username :    ";
    cin >> UName;    
    if(UName[13] == 'Administrator') {
            cout << "Please enter Password :   ";
            cin >> PWord;     
            }
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited by LuciWiz : 08-Aug-2005 at 08:36. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 08-Aug-2005, 08:37
alcedo's Avatar
alcedo alcedo is offline
Member
 
Join Date: Jul 2005
Location: Singapore
Posts: 198
alcedo will become famous soon enough
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:
//an array is defined as such
char temp[10];
cin>>temp;
//user keys in e word "admin";
/*this is how data are stored in e array:
temp[0] == a;
temp[1] == d;
..
..
..
temp[4] == n;
thus, in your coding, UName[13] would only point to A SINGLE CHARACTER, and not an entire string...
so u have to compre char by char using a for loop
*/

Hope that helps =)
__________________
People should read the rules and regulation before posting!

The Best is yet to be...
  #3  
Old 08-Aug-2005, 08:55
Swys Swys is offline
New Member
 
Join Date: Aug 2005
Posts: 4
Swys is on a distinguished road
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  
Old 08-Aug-2005, 08:57
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
In addition to what alcedo pointed out about the left term of the condition:
CPP / C++ / C Code:
if(UName[13] == 'Administrator') {
there are two other things to note about the line above:
  1. The proper way to compare two C-strings* is to use the standard C function strcmp().
  2. The second term of the if-condition is what? By using single quotes, you have told the compiler it is a single character, but inside appears to be a string; I think you mean to use double quotes.

* 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  
Old 08-Aug-2005, 09:32
alcedo's Avatar
alcedo alcedo is offline
Member
 
Join Date: Jul 2005
Location: Singapore
Posts: 198
alcedo will become famous soon enough
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:
#include <string>
string myString("HeLlO, WoRlD!");
strupr((char *) myString.c_str());

but thats not standard

or use this:
CPP / C++ / C Code:
string str22 = "This IS a MiXed CaSE stRINg";
transform (str22.begin(),str22.end(), str22.begin(), tolower);
cout << "[" << str22 << "]" << endl; // [this is a mixed case string]

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  
Old 08-Aug-2005, 09:33
Swys Swys is offline
New Member
 
Join Date: Aug 2005
Posts: 4
Swys is on a distinguished road
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  
Old 08-Aug-2005, 09:42
alcedo's Avatar
alcedo alcedo is offline
Member
 
Join Date: Jul 2005
Location: Singapore
Posts: 198
alcedo will become famous soon enough
Ar, Glad those information above helps...
__________________
People should read the rules and regulation before posting!

The Best is yet to be...
  #8  
Old 08-Aug-2005, 09:48
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 918
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
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:
	if( UName == "Administrator") 
	{
		cout << "Please enter Password :   ";
		cin >> PWord;     
	}

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:
	if( ! strcmp(UName, "Administrator")) 
	{
		cout << "Please enter Password :   ";
		cin >> PWord;     
	}

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:
char UName[14];

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:
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;

int main()
{
	//char UName[14];
	//char PWord[8];
	string UName;
	string PWord;
	cout << "Please enter Username :    ";
	cin >> UName;    

	if( UName == "Administrator" ) 
	{
		cout << "Please enter Password :   ";
		cin >> PWord;     
	}
	return 0;
}

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:
#include <algorithm>
#include <cctype>

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:
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using std::cin;
using std::cout;
using std::string;

int main()
{
	string UName;
	string PWord;
	cout << "Please enter Username :    ";
	cin >> UName;    
	
	string admin = "ADMINISTRATOR";
	std::transform(UName.begin(), UName.end(), 
		UName.begin(), (int(*)(int))std::toupper); // change the string to all upper

	if( UName == admin ) 
	{
		cout << "Please enter Password :   ";
		cin >> PWord;     
	}

	return 0;
}

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:
cin.get();
instead.

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  
Old 08-Aug-2005, 09:49
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 918
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
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  
Old 08-Aug-2005, 10:00
Swys Swys is offline
New Member
 
Join Date: Aug 2005
Posts: 4
Swys is on a distinguished road

String help


Thanx 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 GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:32.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.