By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,221 Members | 1,234 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,221 IT Pros & Developers. It's quick & easy.

problem with Converter in C..

P: 17
can anyone here show me some examples of converter like Currency converter or
universal measurement converter...i tried to search but no working example..
Oct 14 '08 #1
Share this Question
Share on Google+
15 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Well, there are rules against doing things like posting full code, and this sounds like a pretty common homework problem.

However, we can help you with your program, have you started this? I'd start this by looking at how this is done by hand - gathering the conversion ratios. Then you can write an algorithm for an "any case" conversion, and from there create your code.

I'd recommend Google for the current currency conversion rates, and probably Google or Wikipedia for measurement conversion ratios.
Oct 14 '08 #2

P: 17
the problem is i don't understand my question ..
well my question is like this
PROBLEM STATEMENT
Write a program that takes a measurement in one unit (e.g., 4.5 miles, etc) and converts it to another unit (i.e. kilometers). For example this conversion request
450 km miles
Would result in this program output
Attempting conversion of 450.000 km to miles
450.000 km = 279.6247 miles
The program should produce an error message if a conversion between two unit of different classes (e.g., distance to volume) is requested. The program should take a data base of conversion information from an input file before accepting conversion programs entered interactively by the user. The user should be able to specify units either by name (e.g. kilograms) or by abbreviation (e.g. kg).
the the ALGORITHM)
The algorithm for the program is fairly straightforward.
1
. Load units of measurement database.
2.
Get value to convert and old and new unit names.
3.
Repeat until data format error encountered.
4.
Search for old unit in database.
5.
Search for new units in database.
6.
If conversion is impossible, issue appropriate message,
7.
Else, compute and display conversion
8.
Get value to convert an old and new unit names.
about loading units of measurement database do we need to fscan database from a unit.dat or just do
define a structure type that groups all relevant attributes about one unit..

examples of unit is
miles mi distance 1609.3
kilometers km distance 1000
yards yd distance 0.9144
Oct 15 '08 #3

Expert 100+
P: 671
Ok, so, it seems pretty clear to me. Let's go over the information we have:

The program should take a data base of conversion information from an input file
So you are reading a file here. Now, clearly, you want to store this information somehow in your program. Some data structure needs to hold the information after you read the file, right?

If you want to define a structure type, that's up to you.
Oct 15 '08 #4

sicarie
Expert Mod 2.5K+
P: 4,677
Ok, so, it seems pretty clear to me. Let's go over the information we have:

So you are reading a file here. Now, clearly, you want to store this information somehow in your program. Some data structure needs to hold the information after you read the file, right?

If you want to define a structure type, that's up to you.
Agreed, from this file you can determine what specific conversions you are going to need to make - distance, volume, time, etc..., and you can look up those conversion formulas. From there, you can store those in a "database" or data structure, and once you read the amount and type from the file, based on the type determine which formula to use.

Please have a look at our Posting and Homework Guidelines, take a stab at it, and come back with your questions.
Oct 15 '08 #5

P: 17
miles mi distance "value"


may i know what use is miles and distance?
both of them have the same value...
i don't get the idea..is it for one way conversion??
Oct 17 '08 #6

Banfa
Expert Mod 5K+
P: 8,950
distance (or length) is what is being measured, there are many units you could measure distance (or length) in, miles is one of them, kilometres is another.
Oct 17 '08 #7

Expert 100+
P: 2,418
miles mi distance "value"

may i know what use is miles and distance?
both of them have the same value...
i don't get the idea..is it for one way conversion??
Are you quoting one line from the database file? If so, then let's try to parse the line:
... miles ......... This is the name of the unit.
... mi ............. This is the abbreviation of the unit.
... distance .... This is the class the unit belongs to.
... "value" ....... Is this perhaps a numeric value in the file? If so, it is the scale factor used to convert a distance to or from miles. To understand how to use this scale factor you need to compare two lines from your database with identical class.
Oct 17 '08 #8

P: 17
can i know the ways of acessing the arrays in my structure??
i want to use it in my calculations..........
like
struct unit
{

float value[10];
char unit[10];
char measurement[10];
}

how to access it from the main??
is it using pointers but how to call individually?
Oct 18 '08 #9

P: 17
well here is my incomplete coding..i dunno how to get the file until error is found...in my first post..
stuck hard at point 3 and 4
where we have to encounter error..



