Hello. I am trying to validate a customer number using isdigit. If the user accidently enters a character other than an integer, I want it to allow the user to try again. The problem is it only will check the first number and if that is a digit it it presumes it is ok. I want it to check all the numbers entered. I have it currently looping around the whole thing so I can keep testing input. Thank you (in anticipation)

#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;


int main()
{
char password [6]; //password is 6 digit long
int i;

cout << "enter your login : " ;
cin >> password;

for (i = 0; i < 6; i++)
{
	if (isdigit (password[i]))
	{cout << "\nThank you." << endl;}// it is an integer yay!
	else
		cout << "\nYou can only have a numeric login" << endl;
	cout <<"Enter your login: " << endl;
	cin >> password;
}
system("pause");
	return 0;
}

Recommended Answers

All 6 Replies

First, if you want a 6-digit password then the variable must be declared as 7 characters to allow room for the null-terminator that cin will add.

There are several ways to do this (and more efficient ones too), here is one of them.

bool flag = false;
for(i = 0; i < 6; i++)
{
  if( !idigit(password[i]) )
  {
    cout << "Wrong password\n";
    flag = true;
    break;
  }
}
if( flag)
{
  // go back and ask for password again
}
#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;

bool validate(char[] password, int len)
{
    for (i = 0; i < len; i++)
    {
	if (!isdigit (password[i]))
        return false;
    }
    return true;
}

int main()
{
    char password [7]; //password is 6+1('/0') digit long
    int i;

    cout << "enter your login : " ;
    cin >> password;

    while(!validate(password,7))
    {
        cout << "\nYou can only have a numeric login" << endl;
        cout <<"Enter your login: " << endl;
        cin >> password;
    }
    cout << "\nThank you." << endl;}// it is an integer yay!
    system("pause");
    return 0;
}

Thank you, since I am very new to this, all variations are appreciated. I still have the problem where it will not pick up if I have entered a letter. for example, if I enter 1234k I will get an error message, I feel because it is not 6 characters long, but if I type 12345k It will allow it through because it is the right length even though it has a letter. I have written ...

while (password < MIN_NUM && password > MAX_NUM)
{cout << "Your password must be 6 digits long." << endl;
cout <<"Enter your 6 digit password.: " ;
cin >> password;

to ensure they only type in 6 digits, I just need each of those digits validated, presumably with a loop, but I don't know how to implement it. Thank you again.

normally i do that with string. So user can enter whatever length/alpha/number they want. After that only write errorcheck function for the input.

Is it also possible to do it with atoi (I think that was what it is)?

Thank you all, your input has put me on the right track. I could not use len, since it also evaluated the null at the end which always returned and error. This is the finished code. Thanks again

#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <string>
#include <iomanip>
 
using namespace std;
 
int main()
{
    char password [7]; //password is 6+1('/0') digit long
	int len;
    bool validate(char[],int);
	//int strlen(char password[]);

	cout << "enter your login : " ;
    cin >> password;

	len = strlen(password); //This will get the exact length of the password typed in
  
    while(!validate(password, len))
    {
        cout << "\nYou can only have a numeric login" << endl;
        cout <<"Enter your login: " << endl;
        cin >> password;

		len = strlen(password);
     }
    cout << "\nThank you." << endl;
	system("pause");

}
	

	bool validate(char pass[], int len)
{
	int i = 0;
	
    for (i = 0; i < len; i++)
    {
	if (!isdigit (pass[i]))
        return false;
    }
    return true;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.