Link to home
Start Free TrialLog in
Avatar of Tam39
Tam39

asked on

Teaching myself C using "Sams Teach Yourself C". I am doing a currency program I got off the web and need help having it to accept 1 input currency and make it do an error check for a valid entry.

The Question title is long and I'm sorry. I am trying to teach myself C programming using "Sams Teach Yourself C in 24 hours", which has taken me 3 weeks. I found a question on another website asking for a currency program with 5 different currency which needed to be converted to USD. I did that which is below,took me 2 weeks, now I am trying to do just one input currency which do the error check for correct entry and then display the USD. Now in Sams this seems to be the handling standard i/o, loops and the for while statements. I did ok I think but maybe unsure about adding for or while statements which I am not real clear on. My program is below and it actually works and would like to use the for or while statement and some loops. I think. I am sorry if it sounds silly but being disabled I found programming quite interesting. I am also working at the Sun site trying to teach myself JAVA, another story. Any help would be greatly appreciated.







/*include the library*/
#include <stdio.h>
/*define the five currencies used for the conversion*/
#define Currency1 1.63
#define Currency2 206.43
#define Currency3 71.50
#define Currency4 259.162
#define Currency5 6.27
/*function prototyping*/
void menu (int *);
float calculateConversion(int, float);
/*main function of the program*/
int main()
{
/*declare variables*/
float us;
float conversion;
int choice;
/*call user selection menu by reference*/
menu(&choice);
/*prompt user to enter amount of US to convert*/
printf("Please enter an amount in US Dolalrs to convert: ");
/*store user entry into memeory*/;
scanf("%f",&us);
/*call conversion function by value*/
conversion=calculateConversion (choice, us);
/*print results of conversion*/
printf("The amount of that currency in US Dollars is: %.2f\n", conversion);
/*display results*/
getchar();
/*terminate program*/
return 0;
}
/*function definition by reference*/
void menu(int *CHOICE)
{
/*local variables for menu()*/
int choice;
/*show options to user*/
printf("Welcome to the currency conversion program\n");
printf("Press number 1 to 5 to select a currency to convert\n");
printf("Press [1] for Germain Marks\n");
printf("Press [2] for Hungary Forint\n");
printf("Press [3] for Iceland Kronur\n");
printf("Press [4] for Sudan Dinars\n");
printf("Press [5] for South Africa Rand\n");
/*store choice in memory*/
scanf("%d", &choice);
/*set value of referenced variable*/
*CHOICE = choice;
}
/*function definition by value*/
float calculateConversion(int CHOICE, float us)
{
/*local variable for conversion*/
float conversion;
/*switch to user the case selected by user in reference funtion*/
switch (CHOICE)
{
case 1:
conversion=Currency1*us;
break;
case 2:
conversion=Currency2*us;
break;
case 3:
conversion=Currency3*us;
break;
case 4:
conversion=Currency4*us;
break;
case 5:
conversion=Currency5*us;
break;
}
/*returns the new value to the main function*/
return conversion;
}
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

It is strange the fact that these past two week a dozen guys are asking for currency convertion programs specifically for the C forum.
er..........
i didn't exactly get ur question
however, if u wanna add a loop to this program u have many alternatives .
One of them is that u can ask the user repeatedly about conversion using do while loop

to do this you have to add a do statement before calling th menu function and a while statement after the conversion routine

i.e
do
{
menu(&choice);
..
..
conversion  = ...
printf("..");
}while(choice >0 && choice <6);

however ,if this is not what u r looking for ,plz elaborate.
i hope this 'll help

ASKER CERTIFIED SOLUTION
Avatar of ankuratvb
ankuratvb
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of guynumber5764
guynumber5764

On the whole, if you do not have previous programming experience, and there is no specific motivator (a particular game or software package you want to work on) I would suggest learning Java first.  There are some things that you can't do in Java but many things are much easier.  More importantly, it will teach you the "object-oriented" paradigm that is the foundation of most modern programming.  If you learn C first, you may have to retrain yourself which can be harder than learning it right the first time whereas going from Java to C is more just a matter of looking a little bit closer at how things work..

C Loops:
Almost every loop in any language basically consists of a block of statements to be executed repeatedly and a reason to continue (or stop) looping.  Let's call those <repeated_block> and <loop_test> (where true means loop again).

C has 4 different loop constructs.  They all work about the same and they are all interchangeable.  The simplest form is the while loop:

WHILE

while (<loop_test>)
{
     <repeated_block>
}

As long as <loop_test> evaluates to true (not zero), <repeated_block> will be run forever.

WHILE...DO

do
{
     <repeated_block>
}
while (<loop_test>);

Exactly the same except the test has been moved to the bottom of the loop.  Forces the <repeated_block> to be run at least once.

REPEAT...UNTIL

repeat
{
     <repeated_block>
}
until (!<loop_test>);

Exactly the same as do...while but the test is reversed (notice the not (!) operator I stuck in there).  I only include it for completeness:  Use do...while(!...) instead.

FOR

for(<init>, <loop_test>; <incr>)
{
     <repeated_block>
}

This is one of the most cryptic constructs I have encountered in any language.
Functionally it is identical to:

<init>
while (<loop_test>)
{
     <repeated_block>
     <incr>
}

So
for(i=0; i<max; i++)
{...}

Could be written as
i=0;
while (i<max)
{
     ...
     i++;
}

Note that this is equivalent to Basic's:  FOR i = 0 TO (max-1)


SUMMARY

It is important to note that all of these loops are interchangeable, they just have slight differences that make them easier to read in certain circumstances.  In addition, there are conventions:
- while() is usually used where the number of iterations cannot be easily determined in advance or where the test may already be satisfied (ie: loop does does not need to be executed).  User input & file IO for example.
- for() is usually used where the loop follows a simple sequence:  1,2,3... or iterating through a string.
- do{}while() is occasionally used in place of a while() loop where the loop must be executed at least once.

Finally, a word on true/false values in C.  If an integer equals 0 it is said to be "false", if it  does not equal 0, it is considered TRUE.  Note that this is subtly different from the conventionally defined values of FALSE (==0) and TRUE (==1). (Consider the statements "while (6==TRUE) {...};" and "while (6) {...};"