Question:
C programming question. How to put 'limits'?
NyQuil
2013-06-11 05:59:27 UTC
I don't exactly know if limit is the right word.. but here's some examples

Enter UserID: The limit should be no strings and chars allowed, if more than or less than 9 digits are entered, it'll loop and ask user to enter again.

Would you like to try again? [Y/N]: Only upper and lowercase 'y' and 'n' can be entered, if strings, numbers, or letters that are not upper and lowercase 'y' and 'n' are entered, it will loop and ask again.

Can you please put some example codes.

Thanks a million to those who'll help.
By the way, no arguments please, I'm super new to programming and arguments are very confusing to me.
Five answers:
MichaelInScarborough
2013-06-11 15:46:46 UTC
This is my solution: http://ideone.com/Is5v6E



This code incorporates your instructions with the exception that get_ninedigits has one parameter. The alternative would have been to allocate memory in get_ninedigits and return this allocated memory to the calling function (here main) and then free the memory, thereafter. I thought this could have been more confusing than passing a char array as a parameter.



This is the input for IDEOne.com

line 1 is correct. user presses y to continue

line 3 input is too short

line 4 input is too long

line 5 input contains a letter

line 6 input is okay

line 7 user terminates.



1 123456789

2 y

3 1234

4 1234567890

5 123a12312

6 123123123

7 n
lansingstudent09101
2013-06-11 18:06:03 UTC
This is called validation. It depends on what language and what libraries you're using how you do it.



In C console programming, since in most supported libraries that get text can only read the full line (or part of it based on a data type).



The best way to do this is (as you've actually alluded to) to put the input in a loop. It looks like this:



user input is unacceptable

while( user input is unacceptable )

{

Ask user for input;

Save it in a variable;



Check if user input is acceptable and update the bool.

}



There are some variations on this, javascript (for the web) and swing or other gui frameworks that let you capture each letter as its pressed can give you the ability to validate each time an event "keypress" happens, so you never get bad data past the GUI anyway.
roger
2013-06-11 16:15:38 UTC
You want to enter a nine digit number with no non-digit characters allowed.



This should help (untested code follows)



char User[100];

char again;

int j;

while(1){

printf("Enter UserID ");

scanf("%9s",User);



for(j=0;j<9j++;)

if(User[j]<'0' || User[j] > '9'){

printf("bad input try again\n");

break;

}

if(j==9) break;

printf("not enuf digits\nTry again? [Y/N] ");

again=ucase(getchar());

if(again!='Y')exit(1);

}
AnalProgrammer
2013-06-11 14:10:07 UTC
Try this little program.

int isnotvaliduserid(char userid[]) {

int i;

for (i = 0; i < 11; i++) {

if (userid[i] == '\0') {

if (i == 9) {

return 0;

} else {

return 1;

} //End if

} else {

if (!isdigit(userid[i])) {

return 2;

} //End if

} //End if

} //End if

return 3;

} //End isnotvaliduserid



int main() {

char userid[80];

printf("Enter your userid: ");

scanf("%s", &userid);

while (isnotvaliduserid(userid)) {

printf("That is not a valid userid, try again\n");

printf("Enter your userid: ");

scanf("%s", &userid);

} //End while

printf("The userid %s is valid\n");

return (0);

} //End main



Have fun.
Fadi
2013-06-11 13:09:15 UTC
you want to validate input type. Check out the link I've included below


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...