Exforsys
+ Reply to Thread
Results 1 to 2 of 2

about this C programming thing

This is a discussion on about this C programming thing within the C and C++ forums, part of the Programming Talk category; this is a program that takes a measurement in one unit (e.g., 4.5 miles, etc) and converts it to another ...

  1. #1
    MyRedz is offline Junior Member Array
    Join Date
    Oct 2008
    Answers
    2

    about this C programming thing

    this is a program that takes a measurement in one unit (e.g., 4.5 miles, etc) and converts it to another unit (i.e. kilometers).
    the algorithm is
    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..


    and then the unit.dat
    is
    Code:
    kilometers km distance 1000
    yards yd distance 0.9144
    meters m distance 1
    quarts qt volume 0.94635
    liters l volume 1
    gallons gal volume 3.7854
    milliliters ml volume 0.001
    kilograms kg mass 1
    grams g mass 0.001
    slugs slugs mass 0.14594
     my code is this
    i
    well how to search and display the error when have found the target??any ideas??


    Code:
    #include <stdio.h>
    void calculate conversion(double input)
    char input_oldunit;
    double input_value;
    char input_newunit;
    struct unit
    4{
    char oldunit[12];
    char SI	[5];
    char newunit[10];
    double value;
    };
    
    int main()
    {
    	struct unit h[20];
            int k = 0.0;
            char choice
    
    
    FILE *measurement;
    	
    	printf("\THIS IS A UNIVERSAL MEASUREMENT PROGRAMn");
    	
    measurement = fopen("unit.dat","r");
    	do{
    printf("\nPlease select q to quit or other to enter conversion program:");
    	scanf("%s",&choice);
    	printf("You have selected option %d\n\n",choice);
    	switch(choice)
    	{
    
    
    case q:
    
    printf("thank s and have a nice day\n);
    
    return 0;
    break;
    }
    default:{
    	
    
    	if (measurement == NULL)
    		printf("Error opening data file.\n");
    	else
    	{
    		while(fscanf(measurement,"%s %s %s %lf ",&h1[k].oldunit,&h1[k].SI,&h1[k].newunit,&h1[k].value)==4)
    		{
                           printf(" input your values followed with it's measurement name or SI unit and the target measurement's name or SI unit.\n");
                           scanf(" %lf %s %s",&input_value,&input_oldunit,&input_newunit);
    			calculate conversion(input);
    }
    
    
    		}
    
    		
    		
    	
    	}
    
    fclose(measurement);
    }
    	printf("If you want to quit, press q or else press any key to continue\n");
    	scanf("%s",&choice);
    	}while(optn!=q);
    	printf("To the infinite..and BEYOND....!!!!!\n\n");
    break;
    return;
    }
    i am stuck at the point 3 or 4...

    any ideas on the repeat data format until error found???
    i have no idea...so and search for old and new units..


  2. #2
    priyaraji is offline Member Array
    Join Date
    Apr 2006
    Answers
    52
    For implementing point 3 that is for Repeating until data format error encountered loop structures in C like do...while or while must be used.

    For implementing point 4 that is Search for old unit in database you have to make the program read the whole input data file. For this using array is the best choice. You can make the program read the whole input data file in a array structure and then whenever user enters data this could be compared with the whole array data.

    Another way to implement point 4 is to use the fread function in C and read each data in input data file line by line.


Latest Article

Network Security Risk Assessment and Measurement

Read More...