Writing a Currency Conversion Program in Basic C

Status
Not open for further replies.

Carrie Robinson

Registered Member
I'm need some help in writing a Currency Conversion Program in Basic C.

I have started the program but I don't know how to list to countries into the program?

Here is what I have so far

main function: first function called by the operating system
inputs: five different currenices
and their equivalent to the US dollar
output: return "list the equivalents"

#include <stdio.h>
int main(void)
{
printf("\nCurrency Conversion n\n");

return 0;

}
 
Last edited:
Carrie, I'd be glad to help you, but I'm afraid you haven't given us enough information to understand your problem.

Can you please provide the "problem statement" that defines exactly what you program is supposed to do?

- Warren
 
Write a C program that displays a title, "Currency Conversion," and then write the names of five currencies and their equivalents to the US dollar. The conversions are hard coded equations. Insert comments in the program to document the program internally. Attach a design flow chart and a version control sheet to a hard copy of the source code of the program.
 
Okay, so the meat of the program is something like the following:

printf("1 US dollar = 1.3 Euro\n");
printf("1 US dollar = <blah> Canadian dollar\n");

and so on -- with the right numbers, of course.

- Warren
 
#include <stdio.h>

int main(int argc, char *argv[])
{
float inputamount, result;

printf("\n\n Enter amount to be converted");
printf("\n in the form 2.22 AU, where the");
printf("\n where the two letters represent");
printf("\n a country.");
printf("\n\n Enter amount to convert: ");

switch(inputamount)
{
case "au":
result = inputamount * 1.5;
break;
case "ro":
result = input amount * 0.3;
break;
/* Add in as many possibilities as you like */
default:
result = inputamount * 1.0;
break;
}
printf("\n\n In our currency that is: %f", result);
getch();
return 0;
}

You may wish to add a "tolower" function above the switch and make all the country codes lower case, or upper, whichever you prefer. Oh, also, I forgot to assign the variables to each input.

Just thought I'd space it out a bit.
 
Last edited:
Adam,

The spec she provided for her program made no mention of any input or calculations to be performed. It said it should "write the names of five currencies and their equivalents to the US dollar."

A program with five printfs meets this specification.

Perhaps the spec is not well-formed -- and that would fully be the teacher's fault -- but it doesn't say anything about choosing countries, getting input, and calculating anything.

- Warren
 
/* Write a beginning of a C program that displays "Currency Conversion" and then write
the names of five currencies and their equivalents to US dollars. We need to be prepared
to demonstrate our program.
*/

#include

int main (int argr, char *argv[])
{
float inputamount;

printf("n\n Enter amount to be converted ");
printf("n\n the form 2.22 AU, where the ");
printf("\nwhere the two letters represent");
printf("n\nEnter amount to convert: ");
}
(inputamount);
{
case "au":
(result = input amount*1.5);
break;
case "ro":
(result = input amount*0.3);
break;
case "cad":
(result = input amount*.062818);
break;
case "eur":
(result = input amount*0.8713);
break;
/* Add in as many possibilities as you like*/
default:
result = inputamount * 1.0;
break;

printf("\n\nIn our currency that is: %f",result);
return 0;
}

This is what I typed in but I get an error line 14: function not declared '("n\nEnter amount to converted")

Please let me know what I did wrong.
 
switch(inputamount) instead of (inputamount)

although i don't have c compiler here to check it.
 
I wrote mine in a hurry on here, no compiler. I'm doing it properly now...
 
a scanf somewhere in the code would also help, or catching the arguments from the argv for input.
 
Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	float inputamount, result;
	int countrycode;

	inputamount = atoi(argv[1]);
	countrycode = atoi(argv[2]);

	switch(countrycode)
	{
	case 1:
		result = inputamount * 1.5;
		break;
	case 2:
		result = inputamount * 0.3;
		break;
	case 3:
		result = inputamount * 2.3;
		break;
	case 4:
		result = inputamount * 3.3;
		break;
	case 5:
		result = inputamount * 0.4;
		break;
	default:
		result = inputamount * 1.0;
		break;
	}
	return result;
}

Of course this is using it from the command line, no help or instructions. Like you start a little DOS window and type "programme-name amount-of-money number-representing-a-country", separated by spaces. For example you might type "cc 250.50 1" where the last argument means Ausrtalia.
 
/*Cathy Robinson Currency Conversion Program*/

