Thread: Airline ticket problem

  1. #16
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >however when I enter the number 47715497443 (which is the test number) it gives off a not valid entry

    Integer overflow?
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
       printf("%d\n", INT_MAX);
       puts("47715497443");
       return 0;
    }
    
    /* my output
    2147483647
    47715497443
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #17
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by CrackerJack
    Code:
    #include <stdio.h>
    
    main ()
    {
    
    int ch=0, last=0;
    int num=0;
    int ticketval=0;
    
    	printf("Enter ticket number: ");
    	while ( ( ch = getchar() ) != '\n' )
    	{
    		ticketval = ch - '0' + (ticketval * 10);
    		num = ch;
    all you've done with num is load a useless value into it.
    What I was trying to get you to you've almost got, so...
    		num = ch - '0';    // convert the character to a number
    		ticketval = num + (ticketval * 10);  
    
    
    	}
    	last = num - 48;  // kill this line, you won't need it 
    
    // finally, you get to fix this IF based on the above
    	if ( (ticketval % 7) == last ) 
    		printf("VALID \n");
    	else
    		printf("Not a valid ticket! \n");
    
    }
    Well that seemed to work for some numbers, but not all of the ones I tried. Man I am really confused on what other way to do this!!!

    WHAT AM I MISSING HERE? :Bangs head against wall:
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #18
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    I'm starting to really hate this language... lol

    OK...Walt I put your suggestion for changes into the complier and it brought up the same return as my last posted code. So it ran the same way!

    And yeah I guess it must be a overflow. I tried changing it to long int instead (b/c I thought that allowed for more numbers), however it did the same thing.

    I'm almost ready to give up and just do it the easy way our teacher suggested (without using characters). I like challenges, but this one is stressing me out...lol

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I tried changing it to long int instead (b/c I thought that allowed for more numbers)

    Not necessarily.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
       printf("%d\n",  INT_MAX);
       printf("%ld\n", LONG_MAX);
       puts("47715497443");
       return 0;
    }
    
    /* my output
    2147483647
    2147483647
    47715497443
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    For a number of that size, you may have to switch to a double, which would mean the % operator won't work.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #21
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Just out of interest CJ, can you elaborate on how your teacher suggested you do it?
    Demonographic rhinology is not the only possible outcome, but why take the chance

  7. #22
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If the ticket number is that large, then you may as well use a char array instead of an int. It's not like you're going to want to perform calculations with it anyway, eh, other than the mod validation.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #23
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Hammer
    If the ticket number is that large, then you may as well use a char array instead of an int. It's not like you're going to want to perform calculations with it anyway, eh, other than the mod validation.
    Now you got me. How do you perform a mod on a char array?

    Anyway, the fmod() function is standard so you can use that on a float
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #24
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by WaltP
    Now you got me. How do you perform a mod on a char array?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MOD_BY 7
    
    int mymod(char *data, size_t length)
    {
      int num = 0;
      while (length--)
      {
        num = ((num * 10) + (*data++ - '0')) % MOD_BY;
      }
      return num;
    }
    
    int main(void)
    {
      char account[] = "4771549744";
      
      printf ("%s mod %d is %d\n", account, MOD_BY, mymod(account, strlen(account)));
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #25
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    That's kool! Thanks Hammer
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM