Thread: Help with my c-program

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

    Help with my c-program

    I am suppose to write a program that presents a main menu to a user. The main menu should present the user with two option. 1) convert a temperature or 2) quit the program. If the user selects the first option, she should be prompted to enter her name, after which the screen should clear, followed by a presentation of a conversion menu the conversion menu must provide six temperature conversions 1) Fahrenheit to celsius 2)celsius to fahrenheit 3) fahrenheit to kelvin 4) celsius to kelvin 5) kelvin to fahrenheit 6) kelvin to celsius as well as a seventh option to return to the main menu.

    the output should display the original input temp and the converted temperature plus the user name.

    ex:
    Alice conversions:
    99.9 degrees fahrenheit = 88.8 kelvins

    we also have to clear displays between users that is when the is user returns to the main menu but does not quit the program clear the screen for the next user.

    in addition to diplaying the conversions to the screen it must collect the following data written to a file tmpstat.txt
    total conversions by type for each user
    total conversion for each user
    total conversion by type for all users
    total conversion for all user.

    you must use functions and no passing by value.

    Here is my source code i wrote up i need help in it because i'm new to c programming and i have no clue as to how to collect the dat to write to the file? I also need help cleaning up my program.

    program:
    Code:
    /* Program name: project5.c
     * Author: William Woo
     * CS 223: Section 03
     * Date written: 12/25/2002
     * Description: This program uses menus to provide temperature conversions.
     */
    
    #include <stdio.h>
    #include <string.h>
    #define MAX 20
    
    void Greetings(void);
    void getchoice(void);
    void cmenu(void);
    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)
    {
       char ch;
       char *name[MAX];
       
       printf("          Main Menu           \n");
       printf("------------------------------\n");
       printf("a. Temperature Conversion\n");
       printf("q. Quit the program\n");
       printf("Enter the letter of your choice:\n");
       scanf("%c", &ch);
       
       if (ch == 'a') 
       {
    	   printf("Hi, please enter your first name.\n");
           scanf("%s", &name);
    	   printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    	   cmenu();
       }
       else (ch == 'q');
    	   printf("Goodbye!\n");
       
    }
    
    void cmenu(void)
    {
       int choice;
       double temp;
       char name;
       
           while (choice != 7)
    	   {
           printf("       Conversion Menu        \n");
           printf("------------------------------\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");
    	   printf("Enter the number of your choice:");
    	   scanf("%d", &choice);
    	   
       
    	   
    	   switch (choice)
    	   {
               case 1 : ftoc(temp, name);
    			        break;
    
               case 2 : ctof(temp, name);
    			        break;
    	
    	       case 3 : ftoK(temp, name);
    			        break;
    
    	       case 4 : ctoK(temp, name);
    			        break;
    
    	       case 5 : Ktof(temp, name);
    			        break;
    
    	       case 6 : Ktoc(temp, name);
    			        break;
    
    	       case 7 : 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 * 1.8) + 32;
    	displayresult(Ctemp, temp, &name);
    }
        
    void ctof(double temp, char name)
    {
    	double Ctemp;
    	getdata(&temp);
    	Ctemp = (temp - 32) / 1.8;
    	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);
    }
    
    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);
    }




    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Your formulas for Fahrenheit to Celsius and Celsius to Fahrenheit are backwards. I'll let you fix that.

    > char *name[MAX];
    You just need an array of characters to hold name. So:
    char name[MAX];

    > else (ch == 'q');
    You left out if here:
    else if (ch == 'q');

    >void cmenu(void)
    You need to pass the name:
    >void cmenu(char *name)
    >{
    > int choice;
    > double temp;
    > char name; //***** DELETE this line ******

    >void ftoc(double temp, char name)
    To pass a string (char array), use:
    void ftoc(double temp, char *name)
    You have several of these to change.

    > displayresult(Ctemp, temp, &name);
    An array is already passed as an address, so leave of &:
    displayresult(Ctemp, temp, name);
    You have several of these to change. Every time you call displayresult().

    >void displayresult(double Ctemp, double temp, char name)
    Again pass an array:
    void displayresult(double Ctemp, double temp, char *name)
    >{
    > printf("%s's Conversions:\n\n", &name);
    name already refers to an address:
    printf("%s's Conversions:\n\n", name);

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