Tech Support Guy banner
Status
Not open for further replies.
1 - 4 of 4 Posts

· Registered
Joined
·
13 Posts
Discussion Starter · #1 ·
Hi guys,

I'm writing this code that allows people to sign into a system. the programme asks the user to enter their account number, and checks that it is 5 digits (since the account numbers all consist of 5 digits) but it always gives an error. whether there are 5 digits entered, or not. i thought it was because my cin buffer was somehow full. so i've tried cin.clear(), and cin.ignore(), and cin.sync(). but neither one of these seem to work. Please help!

there's the code. (this is only the sign in function. the whole code is much much bigger)

i know the reading from the file bit is not done yet, but let's get this cin problem sorted. that part is easy.

thank you all

Code:
void SignIn()

{
	char accno [5]; // Account Number
	char pw [MAX]; // Password

	file2.open ("Passwords.bin", ios::out);
	
	cin.clear(); 
	cout<< "Account Number: ";
	cin.getline(accno, 5);
	cin.getline(accno, 5);

	int k = strlen(accno);
	cout << "k= " << k << "\n\n\n" ;
	cout.flush() ;

	while (k != 5)
	{	
	//	system("cls");
		cin.clear(); 
		cout<< "This account number is not correct.\n";
		cout<< "Please Enter your account number again: ";
		cin.getline(accno, 5);

		k = strlen(accno);
		cout << "k= " << k << "\n\n\n" ;
		cout.flush() ;
	}
	system("cls");
	cout<< "Password: ";
	cin.getline(pw, MAX);

	system("cls");

	file2.seekg(0);
	bool found = false;
	while (!found && !file1.eof())
	{
	}

	 MainMenu();
}
 

· Registered
Joined
·
3,258 Posts
You need more room in your strings for terminators etc.
As a simple first run at it - try making all your 5's into 128's (but leave the strlen test for 5 chars in the result).
 

· Registered
Joined
·
3,258 Posts
Ignoring thoughts about CR/LF and the console -- in a simple ansi (single byte) character sense - you need 6 bytes not 5 to store a string of length 5
eg
char *accno = "12345";
is 6 bytes with the null terminator
accno[0] = '1'
.
.
accno[4] = '5'
accno[5] = '\0'
 
1 - 4 of 4 Posts
Status
Not open for further replies.
Top