Airline ticket problem

This is a discussion on Airline ticket problem within the C Programming forums, part of the General Programming Boards category; Promise this is the last time I'm going to pick your guys brain for a while..
OK...the program is suppose ...

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.

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?

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.

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?

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.

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.

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!

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

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.