Thread: Help with my program

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Help with my program

    I'm trying to collect the following data to be written to a file:

    total conversions by type for each user
    total conversion for each user
    total conversion by type for all users
    total conversion for all user.

    I think theres something wrong with my code because when it writes to the file it gives a -895674. can anyone help me
    Heres my file:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 20
    
    void clearScreen();
    void greetings(void);
    void getchoice(void);
    void cmenu(char *name);
    void ftoc(double temp, char *name);
    void ctof(double temp, char *name);
    void ftoK(double temp, char *name);
    void ctoK(double temp, char *name);
    void Ktof(double temp, char *name);
    void Ktoc(double temp, char *name);
    int totalCount(int count1, int count2, int count3, int count4, int count5, int count6);
    void displayresult(double Ctemp, double temp, char *name);
    
    int main(void)
    {
    	int count1, count2, count3, count4, count5, count6, Tcount;
    	FILE *otpt;
    	otpt = fopen("tmpstats.txt", "w");
    	
    	greetings();
        getchoice();
        totalCount(count1, count2, count3, count4, count5, count6);
        fprintf(otpt, "%d", Tcount);
    	fclose(otpt);
    	return (0);
    }
    
    void greetings(void)
    {   
       printf("Hello, this program displays menu options for temperature conversions\n");
       printf("of Fahrenheit, Celsius, and Kelvin.\n\n\n");
    }
    
    void getchoice(void)
    {
       int num;
       char name[MAX];
       
       
       printf("          Main Menu           \n");
       printf("------------------------------\n\n");
       printf("Enter the number of your choice:\n\n");
       printf("1. Temperature Conversion\n");
       printf("2. Quit the program\n\n");
       printf("Choice: ");
       scanf("%d", &num);
       
       if (num == 1) 
       {
    	   printf("Hi, please enter your first name.\n");
           scanf("%s", name);
           clearScreen();
    	   cmenu(name);
       }
       else if(num == 2);
    	   clearScreen();
           printf("Goodbye!\n");
       
    }
    
    void clearScreen()
    {
    	system("cls");
    }
    
    void cmenu(char *name)
    {
       int Choice;
       int *count1, *count2, *count3, *count4, *count5, *count6;
       double temp;
       
       
           while (Choice != 7)
    	   {
           printf("       Conversion Menu        \n");
           printf("------------------------------\n");
           printf("Enter the number of your choice:\n\n");
    	   printf("1. Fahrenheit to Celsius\n");
           printf("2. Celsius to Fahrenheit\n");
           printf("3. Fahrenheit to Kelvin\n");
           printf("4. Celsius to Kelvin\n");
           printf("5. Kelvin to Fahrenheit\n");
           printf("6. Kelvin to Celsius\n");
           printf("7. Main Menu\n\n");
    	   printf("Choice: ");
    	   scanf("%d", &Choice);
    	   
       
    	   
    	   switch (Choice)
    	   {
               case 1 : count1 =0;
    			        ftoc(temp, name);
    			        count1++;
    			        break;
    
               case 2 : count2 =0;
    			        ctof(temp, name);
    			        count2++;
    			        break;
    	
    	       case 3 : count3 =0;
    			        ftoK(temp, name);
    			        count3++;
    			        break;
    
    	       case 4 : count4 =0;
    			        ctoK(temp, name);
    			        count4++;
    			        break;
    
    	       case 5 : count5 =0;
    			        Ktof(temp, name);
    			        count5++;
    			        break;
    
    	       case 6 : count6 =0;
    			        Ktoc(temp, name);
    			        count6++;
    			        break;
    
    	       case 7 : clearScreen();
    			        getchoice();
    			        break;
    			        
    	   }  
       }
    
    }
    
    void getdata(double *temp)
    {
    	printf("Please enter your temperature to be converted:\n");
    	scanf("%lf", temp);
    }
    
    void ftoc(double temp, char *name)
    {
         
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = (temp - 32) / 1.8;
    	
    	displayresult(Ctemp, temp, name);
    }
        
    void ctof(double temp, char *name)
    {
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = (temp * 1.8) + 32;
    	displayresult(Ctemp, temp, name);
    }
    
    void ftoK(double temp, char *name)
    {
        double Ctemp;
    	getdata(&temp);
    	Ctemp = ((temp * 1.8) + 32) +273.15;
        displayresult(Ctemp, temp, name);
    }
    void ctoK(double temp, char *name)
    {
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = temp + 273.15;
        displayresult(Ctemp, temp, name);
    }
    
    void Ktof(double temp, char *name)
    {
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = ((temp - 273.15) * 1.8) + 32;
        displayresult(Ctemp, temp, name);
    }
    
    void Ktoc(double temp, char *name)
    {
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = temp - 273.15;
        displayresult(Ctemp, temp, name);
    }
    
    int totalCount(int count1, int count2, int count3, int count4, int count5, int count6)
    {
        int Tcount;
    	Tcount = count1 + count2 + count3 + count4 + count5 + count6;
        return Tcount;
    }
    
    void displayresult(double Ctemp, double temp, char *name)
    {
    	printf("%s's Conversions:\n\n", name);
        printf("Original temperature was %.1lf and converted temperature is %.1lf.\n\n", temp, Ctemp);
    }

  2. #2
    Registered User The Junglist's Avatar
    Join Date
    Nov 2002
    Posts
    42
    >> fprintf(otpt, "%d", Tcount);

    You are printing Tcount to the file, yet it hasn't been initialized.

    I think what you meant to do was this :
    Code:
    Tcount = totalCount(count1, count2, count3, count4, count5, count6);
    [EDIT]
    I just noticed that none of the other variables have been initialized as well, so totalCount() will always return garbage.
    [/EDIT]
    Last edited by The Junglist; 11-24-2002 at 04:31 AM.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Need help

    I was able to figure out how to collect the data for Total conversions by type for each user and total conversions for each user. But i can't figure out how to get the total conversion by type for all users and total conversion for all users. can anyone help me.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 20
    
    void pause();
    void clearScreen();
    void greetings(void);
    void getchoice(void);
    void cmenu(char *name);
    void ftoc(double temp, char *name);
    void ctof(double temp, char *name);
    void ftoK(double temp, char *name);
    void ctoK(double temp, char *name);
    void Ktof(double temp, char *name);
    void Ktoc(double temp, char *name);
    void displayresult(double Ctemp, double temp, char *name);
    
    int main(void)
    {
    	
    	greetings();
        getchoice();
       	return (0);
    }
    
    void greetings(void)
    {   
       printf("Hello, this program displays menu options for temperature conversions\n");
       printf("of Fahrenheit, Celsius, and Kelvin.\n\n\n");
    }
    
    void getchoice(void)
    {
       int num;
       char name[MAX];
       
       
       printf("          Main Menu           \n");
       printf("------------------------------\n\n");
       printf("Enter the number of your choice:\n\n");
       printf("1. Temperature Conversion\n");
       printf("2. Quit the program\n\n");
       printf("Choice: ");
       scanf("%d", &num);
       
       if (num == 1) 
       {
    	   printf("Hi, please enter your first name.\n");
           scanf("%s", name);
           clearScreen();
    	   cmenu(name);
       }
       else if(num == 2);
    	   clearScreen();
           printf("Goodbye!\n");
       
    }
    
    void pause()
    {
    	system("pause");
    }
    
    void clearScreen()
    {
    	system("cls");
    }
    void cmenu(char *name)
    {
       int Choice;
       int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0, Tcount;
       double temp;
       FILE *otpt;
       otpt = fopen("tmpstats.txt", "w");
       
       
           while (Choice != 7)
    	   {
           printf("       Conversion Menu        \n");
           printf("------------------------------\n");
           printf("Enter the number of your choice:\n\n");
    	   printf("1. Fahrenheit to Celsius\n");
           printf("2. Celsius to Fahrenheit\n");
           printf("3. Fahrenheit to Kelvin\n");
           printf("4. Celsius to Kelvin\n");
           printf("5. Kelvin to Fahrenheit\n");
           printf("6. Kelvin to Celsius\n");
           printf("7. Main Menu\n\n");
    	   printf("Choice: ");
    	   scanf("%d", &Choice);
    	   
       
    	   
    	   switch (Choice)
    	   {
               case 1 : ftoc(temp, name);
    			        count1++;
    			        break;
    
    			        
               case 2 : ctof(temp, name);
    			        count2++;
    			        break;
    			       
    	
    	       case 3 : ftoK(temp, name);
    			        count3++;
    			        break;
    			        
    
    	       case 4 : ctoK(temp, name);
    			        count4++;
    			        break;
    			        
    
    	       case 5 : Ktof(temp, name);
    			        count5++;
    			        break;
    			        
    
    	       case 6 : Ktoc(temp, name);
    			        count6++;
    			        break;
    			        
    
    	       case 7 : clearScreen();
    			        getchoice();
    			        break;
    			        
    	   }  
       
    	 
    	   }
       
    	   Tcount = count1 + count2 + count3 + count4 + count5 + count6;
    	   fprintf(otpt, "The total conversion for %s is %d.\n\n", name, Tcount);
    	   fprintf(otpt, "Total Conversion by type:\n");
    	   fprintf(otpt, "ftoc = %d\n", count1);
    	   fprintf(otpt, "ctof = %d\n", count2);
    	   fprintf(otpt, "ftoK = %d\n", count3);
    	   fprintf(otpt, "ctoK = %d\n", count4);
    	   fprintf(otpt, "Ktof = %d\n", count5);
    	   fprintf(otpt, "Ktoc = %d\n", count6);
    
    	   fclose(otpt);
    	   
    }
    
    void getdata(double *temp)
    {
    	printf("Please enter your temperature to be converted:\n");
    	scanf("%lf", temp);
    }
    
    void ftoc(double temp, char *name)
    {
         
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = (temp - 32) / 1.8;
    	clearScreen();
    	displayresult(Ctemp, temp, name);
    	pause();
    	clearScreen();
    }
        
    void ctof(double temp, char *name)
    {
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = (temp * 1.8) + 32;
    	pause();
    	clearScreen();
    	displayresult(Ctemp, temp, name);
    }
    
    void ftoK(double temp, char *name)
    {
        double Ctemp;
    	getdata(&temp);
    	Ctemp = ((temp * 1.8) + 32) +273.15;
        pause();
    	clearScreen();
    	displayresult(Ctemp, temp, name);
    }
    void ctoK(double temp, char *name)
    {
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = temp + 273.15;
        pause();
    	clearScreen();
    	displayresult(Ctemp, temp, name);
    }
    
    void Ktof(double temp, char *name)
    {
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = ((temp - 273.15) * 1.8) + 32;
        pause();
    	clearScreen();
    	displayresult(Ctemp, temp, name);
    }
    
    void Ktoc(double temp, char *name)
    {
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = temp - 273.15;
        pause();
    	clearScreen();
    	displayresult(Ctemp, temp, name);
    }
    
    void displayresult(double Ctemp, double temp, char *name)
    {
    	printf("%s's Conversions:\n\n", name);
        printf("Original temperature was %.1lf and converted temperature is %.1lf.\n\n", temp, Ctemp);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM