Jump to content

Better way to do this?? .. check inside

- - - - -

  • Please log in to reply
17 replies to this topic

#1
CheckList

CheckList

    Newbie

  • Members
  • Pip
  • 7 posts
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

#2
ash

ash

    Newbie

  • Members
  • Pip
  • 3 posts
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
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
For digits you could use a loop.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
for(int i=0;i<7;i++) jumps out as a better loop format, along with a sentinel to track errors.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
If using manual checking with 'if' statement, why not use a 'while' loop instead?

#6
CheckList

CheckList

    Newbie

  • Members
  • Pip
  • 7 posts
But how I do a while inside of an "If" ? :confused:

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
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
Muted

Muted

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts

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

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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
Muted

Muted

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts

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

#11
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

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