Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Using basic codes.

2 Pages V  1 2 >  
Reply to this topicStart new topic

Using basic codes.

shangyi
1 Jul, 2008 - 01:47 AM
Post #1

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 16


My Contributions
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.
cpp

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

int main()
{
char start_station, end_station;
int start_value, end_value, station_count;

printf("Enter your initial station: ");
scanf("%c", &start_station);
fflush(stdin);

printf("Enter your final station: ");
scanf("%c", &end_station);
fflush(stdin);

if (start_station == 'A')
{
start_value = 1;
printf("You have entered initial Station A\n");
}
else if (start_station == 'B')
{
start_value = 2;
printf("You have entered initial Station B\n");
}
else if (start_station == 'C')
{
start_value = 3;
printf("You have entered initial Station C\n");
}
else if (start_station == 'D')
{
start_value = 4;
printf("You have entered initial Station D\n");
}
else if (start_station == 'E')
{
start_value = 5;
printf("You have entered initial Station E\n");
}
else
{
printf("Invalid initial station entered.\n");
}

if (end_station == 'A')
{end_value = 1;
printf("You have entered last station: Station A\n");}

else if (end_station == 'B')
{end_value = 2;
printf("You have entered last station: Station B\n");}

else if (end_station == 'C')
{end_value = 3;
printf("You have entered last station: Station C\n");}

else if (end_station == 'D')
{end_value = 4;
printf("You have entered last station: Station D\n");}

else if (end_station == 'E')
{end_value = 5;
printf("You have entered last station: Station E\n");}

else
{
printf("Invalid last station entered.\n");
}

station_count = end_value - start_value;

if (station_count < 0)
station_count = -(station_count);
else
station_count = station_count;

printf("\n\nTotal stations travelled : %d\n", station_count);


return 0;
}


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
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Using Basic Codes.
1 Jul, 2008 - 02:11 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
You could use a switch, like so:
cpp
#include <stdio.h>
#include <math.h>

int main()
{
char start_station, end_station;
int start_value, end_value, station_count;

printf("Enter your initial station: ");
scanf("%c", &start_station);
fflush(stdin);

printf("Enter your final station: ");
scanf("%c", &end_station);
fflush(stdin);

switch (start_station)
{
case 'A':
start_value = 1;
printf("You have entered initial Station A\n");
break;

case 'B':
start_value = 2;
printf("You have entered initial Station B\n");
break;

case 'C':
start_value = 3;
printf("You have entered initial Station C\n");
break;

case 'D':
start_value = 4;
printf("You have entered initial Station D\n");
break;

case 'E':
start_value = 5;
printf("You have entered initial Station E\n");
break;

default:
printf("Invalid initial station entered.\n");
}


switch (end_station)
{
case 'A':
end_value = 1;
printf("You have entered last station: Station A\n");
break;

case 'B':
end_value = 2;
printf("You have entered last station: Station B\n");
break;

case 'C':
end_value = 3;
printf("You have entered last station: Station C\n");
break;

case 'D':
end_value = 4;
printf("You have entered last station: Station D\n");
break;

case 'E':
end_value = 5;
printf("You have entered last station: Station E\n");
break;

default:
printf("Invalid last station entered.\n");
}

station_count = end_value - start_value;

if (station_count < 0)
station_count = -(station_count);
else
station_count = station_count;

printf("\n\nTotal stations travelled : %d\n", station_count);

return 0;
}

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

shangyi
RE: Using Basic Codes.
1 Jul, 2008 - 02:17 AM
Post #3

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 16


My Contributions
QUOTE(gabehabe @ 1 Jul, 2008 - 03:11 AM) *

You could use a switch, like so:
cpp
#include <stdio.h>
#include <math.h>

int main()
{
char start_station, end_station;
int start_value, end_value, station_count;

printf("Enter your initial station: ");
scanf("%c", &start_station);
fflush(stdin);

printf("Enter your final station: ");
scanf("%c", &end_station);
fflush(stdin);

switch (start_station)
{
case 'A':
start_value = 1;
printf("You have entered initial Station A\n");
break;

case 'B':
start_value = 2;
printf("You have entered initial Station B\n");
break;

case 'C':
start_value = 3;
printf("You have entered initial Station C\n");
break;

case 'D':
start_value = 4;
printf("You have entered initial Station D\n");
break;

case 'E':
start_value = 5;
printf("You have entered initial Station E\n");
break;

default:
printf("Invalid initial station entered.\n");
}


switch (end_station)
{
case 'A':
end_value = 1;
printf("You have entered last station: Station A\n");
break;

case 'B':
end_value = 2;
printf("You have entered last station: Station B\n");
break;

case 'C':
end_value = 3;
printf("You have entered last station: Station C\n");
break;

case 'D':
end_value = 4;
printf("You have entered last station: Station D\n");
break;

case 'E':
end_value = 5;
printf("You have entered last station: Station E\n");
break;

default:
printf("Invalid last station entered.\n");
}

station_count = end_value - start_value;

if (station_count < 0)
station_count = -(station_count);
else
station_count = station_count;

printf("\n\nTotal stations travelled : %d\n", station_count);

return 0;
}

Hope this helps smile.gif


Thanks for your reply but I'm not allowed to use switch too =(. im only allowed to use really basic things to do it
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Using Basic Codes.
1 Jul, 2008 - 02:43 AM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
How's this for lean and mean:
cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

