Thread: Nee help for me project plez...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    Nee help for me project plez...

    welll sorry for it..
    i need to figure how it's done...
    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

    Help with Code Tags (Toggle Plain Text)
    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
    ikilometers 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??


    any ideas on the repeat data format until error found???
    i have no idea...so and search for old and new units..
    i m stuck...
    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;
    }
    
    #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;
    }
    well i

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm guessing that the error you're supposed to get is EOF. There are many posts/FAQ here about dealing with EOF.

    Why would you want to display an error when you find the target?

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Your program does not compile. I can tell you that by looking at your source-code. It is also badly formatted and, to be honest, barely looks like a serious attempt at the project.

    Format your code appropriately, then someone might feel like helping you.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    61
    well here is my revised coding..and it should be complete ..
    what ways to make it simplifier??
    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???
    Code:
    
    
    Code:
    #include<stdio.h>
    #include<string.h>
     #define  NOT_FOUND  -1
    typedef struct 
    {
    	char name[30];
    	char abbrev[15];
    	char class[20];
    	double standard;
    } unit_t
    
    int fscan_unit(FILE *filep,unit_t *unitp){
    	int status;
    	status = fscanf(filep, "&#37;s%s%s%lf",(*unitp).name,(*unitp).abbrev,(*unitp).class,&(*unitp).standard);
    	if(status == 4)
    		status = 1;
    	else if (status != EOF)
    		status = 0;
    
    	return (status);
    }
    
    void
    load_units(int		unit_max,
    		   unit_t	units[],
    		   int	   *unit_sizep)
    
    {
    	FILE  *inp;
    	unit_t data;
    	int	   i, status;
    
    	inp = fopen("units.dat","r");
    	i = 0;
    
    	for (status = fscan_unit(inp, &data);
    		 status == 1  &&  i < unit_max;
    		 status = fscan_unit(inp, &data)) {
    	   units[i++] = data;
    	}
    	fclose(inp);
    	if (status == 0) {
    		  printf("\n*** Error in data format ***\n");
    		  printf("*** Using first &d data format ***\n");
    	} else if (status != EOF) {
    		  printf("\n*** Error: too much data in file ***\n");
    		  printf("*** Using first %d data values ***\n", i);
    	}
    
        *unit_sizep = i;
     }
    
    
    
     int
     search(const unit_t units[],
            const char  *target,
    		int         n)
    
    {
    		int i,
    			found = 0,
    			where;
    
    		i = 0;
    		while (!found && i < n) {
    			if (strcmp(units[i].name,	target) == 0 ||
    			    strcmp(units[i].abbrev,	target) == 0)
    				   found = 1;
    			else
    			++i;
    		 }
    if(found)
    where = i;
    else 
     where = NOT_FOUND;
    return(where);
    
    }
    double convert(double quantity,double old_stand, double new_stand)
    {
    
      return(quantity*old_stand/new_stand);
    
    }
    
    
    
    
    int main(void)
    
    {
    
    
     unit_t units[20];
    
    int num_units;
    char old_units[30],new_units[30];
    int status;
    double quantity;
    
    int old_index,new_index;
    
    load_units(20,units,&num_units);
    
    printf("Enter a conversion problem or q to quit.\n");
    printf("To convert 25 kilometers to miles,you would enter\n");
    printf(">25 kilometers miles\n");
    printf("       or,alternatively,\n");
    printf(">25 km mi\n>");
    
    for(status = scanf("%lf %s%s",&quantity,&old_units,&new_units);
    status == 3;
    status = scanf("%lf %s%s",&quantity,&old_units,&new_units)){
    printf("attempting conversion of %.4e %s to %s ...\n",quantity,&old_units,&new_units);
    old_index = search(units,old_units,num_units);
    new_index = search(units,old_units,num_units);
    if (old_index == NOT_FOUND){
    printf("Unit %s not in database\n",old_units);
    }
    else if (new_index == NOT_FOUND){
    printf("UNit %s not in database\n",new_units);
    }
    else if (strcmp(units[old_index].class,units[new_index].class)!=0)
    else 
    printf(" Cannot convert %s(%s) to %s (%s)\n",old_units,units[old_index].class,new_units,units[new_index].class);
    else
    printf("%.4e %s = %.4e %s\n",quantity,old_units,convert(quantity,units[old_index].standard,units[new_index].standard),new_index);
    
    printf("\nEnter a conversion problem or q to quit\n");
    }
    return(0);
    }
    Last edited by MyRedz; 10-21-2008 at 12:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM