Thread: Simple Conversion tool

  1. #16
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Adak, I tried your bubble sort, and the problem persists.

    I even tried a separate file, without the first, last, median, and choice of descending or ascending

    here it is:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <limits.h>
    
    main(){
    int i;
    int count;
    int InputNumbers[100];
    int max;
    int Temp;
    char filename[256];
    
    FILE *in_fd;
    FILE *out_fd;
    char outputname[256];
    float next_number;
    
    printf("Enter input file name:");
    
      scanf("%s",filename);
    
    
    
    
    
      /* open file and print error message and exit on failure */
    
     
      if ((in_fd = fopen(filename, "r")) == 0) {
    
        fprintf(stderr, "File: %s Not Found!\n",filename);
    
        exit(0);
    
        }
    
        
       /*Asks user to name the ouput file, storing it in "outputname"*/
       
       printf("Enter the name of your output file:");
       scanf("%s", outputname);
       
       /*Creates output file, and opens it for writing*/
       
       out_fd=fopen(outputname, "w");
    
    
      /* this while loop reads numbers from the file until it is empty. */
      /* It stores these numbers in "InputNumbers" as "count" increments */
      /* through the array */
     
      
    
    
      while(fscanf(in_fd,"%f\n",&next_number)!=EOF) 
      {
    
        	count++;
        	InputNumbers[count]=next_number;
        	
        }
        
       max=count;
      
       
       /*This is a bubble sort. The program increments through the array, */
       /*comparing each set of adjacent numbers. It swaps the numbers in  */
       /*order to satisfy the inequality*/
       
       	for (count=0;count < max-1;count++)
    	{
       		for(i=count+1;i < max;i++)
       		{
       			if(InputNumbers[count] < InputNumbers[i])
     	 	 {
       			Temp=InputNumbers[count];
       			InputNumbers[count]=InputNumbers[i];
       			InputNumbers[i]=Temp;
       		}
       		}
       		
       		
    		
    	}
    return (0);
    	}
    I still get a segmentation fault on it. If i can get it working on its own, then i should be able to put it into the larger code.

    also, I got the converter working with this

    Code:
    #include <stdio.h>
    
    
    
    main() {
    
    
    
      /* Declare main program variables. Use of float allows for decimal points. */
    
    
    float number; 
    char unit;
    float m_km_ConvNum;
    float km_m_ConvNum;
    float M_ft_ConvNum;
    float f_M_ConvNum;
    
    /*Program asks the user to type the distance, including units, that they wish*/
    /*to be converted*/
    
    printf("Please enter the distance that you wish to convert (including units:(f)eet,(M)iles,(m)eters,(k)ilometers):\n");
    
    /*Program reads the input from the user, sotring them in number and unit*/
    
    scanf("%f %c\n",&number,&unit);
    
    /*The following if statements direct the program to the correct operation, depending*/
    /*on the value of unit*/
    
    if(unit=='m'){
    
    
    	m_km_ConvNum=number*.001;
    
    	printf("%.3f Meters is equivalent to: %.3f Kilometers\n",number,m_km_ConvNum);
    
    }
    
    
    if(unit=='K'){
    
    	km_m_ConvNum=number*1000;
    
    	printf("%.3f Kilometers is equivalent to: %.3f Meters\n",number,km_m_ConvNum);
    
    }
    
    if(unit=='M'){
    
    	M_ft_ConvNum=number*5280;
    
    	printf("%.3f Miles is equivalent to: %.3f Feet\n",number,M_ft_ConvNum);
    
    }
    
    if(unit=='f'){
    
    	f_M_ConvNum=number*.00018939;
    
    	printf("%.3f Feet is equivalent to: %.3f Miles\n",number,f_M_ConvNum);
    
    }
    
    	return (0);	
    
    }
    I had to add "&" to unit, is this wrong?

    I still, however, have to type end after putting in my distance. after that it works fine. any ideas?

  2. #17
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    got things working, if anyone needs to see any of the code just say so

    thanks for everyones help

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One problem with the posted code is that the variable count, was never initialized before being used.

    Then max is assigned the value, after count is incremented, and max is used in the bubblesort as the upper subscript value.

    You must have corrected this error, if it's working ok, now.

  4. #19
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    One suggestion you could use is, instead of having one variable for each type of conversion, have just two variables - your input, and your result. This way, if you want to expand on this to include more units for conversion, you only just need the conversion factor. Here's an example of such a case:

    Code:
    int main()
    {
    	float Input;
    	float Output;
    
    	...
    
    	// To convert mph into meters per second as the example
    	Output = Input*0.44704f; // f at the end means float (otherwise it's a double)
    }
    This is just something to consider for the future. If you were only showing the output value, you can do it with just one variable:

    Code:
    ...
    Input *= 0.44704f; // multiplies the original by the conversion factor
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple currency conversion program gone south!!
    By lildroopie in forum C Programming
    Replies: 1
    Last Post: 02-01-2009, 12:45 PM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  4. simple currency conversion problem
    By sweetgem in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 12:41 PM
  5. Currency Conversion Program
    By kgraw21 in forum C Programming
    Replies: 5
    Last Post: 04-19-2002, 08:39 AM