I'm doing a program in C.. and one of the things I need its request to user an identification in this form:
RCddddd - d's must be numbers including 0;
So to check the ID inserted by user, I made this:
-------------------
for( ; ; )
{
printf("Insert your identification RCddddd (ddddd must be your Univ identification\n");
scanf("%s", id);
if (id[0] != 'R')
printf("Invalid identification...\n");
else if (id[1] != 'C')
printf("Invalid identification...\n");
else if (strlen (id) != 7 )
printf("Invalid identification...\n");
else if( isdigit (id[2]) == 0)
printf("Invalid identification...\n");
else if( isdigit (id[3]) == 0)
printf("Invalid identification...\n");
else if( isdigit (id[4]) == 0)
printf("Invalid identification...\n");
else if( isdigit (id[5]) == 0)
printf("Invalid identification...\n");
else if( isdigit (id[6]) == 0)
printf("Invalid identification...\n");
else
break;
}
------------------------------------
So my question is: Is there any way to make this in a better way? without so many conditions?
Regards
17 replies to this topic
#1
Posted 20 December 2011 - 01:50 PM
|
|
|
#2
Posted 20 December 2011 - 03:30 PM
Not sure if you mean is there logically a way to reduce the actual number of conditions, or just not use the ugly looking multiple if-elses. If the latter, use a switch statement: C Switch Statement
#3
Posted 20 December 2011 - 04:49 PM
For digits you could use a loop.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#4
Posted 20 December 2011 - 05:16 PM
for(int i=0;i<7;i++) jumps out as a better loop format, along with a sentinel to track errors.
#5
Posted 20 December 2011 - 05:57 PM
If using manual checking with 'if' statement, why not use a 'while' loop instead?
#6
Posted 21 December 2011 - 03:41 AM
But how I do a while inside of an "If" ? :confused:
#7
Posted 21 December 2011 - 11:53 AM
int i;
for (i= 0; i < 10; i++){
printf ("%d\r\n", i);
}
= int i;
i= 0;
while (i < 10){
printf ("%d\r\n", i);
i++;
}
= int i;
int true= 1;
i= 0;
while (true){
if (i < 10);
else break;
printf ("%d\r\n", i);
i++;
}
= int i;
int true= 1;
i= 0;
while (true){
if (i >= 10) break;
printf ("%d\r\n", i);
i++;
}
#8
Posted 26 December 2011 - 02:31 PM
CheckList said:
So my question is: Is there any way to make this in a better way? without so many conditions?
Hello, CheckList.
I can see that your program worked, and it wasn't as nearly compacted as it could have been.
In my own personal opinion: Yes. I do believe it could be written a lot cleaner, and more readable.
Here is how I might have written it, if I honestly had to (in C that is):
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int allDigits(char *id) {
for (unsigned int n = 2; n < strlen(id); n++) {
if (!isdigit(id[n])) {
return 0;
}
}
return 1;
}
int main() {
char id[7] = {0};
while (1) {
printf("Insert your identification RCddddd (ddddd must be your Univ identification\n");
scanf("%7s", &id);
/* Check the length first */
if (strlen(id) != 7) {
printf("Invalid identification...\n");
continue;
}
/* Check the first two characters */
if (strncmp(id, "RC", 2)) {
printf("Invalid identification...\n");
continue;
}
/* Check if all characters (but the first two) are digits! */
if (!allDigits(id)) {
printf("Invalid identification...\n");
continue;
}
}
return 0;
}
Do you agree that mine is easier to read, compared to yours? Or do you find mine more difficult?
Welcome! Good luck. :)
“You may be disappointed if you fail, but you are doomed if you don't try.”
- Beverly Sills
- Beverly Sills
#9
Posted 26 December 2011 - 03:40 PM
A minor improvement can be made to your code, Muted, in allDigits function. Instead of calling strlen() at end of each for loop iteration, you can save the size to another variable.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#10
Posted 26 December 2011 - 03:51 PM
Flying Dutchman said:
A minor improvement can be made to your code, Muted, in allDigits function. Instead of calling strlen() at end of each for loop iteration, you can save the size to another variable.
Hello, Flying Dutchman.
Thank you for pointing it out, however, that and other minor improvements were left for the OP to find and fix.
Another blatantly obvious one would be "++n" over "n++."
:P
“You may be disappointed if you fail, but you are doomed if you don't try.”
- Beverly Sills
- Beverly Sills
#11
Posted 26 December 2011 - 04:29 PM
Pre increment vs post increment generally doesn't matter on primitive data types. However, when working with iterators (C++) it's better to use pre increment. softwareramblings.com and stackoverflow.com
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#12
Posted 27 December 2011 - 12:14 PM
Muted said:
Another blatantly obvious one would be "++n" over "n++."
There's a difference?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









