Join 136,466 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,535 people online right now. Registration is fast and FREE... Join Now!
Hello guys, i have a problem using C programming language, in this exercise. I am required to prompt user to enter a initial train station (A, B, C, D or E) and enter final train station (A, B, C, D or E) and calculate the total distance between the selected initial station and final station.
So what i did was i used the conditional statements if-else else-if to do it. And my basic idea was A=1, B=2, C=3, D=4, E=5 for the initial station and A=1, B=2, C=3, D=4, E=5 for the final station. Then i find the final minus initial to get the stations in between. Which is illustrated below:
A--B--C--D--E
So i used the conditional statements which worked fine.
But I would like to ask, can i use an alternative method other than conditional statements? Because the exercise does not cover conditional statements, and i can't think of anything else except if-else else-if to do this.
Your help is greatly appreciated.
This post has been edited by shangyi: 1 Jul, 2008 - 02:20 AM
oh my! This never crossed my mind... This piece of code tells me to minus the two character converted into integer form to get the number of stations right? Thanks dude! although if the first station is A and the second station is D.. then this might not work, but i owe u many thanks!
I accidentally deleted the station_count declaration though
It checks the difference between the ASCII value, but be warned: If the user enters A to d, the difference will be wrong... So we need to convert them to upper case, to avoid this problem, right?
I accidentally deleted the station_count declaration though
It checks the difference between the ASCII value, but be warned: If the user enters A to d, the difference will be wrong... So we need to convert them to upper case, to avoid this problem, right?
yeah thanks dude! since it gets the value of the ASCII characters, so if the if the first station is A and the second station is D instead of B i would get D-A= 68-65=3 instead if 1, which is still a flaw though.. But never mind that..
Hey Dude.......Great ......... Can u try another program..............if u r free..... WAP to print Amstrong numbers form 1 to n. Carry on....................
Hey Dude.......Great ......... Can u try another program..............if u r free..... WAP to print Amstrong numbers form 1 to n. Carry on....................
One, there are several topics about Armstrong number generators right here at DIC. Search is your friend. Hint, use the % operator to break up a number into its component numbers.
Two, we are not a code writing service.
QUOTE
You Must Show Us Your Code.
Is shown inside every posting box whenever you click "add reply" or "New Topic".
I accidentally deleted the station_count declaration though
It checks the difference between the ASCII value, but be warned: If the user enters A to d, the difference will be wrong... So we need to convert them to upper case, to avoid this problem, right?
yeah thanks dude! since it gets the value of the ASCII characters, so if the if the first station is A and the second station is D instead of B i would get D-A= 68-65=3 instead if 1, which is still a flaw though.. But never mind that..
You have been a great help, and i appreciate it
Wait, why would that be a flaw? Isn't that exactly what is supposed to happen?