Solved

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.

Posted on 2004-09-24
4
333 Views
Last Modified: 2010-04-15
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;
}
0
Comment
Question by:Tam39
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
It is strange the fact that these past two week a dozen guys are asking for currency convertion programs specifically for the C forum.
0
 
LVL 6

Expert Comment

by:Amritpal Singh
Comment Utility
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

0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 500 total points
Comment Utility
Hi,

Going by your question,you want to validate the amount that the user enters.

scanf returns the number of items successfully read in so you can use the following additions to your code to validate the amount.

printf("Please enter an amount in US Dolalrs to convert: ");
//If valid entry then calculate and print result
if(scanf("%f",&us)==1)
{
  /*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*/
}
//else print invalid entry message
else
{
  printf("Invalid Amount");
}
getchar();
/*terminate program*/
return 0;
0
 
LVL 3

Expert Comment

by:guynumber5764
Comment Utility
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) {...};"

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now