Thread: don't know where to start

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    9

    don't know where to start

    I am writing a unit conversion program for a final project in my C-programming class. One of the requirements I have is that I have to have it keep track of only the 20 most recent inputs. I am trying to have it display like this:

    Starting Unit Ending Unit Filename
    mm ft mm.txt


    I am not sure of how to get this started. I have some code written that converts the units, and writes the results to a file, but I don't know where to start for this part. Any help on this would be great.
    Here's what I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void menu(void);
    void length(void);
    void mm(void); //millimeter function
    
    
    typedef struct{
            return_end[5];
            return_file[20];
            }return_info;
    
    
    
    int main(int argc, char *argv[])
    {
    	char check[5];
    	printf("Welcome to Casey's Conversion Program\n");
    
    	/* loop to rerun program */
    	do{
    		menu();
    		printf("Do you want to continue (y for yes)? ");
    		scanf("%s", check);
    		
    	}while(strcmp("y", check) == 0);
                 
    
    	system("PAUSE");	
    	return 0;
    }
    
    /* Menu Function */
    void menu(){
    	int start;
    
    	printf("Enter:\n1 for Length Conversion\n");
    	printf("2 for Area Conversion\n");
    	printf("3 for Volume Conversion\n");
    	printf("4 for Time Conversion\n");
    	printf("5 for Angle Conversion\n");
    	printf("6 for Speed Conversion\n");
    	printf("7 for Acceleration Conversion\n");
    	printf("8 for Mass Conversion\n");
    	printf("9 for Force Conversion\n");
    	printf("10 for Pressure Conversion\n");
    	printf("11 for Energy Conversion\n");
    	printf("12 for Power Conversion\n");
    	scanf("%d", &start);
    
    	switch(start){
    		case 1:
    		  length();
              break;
    
    		default:
    			printf("Not valid entry\n");
    			break;
    	}
    }     
    
    /* Length Function */
    char length(){
    	int start;
    	char return_length[5];
    
    	printf("Enter starting unit: \n");
    	printf("1 for millimeters\n");
    	printf("2 for centimeters\n");
    	printf("3 for meters\n");
    	printf("4 for kilometers\n");
    	printf("5 for inches\n");
    	printf("6 for feet\n");
    	printf("7 for yards\n");
    	printf("8 for miles\n");
    	scanf("%d", &start);
    
    	switch(start){
         case 1:
                mm();
    	    return_length = mm;
    	    break;
         case 2:
                cm();
                return_length = cm;
                break;
         case 3:
                m();
                return_length = m;
                break;
         case 4:
                km();
                return_length = km;
                break;
         case 5:
                in();
                return_length = in;
                break;
         case 6:
                ft();
                return_length = ft;
                break;
         case 7:
                yd();
                return_length = yd;
                break;
         case 8:
                mi();
                return_length = mi;
                break;
    
    		default:
    			printf("Not valid entry\n");
    			return_length = n/a;
    			break;
    	}
    	return(return_length);
    }  
    
    /* mm Function */
    return_info mm(void){
    	int end;
    	double mm;
    	double ans;
    	FILE *mmptr;
    	return_info mm_info[30];
    	
    mmptr = fopen("mm.txt", "a");
    	
    	printf("Enter the number of millimeters: ");
    	scanf("%lf", &mm);
    	
    	printf("Enter ending unit: \n");
    	printf("1 for millimeters\n");
    	printf("2 for centimeters\n");
    	printf("3 for meters\n");
    	printf("4 for kilometers\n");
    	printf("5 for inches\n");
    	printf("6 for feet\n");
    	printf("7 for yards\n");
    	printf("8 for miles\n");
    	scanf("%d", &end);
        
    	switch(end){
    		case 1:
    			ans = mm;
    			printf("%lf millimeters equals %lf millimeters\n", mm, ans);
    			fprintf(mmptr,"%lf mm = %lf mm\n", mm, ans);
    			mm_info[] = {"mm","mm.txt"};
    			break;
    
    		case 2:
    			ans = mm/10;
    			printf("%lf millimeters equals %lf centimeters\n", mm, ans);
    			fprintf(mmptr,"%lf mm = %lf cm\n", mm, ans);
    			mm_info[] = {"cm","mm.txt"};
    			break;
    
    		case 3:
    			ans = mm/1000;
    			printf("%lf millimeters equals %lf meters\n", mm, ans);
    			fprintf(mmptr,"%lf mm = %lf m\n", mm, ans);
    			mm_info[] = {"m","mm.txt"};
    			break;
    
    		case 4:
    			ans = mm/1000000;
    			printf("%lf millimeters equals %lf kilometers\n", mm, ans);
    			fprintf(mmptr,"%lf mm = %lf km\n", mm, ans);
    			mm_info[] = {"km","mm.txt"};
    			break;
    
    		case 5:
    			ans = mm/25.4;
    			printf("%lf millimeters equals %lf inches\n", mm, ans);
    			fprintf(mmptr,"%lf mm = %lf in\n", mm, ans);
    			mm_info[] = {"in","mm.txt"};
    			break;
    
    		case 6:
    			ans = mm/304.8;
    			printf("%lf millimeters equals %lf feet\n", mm, ans);
    			fprintf(mmptr,"%lf mm = %lf ft\n", mm, ans);
    			mm_info[] = {"mm","ft.txt"};
    			break;
    			
    		case 7:
    			ans = mm/1914.4;
    			printf("%lf millimeters equals %lf yards\n", mm, ans);
    			fprintf(mmptr,"%lf mm = %lf yd\n", mm, ans);
    			mm_info[] = {"yd","mm.txt"};
    			break;
    
    		case 8:
    			ans = mm/1609000;
    			printf("%lf millimeters equals %lf miles\n", mm, ans);
    			fprintf(mmptr,"%lf mm = %lf mi\n", mm, ans);
    			mm_info[] = {"mi","mm.txt"};
    			break;
         
    		default:
    			printf("Not valid entry\n");
    	        mm_info[] = {"N/A","N/A"};
    			break;
    	}
    	fclose(mmptr);
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    	char return_length[5];
    
    	printf("Enter starting unit: \n");
    	printf("1 for millimeters\n");
    	printf("2 for centimeters\n");
    	printf("3 for meters\n");
    	printf("4 for kilometers\n");
    	printf("5 for inches\n");
    	printf("6 for feet\n");
    	printf("7 for yards\n");
    	printf("8 for miles\n");
    	scanf("%d", &start);
    
    	switch(start){
         case 1:
                mm();
    	    return_length = mm;
    That is not going to work for you. mm is defined as a function and return_length is a char array. If you want to keep track of the latest 20 inputs, a good way to do it might be to use a circular linked list of 20 nodes. That way, when the user enters the 21st input it just overwrites the 1st input and so on.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    9
    I think I almost figured it out. I am writing the information I need to a file in this format:

    i(counter) starting unit ending unit filename


    I then read it in main and only display the first 20.
    When I wrote the code, I got an infinite loop.

    Code:

    Code:
       for (status = fscanf(cntr, "%d %s %s %s",&i, start_unit,end_unit, filename); 
                status != EOF;
             status = fscanf(cntr, "%d %s %s %s",&i, start_unit,end_unit, filename))
             {
                    while(i<20){
                                printf("%d\t%s\t\t%s\t\t%s\n",i,start_unit,end_unit,filename);
                                }
                                }

  4. #4
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    maybe because ";"
    Code:
     for (status = fscanf(cntr, "%d %s %s %s",&i, start_unit,end_unit, filename);
                status != EOF;
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by xxxrugby
    maybe because ";"
    Code:
    for (status = fscanf(cntr, "%d %s %s %s",&i, start_unit,end_unit, filename);
                status != EOF;
    You may want to take a closer look at that.
    Quote Originally Posted by spikerotc04
    Code:
       for (status = fscanf(cntr, "%d %s %s %s",&i, start_unit,end_unit, filename); 
                status != EOF;
             status = fscanf(cntr, "%d %s %s %s",&i, start_unit,end_unit, filename))
             {
                    while(i<20){
                                printf("%d\t%s\t\t%s\t\t%s\n",i,start_unit,end_unit,filename);
                                }
                                }
    What is the purpose of the inner while loop? And where does this code fit into the rest?

    Would something like this be more suitable?
    Code:
    while ( fscanf(cntr, "%d %s %s %s",&i, start_unit,end_unit, filename) == 4 )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    9
    I am trying to get it to display only the 20 most recent inputs. It is for a unit conversion program I am writing for a final project. I want it to display like this:
    _________Starting Unit___________Ending Unit____________Filename

    1. _________mm___________________in_________________m m.txt

    the while loop was used to only display the top 20 lines in the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. C++ gui for windows where to start
    By prixone in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2006, 11:48 PM
  4. GNOME Desktop won't start (Mandriva)
    By psychopath in forum Tech Board
    Replies: 10
    Last Post: 07-19-2006, 01:21 PM
  5. Start bar color in WinXP
    By confuted in forum Tech Board
    Replies: 4
    Last Post: 05-03-2003, 06:18 AM