Thread: Some help please :)

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    23

    Some help please :)

    Hey guys!

    I'm new to the forums and C overall

    I'm learning for a degree in Math and computer Sciences and I've stumbeled across something I Can't seem to debug... (First semester lol, a noob still).

    Well the program needs to get a list of IDs until it reaches EOF, and does some algorithm on it. if the ID is abcde, then ((a+b)*3+(c+d)*7)%10 should be like "e" in the original ID.

    The first input, everything goes ok but on the second one and so forth it seems as tho there is a problem.... my Array might be getting messed up?

    Some directions will be well appreciated

    Edit:

    just to make things clear:

    ID that is invalid - prints a warning.
    ID that doesn't follow the calculating rule - Printing it and saying it's incorrect.
    ID that is correct - doesn't do anything

    Here's the program:

    Code:
    int main()
    {
    int counter=0, id, fail, loop=2;
    int ids[5] = {0,0,0,0,0};
    int sum=0,last=0;
    
    
    
    
    
    
    printf("Enter list of IDs\n");
    while (loop==2)
    {
        counter=0;
        fail=0;
        id=0;
        sum=0;
        last=0;
    
    
        while (counter<5) //Gets 5 chars!!!
        {
            id=getchar();
            ids[counter] = (id-'0');
            if ((id)== EOF)
            {
                return 0;
            }
            if ((id<'0') || (id>'9'))
            {
                fail=2;
            }
            if ((fail==2) && (counter==4))
            {
                printf("invalid input, it should be 5 digits (0-9)\n");
                fail=0;
                counter=0;
                while (counter<5) //Array reset!!!
                {
                    ids[counter] = 0;
                    counter+=1;
                }
                counter=0;
                continue;
            }
    
    
            counter+=1;
        }
        counter=0;
        sum+= ((ids[0]+ids[1])*3);
        sum+= ((ids[2]+ids[3])*7);
        sum%=10;
        last= (ids[0] * 10000);
        last+=(ids[1] * 1000);
        last+=(ids[2] * 100);
        last+=(ids[3] * 10);
        last+=(ids[4]);
    
    
        if (sum==(ids[4]))
        {
            sum=0;
        }
        else
        {
            printf("incorrect id: %d\n", last);
            sum=0;
            last=0;
    
    
        }
         while (counter<5) //Array reset!!!
        {
                ids[counter] = 0;
                counter+=1;
        }
    }
      return 0;
    }
    Last edited by Gal Abir; 12-05-2012 at 11:23 AM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    How about
    Code:
    ......
            id=getchar();
            if (id == '\n')
                continue;
    ......
    or maybe using fgets() instead of getchar()?

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Quote Originally Posted by Shurik View Post
    How about
    Code:
    ......
            id=getchar();
            if (id == '\n')
                continue;
    ......
    or maybe using fgets() instead of getchar()?
    I don't know what's fgets()...

    and for some reason, that if (id == '\n') solved the problem, lol!

    Care to tell me why did it fix it?

    Thanks a lot!

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Wait, no lol.This input messes it up:
    00000 00000 12345 12348 12000

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Nvm, just solved it

    Added if (id==' ' || id == '\n')

  6. #6
    Registered User MykonBlu's Avatar
    Join Date
    Nov 2012
    Posts
    5
    Quote Originally Posted by Gal Abir View Post
    Wait, no lol.This input messes it up:
    00000 00000 12345 12348 12000
    Doesn't that mess it up because 12000 isn't a valid ID? For it to be valid it would be 12009.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Because each line ended with '\n' and program must pass it.
    If user inputs 12345, then program will get 6 symbols: 1 2 3 4 5 '\n'

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    MykonBlu - that's right. Because the ID wasn't valid it should print "Invalid ID"

    Shurik - I see. But when I entered the first 5 numbers, it shouldn't have gotten out of the loop? I guess what happened is that it got 6 chars instead of 5 and the \n somehow messed up the array with it's Ascii number?

  9. #9
    Registered User MykonBlu's Avatar
    Join Date
    Nov 2012
    Posts
    5
    On a side note, the following is a slightly tidier way of resetting your ID array to 0's:

    Code:
    memset (ids, 0, sizeof(ids));
    This is a function from the string.h header file so don't forget to include that too

  10. #10
    Registered User MykonBlu's Avatar
    Join Date
    Nov 2012
    Posts
    5
    Quote Originally Posted by Gal Abir View Post
    MykonBlu - that's right. Because the ID wasn't valid it should print "Invalid ID"
    Hmm it seems to work fine for me when I try those values, with the correct errors being returned.

  11. #11
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Yep, not everything is ok!

    I have a different problem tho, with a diff code... (actually 2 codes lol. Only 6 C lessons and I'm already messed up lol!)

    Can I post them too, or are you tired?

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    P.S -

    Thanks for that array reseting way!

    Also, what is fgets() ?

  13. #13
    Registered User MykonBlu's Avatar
    Join Date
    Nov 2012
    Posts
    5
    fgets() is a function used for reading characters from a file. So you could have a list of IDs in a file to be tested.

    Feel free to post away, and in future threads, if possible could you try and title the thread so that it relates more to the problem rather than asking for help

  14. #14
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Quote Originally Posted by MykonBlu View Post
    fgets() is a function used for reading characters from a file. So you could have a list of IDs in a file to be tested.

    Feel free to post away, and in future threads, if possible could you try and title the thread so that it relates more to the problem rather than asking for help
    Sure thing mate!

    I'll try a bit more on my own after some Algebra lol. Thanks

Popular pages Recent additions subscribe to a feed