the unit.dat
is
Expand|Select|Wrap|Line Numbers
  1. kilometers km distance 1000
  2. yards yd distance 0.9144
  3. meters m distance 1
  4. quarts qt volume 0.94635
  5. liters l volume 1
  6. gallons gal volume 3.7854
  7. milliliters ml volume 0.001
  8. kilograms kg mass 1
  9. grams g mass 0.001
  10. slugs slugs mass 0.14594
  11.  my code is this
  12. i


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void calculate conversion(double input)
  3. char input_oldunit;
  4. double input_value;
  5. char input_newunit;
  6. struct unit
  7. 4{
  8. char oldunit[12];
  9. char SI    [5];
  10. char newunit[10];
  11. double value;
  12. };
  13.  
  14. int main()
  15. {
  16.     struct unit h[20];
  17.         int k = 0.0;
  18.         char choice
  19.  
  20.  
  21. FILE *measurement;
  22.  
  23.     printf("\THIS IS A UNIVERSAL MEASUREMENT PROGRAMn");
  24.  
  25. measurement = fopen("unit.dat","r");
  26.     do{
  27. printf("\nPlease select q to quit or other to enter conversion program:");
  28.     scanf("%s",&choice);
  29.     printf("You have selected option %d\n\n",choice);
  30.     switch(choice)
  31.     {
  32.  
  33.  
  34. case q:
  35.  
  36. printf("thank s and have a nice day\n);
  37.  
  38. return 0;
  39. break;
  40. }
  41. default:{
  42.  
  43.  
  44.     if (measurement == NULL)
  45.         printf("Error opening data file.\n");
  46.     else
  47.     {
  48.         while(fscanf(measurement,"%s %s %s %lf ",&h1[k].oldunit,&h1[k].SI,&h1[k].newunit,&h1[k].value)==4)
  49.         {
  50.                        printf(" input your values followed with it's measurement name or SI unit and the target measurement's name or SI unit.\n");
  51.                        scanf(" %lf %s %s",&input_value,&input_oldunit,&input_newunit);
  52.             calculate conversion(input);
  53. }
  54.  
  55.  
  56.         }
  57.  
  58.  
  59.  
  60.  
  61.     }
  62.  
  63. fclose(measurement);
  64. }
  65.     printf("If you want to quit, press q or else press any key to continue\n");
  66.     scanf("%s",&choice);
  67.     }while(optn!=q);
  68.     printf("To the infinite..and BEYOND....!!!!!\n\n");
  69. break;
  70. return;
  71. }
  72.  
  73.  
  74.  
Oct 19 '08 #10

Expert 100+
P: 2,418
well here is my incomplete coding..i dunno how to get the file until error is found...in my first post..
stuck hard at point 3 and 4 where we have to encounter error..
What do you mean by "dunno how to get the file until error is found ... in my first post"? The first post for this thread doesn't describe a specific error. Can you describe the problem you're referring to?

When you say "stuck hard at point 3 and 4", do you mean steps 3 and 4 of the algorithm as outlined in message #3:
3. Repeat until data format error encountered.
4. Search for old unit in database.
It would be easier to analyze your code if I knew all the ways in which you believe it to be incomplete.
Oct 19 '08 #11

P: 17
well now the search file until not found or error..
is a if statement right?
so do i have to use a binary search like this??
is it possible??
can i use it??/

