Thread: Airline ticket problem

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Airline ticket problem

    Promise this is the last time I'm going to pick your guys brain for a while..

    OK...the program is suppose to find out if a ticket number is valid or not. What happens is the last digit of number must match the remainder when the other digits - as a group - are divided by 7.

    It then says at the bottom not to read the number in a single step, but to read the digits one-by-one w/ getchar.

    Apprrantly I am doing something wrong though. Any advice?

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    char ch;
    int i=0;
    int count=0;
    
    	printf("Enter ticket number: ");
    	while ( ( ch = getchar() ) != '\n' )
    	{
    		count++;
    		i = 7 % ch;
    	}
    	
    	if ( (ch / count) == i )
    		printf("VALID");
    
    	else
    		printf("Not a valid ticket!");
    
    }

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I would have thought that you'd create an array, and read each digit into that array (perhaps with something to check it's a digit).

    Then you can do what you like with it, copy part of the array to a string and use atoi to get a number etc.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Airline ticket problem

    Originally posted by CrackerJack
    Promise this is the last time I'm going to pick your guys brain for a while..

    OK...the program is suppose to find out if a ticket number is valid or not. What happens is the last digit of number must match the remainder when the other digits - as a group - are divided by 7.

    It then says at the bottom not to read the number in a single step, but to read the digits one-by-one w/ getchar.

    Apprrantly I am doing something wrong though. Any advice?

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    char ch;
    int i=0;
    int count=0;
    
    	printf("Enter ticket number: ");
    	while ( ( ch = getchar() ) != '\n' )
    	{
    		count++;
    		i = 7 % ch;
    	}
    	
    	if ( (ch / count) == i )
    		printf("VALID");
    
    	else
    		printf("Not a valid ticket!");
    
    }
    You need to get the mod of the value of the ticket. Therefore in your first loop you need to convert the characters entered into the ticketno (an int)
    But you also need to save the last number (ones digit) for the comparison, also converted from a char to an int. Figure out how to do that without loosing the value when the \n is read.

    Then, after the input you do the calculating...

    Oh, and no array is necessary...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Oh, and no array is necessary...
    I have no doubt WaltP that your skills far exceed my own, and I can see that this could be done without an array. I have a few thoughts as to why I'd probably use an array for this (given the information at hand). It does seem like you went out of your way to point this out however and I would like to know why you'd avoid the use of the array, particularly when the original poster says that the digits must be read in one at a time and you want to work with different parts of the number.

    If I understand correctly if the ticket number was 6578935 then you'd want to divide 657893 by 7 then see if the remainder was 5?
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Azuth
    If I understand correctly if the ticket number was 6578935 then you'd want to divide 657893 by 7 then see if the remainder was 5?
    Yes. You are dealing with numbers. It just so happens that the input seems to be by character (which it also doesn't really need to be, but that's how CrackerJack started).

    The pseudo code is:
    Code:
    set ticketval  to 0
    loop:
        read a character ch
        if '\n', break
        convert ch to a number num
        add num to ticketval * 10
         back to loop
    No arrays. Only one chararacter value. This loop needs a little more to fully program his project but that's beyond the scope of this post CJ gets to work that part out.

    After CJ is done with the program, I'll post a solution if his is different than mine. Remind me.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    OK...after looking at your suggestions Walt I came up w/ this:

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    char ch;
    int num=0;
    int ticketval=0;
    
    	printf("Enter ticket number: ");
    	while ( ( ch = getchar() ) != '\n' )
    	{
    		ch = num;
    		ticketval = num * 10;
    	}
    	
    	if ( (ticketval % 7) == num )
    		printf("VALID");
    
    	else
    		printf("Not a valid ticket!");
    
    }
    However I am confused on how to hold the last digit? Cuz after reading my book it says that when getchar is executed it reads the ch then empty's, which would make my program void, correct?

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by CrackerJack
    OK...after looking at your suggestions Walt I came up w/ this:

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    char ch;
    int num=0;
    int ticketval=0;
    
    	printf("Enter ticket number: ");
    	while ( ( ch = getchar() ) != '\n' )
    	{
    		ch = num;
    		ticketval = num * 10;
    	}
    	
    	if ( (ticketval % 7) == num )
    		printf("VALID");
    
    	else
    		printf("Not a valid ticket!");
    
    }
    However I am confused on how to hold the last digit? Cuz after reading my book it says that when getchar is executed it reads the ch then empty's, which would make my program void, correct?
    You didn't run this code, did you? You read ch then replace it with num?

    Remember, you are reading in a character -- you need to convert it to a number.

    The last digit must be saved when you go back to read the return -- how would you do that?

    Think about the last two paragraphs and you'll find the solution is staring back at you. As a matter of fact, you may not realize it, but you wrote a portion of the code correctly and already have the solution. Just fix the read loop.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    You didn't run this code, did you? You read ch then replace it with num?

    Remember, you are reading in a character -- you need to convert it to a number.
    Yeah...I see where I messed up. With 'ch' in front it converts 'num' into a character type, right? So, by reverseing that I should be able to do the opposite and force 'ch' into a int variable.

    The last digit must be saved when you go back to read the return -- how would you do that?

    Think about the last two paragraphs and you'll find the solution is staring back at you. As a matter of fact, you may not realize it, but you wrote a portion of the code correctly and already have the solution. Just fix the read loop.
    See I think my idea is different from yours. Ok I already put the 'mod' test in after the loop, and then I should have to divide the 'num' w/ 10. That would put the remainder (that needs to be tested) at the end. Right? And then where would I put that?

    CJ

  9. #9
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    The char will be an integer value corresponsing to the assci character table. To convert a char digit to the corresponding int you'd probably need to do something like num=ch-48; This may be OS dependant however, I'm not sure. There may be a standard function to accomplish this.


    I understand your issue around having to divide the number by 10, but you'll have to do more than that, since if you lose the decimal place by forcing it into say a long int it will actually round the number I would imagine.

    You'll probably have to look at what order things get done in, and perhaps hold onto the last number until you know they've entered either another number or a '\n'. If it's another number you add the last one onto the ticket number and hold the most recent, if it's a '\n' you do your modulus on the ticket number and compare the remainder against the last number you held on to.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by CrackerJack
    Yeah...I see where I messed up. With 'ch' in front it converts 'num' into a character type, right?
    No. What you did was take the integer value in num (zero) and copy it to the character value setting ch to zero also.

    So, by reverseing that I should be able to do the opposite and force 'ch' into a int variable.
    By reversing it, you'll copy the value in ch (say '2' which is the number 50) into num, setting num to 50, an integer form of the character '2'

    See I think my idea is different from yours. Ok I already put the 'mod' test in after the loop, and then I should have to divide the 'num' w/ 10. That would put the remainder (that needs to be tested) at the end. Right? And then where would I put that?
    You could, but (assuming you make the read loop as described, and add one more piece to make it work correctly -- see Azuth's suggestion) what will the value of num be when you exit the loop?
    Originally posted by Azuth
    The char will be an integer value corresponsing to the assci character table. To convert a char digit to the corresponding int you'd probably need to do something like num=ch-48; This may be OS dependant however, I'm not sure. There may be a standard function to accomplish this.
    Not today. ASCII is ASCII. Subtracting 48 will give you the conversion. If you're worried about it, just subtract '0' instead.
    Last edited by WaltP; 10-26-2003 at 01:30 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    I came up with this theory after reading your other advice about a million times... It is seems as though it would work, but still does not. I have VS.net and I watched the values as they move through the loop. After the mod comes out...the char valve of 'num' should match up! Or at least it did in my tests. However, it gives off a not valid error!

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    char ch, last;
    int num=0;
    int ticketval=0;
    
    	printf("Enter ticket number: ");
    	while ( ( ch = getchar() ) != '\n' )
    	{
    		ticketval = num + (ticketval % 10);
    		num = ch;
    
    	}
    	last = num;
    
    	if ( (ticketval % 7) == last )
    		printf("VALID \n");
    
    	else
    		printf("Not a valid ticket! \n");
    
    }

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>ticketval = num + (ticketval % 10);
    This would have been more appropriate:
    >>ticketval = ch - '0' + (ticketval * 10);

    >>if ( (ticketval % 7) == last )
    What is this value of ticketval at this point in the code? Think about it carefully, is there something you need to do it before you mod it by 7?

    Also, ch and last need to be ints, not chars.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    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;
    	}
    	last = num - 48;
    
    	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:

  14. #14
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try putting ticketval /= 10 before the if statement.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by XSquared
    Try putting ticketval /= 10 before the if statement.
    See I tried that already, however when I enter the number 47715497443 (which is the test number) it gives off a not valid entry. Others that are smaller do, but after the second 4 the valve of the number gets all weird.

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