int main()
{
char start_station, end_station;

printf("Enter your initial station: ");
scanf("%c", &start_station);
fflush(stdin);

printf("Enter your final station: ");
scanf("%c", &end_station);
fflush(stdin);

printf ("You chose %c as your starting position\n", start_station);
printf ("You chose %c as your destination\n", end_station);

station_count = start_station - end_station;
if (station_count < 0)
station_count = -(station_count);
printf("\n\nTotal stations travelled : %d\n", station_count);

return 0;
}

Hope this helps smile.gif

This post has been edited by gabehabe: 1 Jul, 2008 - 02:44 AM
User is offlineProfile CardPM
+Quote Post

shangyi
RE: Using Basic Codes.
1 Jul, 2008 - 02:54 AM
Post #5

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 16


My Contributions
QUOTE(gabehabe @ 1 Jul, 2008 - 03:43 AM) *

How's this for lean and mean:
cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

int main()
{
char start_station, end_station;

printf("Enter your initial station: ");
scanf("%c", &start_station);
fflush(stdin);

printf("Enter your final station: ");
scanf("%c", &end_station);
fflush(stdin);

printf ("You chose %c as your starting position\n", start_station);
printf ("You chose %c as your destination\n", end_station);

station_count = start_station - end_station;
if (station_count < 0)
station_count = -(station_count);
printf("\n\nTotal stations travelled : %d\n", station_count);

return 0;
}

Hope this helps smile.gif


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! biggrin.gif
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Using Basic Codes.
1 Jul, 2008 - 04:10 AM
Post #6

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Nope, it works...

I accidentally deleted the station_count declaration though blush.gif

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?
cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <ctype.h> // toupper()

int main()
{
char start_station, end_station;
int station_count;

printf("Enter your initial station: ");
scanf("%c", &start_station);
fflush(stdin);

printf("Enter your final station: ");
scanf("%c", &end_station);
fflush(stdin);

// convert them to upper case to ensure ASCII math is correct
start_station = toupper(start_station);
end_station = toupper(end_station);

printf ("You chose %c as your starting position\n", start_station);
printf ("You chose %c as your destination\n", end_station);

station_count = start_station - end_station;
if (station_count < 0)
station_count = -(station_count);
printf("\n\nTotal stations travelled : %d\n", station_count);

return 0;
}

User is offlineProfile CardPM
+Quote Post

shangyi
RE: Using Basic Codes.
1 Jul, 2008 - 04:39 AM
Post #7

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 16


My Contributions
QUOTE(gabehabe @ 1 Jul, 2008 - 05:10 AM) *

Nope, it works...

I accidentally deleted the station_count declaration though blush.gif

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?
cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <ctype.h> // toupper()

int main()
{
char start_station, end_station;
int station_count;

printf("Enter your initial station: ");
scanf("%c", &start_station);
fflush(stdin);

printf("Enter your final station: ");
scanf("%c", &end_station);
fflush(stdin);

// convert them to upper case to ensure ASCII math is correct
start_station = toupper(start_station);
end_station = toupper(end_station);

printf ("You chose %c as your starting position\n", start_station);
printf ("You chose %c as your destination\n", end_station);

station_count = start_station - end_station;
if (station_count < 0)
station_count = -(station_count);
printf("\n\nTotal stations travelled : %d\n", station_count);

return 0;
}



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 tongue.gif tongue.gif

User is offlineProfile CardPM
+Quote Post

rajesh_strg
RE: Using Basic Codes.
1 Jul, 2008 - 05:03 AM
Post #8

New D.I.C Head
*

Joined: 30 Jun, 2008
Posts: 9

Hey Dude.......Great .........
Can u try another program..............if u r free.....
WAP to print Amstrong numbers form 1 to n.
Carry on....................
User is offlineProfile CardPM
+Quote Post

polymath
RE: Using Basic Codes.
1 Jul, 2008 - 07:05 AM
Post #9

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 414



Thanked: 4 times
Dream Kudos: 500
My Contributions
QUOTE(rajesh_strg @ 1 Jul, 2008 - 09:03 AM) *

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".
User is online!Profile CardPM
+Quote Post

captainhampton
RE: Using Basic Codes.
1 Jul, 2008 - 07:28 AM
Post #10

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
http://www.cplusplus.happycodings.com/Begi...nts/code35.html
User is offlineProfile CardPM
+Quote Post

Einherjar
RE: Using Basic Codes.
1 Jul, 2008 - 10:55 AM
Post #11

D.I.C Head
**

Joined: 10 Feb, 2008
Posts: 73


My Contributions
QUOTE(shangyi @ 1 Jul, 2008 - 08:39 AM) *

QUOTE(gabehabe @ 1 Jul, 2008 - 05:10 AM) *

Nope, it works...

I accidentally deleted the station_count declaration though blush.gif

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?
cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <ctype.h> // toupper()

int main()
{
char start_station, end_station;
int station_count;

printf("Enter your initial station: ");
scanf("%c", &start_station);
fflush(stdin);

printf("Enter your final station: ");
scanf("%c", &end_station);
fflush(stdin);

// convert them to upper case to ensure ASCII math is correct
start_station = toupper(start_station);
end_station = toupper(end_station);

printf ("You chose %c as your starting position\n", start_station);
printf ("You chose %c as your destination\n", end_station);

station_count = start_station - end_station;
if (station_count < 0)
station_count = -(station_count);
printf("\n\nTotal stations travelled : %d\n", station_count);

return 0;
}



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 tongue.gif tongue.gif


Wait, why would that be a flaw? Isn't that exactly what is supposed to happen?
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Using Basic Codes.
1 Jul, 2008 - 11:04 AM
Post #12

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
That's right, D-A should be 3 unsure.gif
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/2/08 05:07PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month