/* Write a beginning of a C program that displays "Currency Conversion" and then write
the names of five currencies and their equivalents to US dollars. We need to be prepared
to demonstrate our program.
*/

#include


int main(int argc, char *argv[])
{
float inputamount, result;
int countrycode;

inputamount = atoi(argv[1]);
countrycode = atoi(argv[2]);

switch(countrycode)
{
case 1:
result = inputamout * 1.5;
break;
case 2:
result = inputamount * 0.3;
break;
case 3:
result = inputamount * 2.3;
break;
case 4:
result = inputamount * 0.4;
break;
case 5:
result = inputamount * 1.0;
}
return result;
when I compile the program, this is what error message is returnedMiracle C Compiler (r3.2), written by bts.
line 8: #include ignored--bad delimiter.
Compiling c:\program files\miracle c\cathy robinson currency program(1).c
main

c:\program files\miracle c\cathy robinson currency program(1).c: line 16: function not declared
'= atoi(argv[1])'
aborting compile

Please let me know what I have done wrong?
 
Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
float inputamount, result;
int countrycode;

inputamount = atoi(argv[1]);
countrycode = atoi(argv[2]);

switch(countrycode)
{
case 1:
result = inputamount * 1.5;
break;
case 2:
result = inputamount * 0.3;
break;
case 3:
result = inputamount * 2.3;
break;
case 4:
result = inputamount * 0.4;
break;
case 5:
result = inputamount * 1.0;
}
return result;
}
You missed an "n" in the "inputamount" in the first case of the switch.

Also, make sure you include both stdio.h and stdlib.h.
 
/*Cathy Robinson Currency Conversion Program*/

/* Write a beginning of a C program that displays "Currency Conversion" and then write
the names of five currencies and their equivalents to US dollars. We need to be prepared
to demonstrate our program.
*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
float inputamount, result;
int countrycode;

printf(input amount = atoi [argv[1]);
printf(countrycode = atoi(argv[2]);

switch(countrycode)
{
case 1:
result = inputamount * 1.5;
break;
case 2:
result = inputamount * 0.3;
break;
case 3:
result = inputamount * 2.3;
break;
case 4:
result = inputamount * 0.4;
break;
case 5:
result = inputamount * 1.0;
}
return result;
}





I made the changes to the program and I still get an error message
C:\Program Files\Miracle C\cathy robinson currency program(1).c: line 16: variable 'amount' not found
'(input amount = atoi [argv[1])'
aborting compile
 
"variable 'amount' not found"

This means you have typed "input amount" instead of "inputamount" on line 16.
 
How would I design a flow chart for this program

How would I design a flow chart for this program?


/*Cathy Robinson Currency Conversion Program*/

/* Write a beginning of a C program that displays "Currency Conversion" and then write
the names of five currencies and their equivalents to US dollars. We need to be prepared
to demonstrate our program.
*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[1])
{
float inputamount, result;
int countrycode;

printf(inputamount = atoi(argv[1]);
printf(countrycode = atoi(argv[2]);

switch(countrycode)
{
case 1:
result = inputamount * 1.5;
break;
case 2:
result = inputamount * 0.3;
break;
case 3:
result = inputamount * 2.3;
break;
case 4:
result = inputamount * 0.4;
break;
case 5:
result = inputamount * 1.0;
}
return result;
}

I also get this error when I try to compile the program:
c: line 16: function not declared
'(inputamount = atoi(argv[1])'
aborting compile
 
Last edited:
Yuck, flow charts. Flow charts really aren't very good. They're too simplistic for decent programmes.

Start. (hitting enter when you type the commands in the command line)

Input amount of money. (the floating point number your typed is inserted into the programme)

Decision point based on country code. (based on the country code you selected, it decides which branch/calculation to use)

Branch from that decision point to each of the possible calculations you can make, plus maybe a branch for Default or Error possibilities. (going off to the required calculation)

A Process Point on each path for calculating the conversions. (performing the required calculation)

Ouput result. (output a floating point number)

End. (end programme)

Hard to do it just in text, and I don't remember which shape is used for each type of thing in the chart. Have some of your friends check it over before you hand it in. Hopefully someone here at sciforums will be able to draw one up for you, as an example.


PS: Have you got the programme working, and can you use it correctly from the command line?
 
Status
Not open for further replies.
Back
Top