# Airline ticket problem

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-23-2003
CrackerJack
Airline ticket problem
Promise this is the last time I'm going to pick your guys brain for a while..:p

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!"); }```
• 10-23-2003
Azuth
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.
• 10-23-2003
WaltP
Re: Airline ticket problem
Quote:

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

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...
• 10-23-2003
Azuth
Quote:

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?
• 10-24-2003
WaltP
Quote:

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.
• 10-24-2003
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?
• 10-24-2003
WaltP
Quote:

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.
• 10-25-2003
CrackerJack
Quote:

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.

Quote:

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
• 10-25-2003
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.

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.
• 10-26-2003
WaltP
Quote:

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?

Quote:

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.

• 10-26-2003
CrackerJack
I came up with this theory after reading your other advice about a million times...:p 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"); }```
• 10-26-2003
Hammer
>>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.
• 10-26-2003
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;         }         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!!! :o :mad:

WHAT AM I MISSING HERE? :Bangs head against wall:
• 10-26-2003
XSquared
Try putting ticketval /= 10 before the if statement.
• 10-26-2003
CrackerJack
Quote:

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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last