Expand|Select|Wrap|Line Numbers
  1. int binary search(int arr[],int size,int target)
  2.  
  3. {
  4.  
  5. int middleposition;
  6. int middlevalue;
  7. int result = -1;
  8. int low = 0;
  9. int high = SIZE - 1;
  10. while(result == -1&& low <= high)
  11. {
  12.  
  13. middleposition = (low -high)/2;
  14. middlevalue = arr[[middleposition];
  15. if(target ==middlevalue)
  16. {
  17. result = middleposition;
  18. }
  19. else if (target<middlevalue)
  20. high = middleposition -1;
  21. else 
  22.  low = middleposition +1;
  23. }
  24. return result;
  25. }
  26.  
Oct 20 '08 #12

Expert 100+
P: 2,418
The binary search algorithm only works if the input list is sorted.
Can you count on the database file to be in strictly sorted order?
Is it worthwhile for you to add a step to create a sorted database file?
An easy alternative to a binary search is a linear search (start at the beginning and look at each entry til you find the one you want). Does the database file have so many entries that the speed difference between linear search and binary search is significant?
Oct 20 '08 #13

P: 17
well here goes my coding..any way to improve it?
i done with the unit.dat file which contains
measurement ,abbreviation, classes and it's value.
i use linear search...
any methods to make it more neat,tidy..?
anyway there are some errors that i tried but couldn't correct it..
is it possible to make the fscanf simple without the function prototype???
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3.  #define  NOT_FOUND  -1
  4. typedef struct 
  5. {
  6.     char name[30];
  7.     char abbrev[15];
  8.     char class[20];
  9.     double standard;
  10. } unit_t
  11.  
  12. int fscan_unit(FILE *filep,unit_t *unitp){
  13.     int status;
  14.     status = fscanf(filep, "%s%s%s%lf",(*unitp).name,(*unitp).abbrev,(*unitp).class,&(*unitp).standard);
  15.     if(status == 4)
  16.         status = 1;
  17.     else if (status != EOF)
  18.         status = 0;
  19.  
  20.     return (status);
  21. }
  22.  
  23. void
  24. load_units(int        unit_max,
  25.            unit_t    units[],
  26.            int       *unit_sizep)
  27.  
  28. {
  29.     FILE  *inp;
  30.     unit_t data;
  31.     int       i, status;
  32.  
  33.     inp = fopen("units.dat","r");
  34.     i = 0;
  35.  
  36.     for (status = fscan_unit(inp, &data);
  37.          status == 1  &&  i < unit_max;
  38.          status = fscan_unit(inp, &data)) {
  39.        units[i++] = data;
  40.     }
  41.     fclose(inp);
  42.     if (status == 0) {
  43.           printf("\n*** Error in data format ***\n");
  44.           printf("*** Using first &d data format ***\n");
  45.     } else if (status != EOF) {
  46.           printf("\n*** Error: too much data in file ***\n");
  47.           printf("*** Using first %d data values ***\n", i);
  48.     }
  49.  
  50.     *unit_sizep = i;
  51.  }
  52.  
  53.  
  54.  
  55.  int
  56.  search(const unit_t units[],
  57.         const char  *target,
  58.         int         n)
  59.  
  60. {
  61.         int i,
  62.             found = 0,
  63.             where;
  64.  
  65.         i = 0;
  66.         while (!found && i < n) {
  67.             if (strcmp(units[i].name,    target) == 0 ||
  68.                 strcmp(units[i].abbrev,    target) == 0)
  69.                    found = 1;
  70.             else
  71.             ++i;
  72.          }
  73. if(found)
  74. where = i;
  75. else 
  76.  where = NOT_FOUND;
  77. return(where);
  78.  
  79. }
  80. double convert(double quantity,double old_stand, double new_stand)
  81. {
  82.  
  83.   return(quantity*old_stand/new_stand);
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90. int main(void)
  91.  
  92. {
  93.  
  94.  
  95.  unit_t units[20];
  96.  
  97. int num_units;
  98. char old_units[30],new_units[30];
  99. int status;
  100. double quantity;
  101.  
  102. int old_index,new_index;
  103.  
  104. load_units(20,units,&num_units);
  105.  
  106. printf("Enter a conversion problem or q to quit.\n");
  107. printf("To convert 25 kilometers to miles,you would enter\n");
  108. printf(">25 kilometers miles\n");
  109. printf("       or,alternatively,\n");
  110. printf(">25 km mi\n>");
  111.  
  112. for(status = scanf("%lf %s%s",&quantity,&old_units,&new_units);
  113. status == 3;
  114. status = scanf("%lf %s%s",&quantity,&old_units,&new_units)){
  115. printf("attempting conversion of %.4e %s to %s ...\n",quantity,&old_units,&new_units);
  116. old_index = search(units,old_units,num_units);
  117. new_index = search(units,old_units,num_units);
  118. if (old_index == NOT_FOUND){
  119. printf("Unit %s not in database\n",old_units);
  120. }
  121. else if (new_index == NOT_FOUND){
  122. printf("UNit %s not in database\n",new_units);
  123. }
  124. else if (strcmp(units[old_index].class,units[new_index].class)!=0)
  125. else 
  126. printf(" Cannot convert %s(%s) to %s (%s)\n",old_units,units[old_index].class,new_units,units[new_index].class);
  127. else
  128. printf("%.4e %s = %.4e %s\n",quantity,old_units,convert(quantity,units[old_index].standard,units[new_index].standard),new_index);
  129.  
  130. printf("\nEnter a conversion problem or q to quit\n");
  131. }
  132. return(0);
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
Oct 21 '08 #14

P: 17
anyone here to help me errors??
Oct 21 '08 #15

Banfa
Expert Mod 5K+
P: 8,950
You should list what the errors are.
Oct 21 '08 #16

Post your reply

Sign in to post your reply or Sign up for a free account.