Assignment in C with functions and menus

Need help, can figure out what I am doing wrong?

Page 1 of 1

7 Replies - 547 Views - Last Post: 17 November 2010 - 01:34 PM Rate Topic: -----

#1 rl1961  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-October 10

Assignment in C with functions and menus

Posted 17 November 2010 - 11:54 AM

Can someone please help???
I am having a lot of difficulty in finishing my assignment.
Can someone look at my code and tell me what I am doing wrong?
Also, the last menu item, printfuelgraph, needs to call input from a file
c:\sample.txt

The errors I get are:

C:\Users\Local\Temp\ccEVcaaa.o(.text+0xe7) In function `printmenu':
[Linker error] undefined reference to `getitem'
[Linker error] undefined reference to `printlogo'
[Linker error] undefined reference to `multgame'
[Linker error] undefined reference to `printfuelgraph'
C:\Users\Local\Temp\ccEVcaaa.o(.text+0x6b1) In function `printfuelgraph.4':
[Linker error] undefined reference to `printChart'
C:\Users\Local\Temp\ccEVcaaa.o(.text+0x6b1) ld returned 1 exit status


/**************************
*  Program Assignment 5   *
*  carcomp1.c             * 
*                         *
*  November 17, 2010      *
*                         *
**************************/ 

/**************************
*   Include Standard C    *
*   Header files          *
**************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

/************************************************************
*                     Define Constant                       *
*                         Macros                            *
*  PI = 3.24159                                             *
*  INCHES_IN_FEET = 12 inches in a foot                     *
*  FEET_IN_MILE = 5280 feet in a mile                       *
*  MAX_MPG = 100 if car moved & gas is equal to 0           *
*  SECS_PER_MIN = 60 seconds per minute                     *
*  INTERVAL_MIN = 5 minute intervals                        *
*  MAX_WINDOW = 30 minute maximum for trip                  *
*  NUM_INTERVALS = 6 30 min max divided by 5 min intervals  *
************************************************************/
#define PI 3.14159
#define FEET_IN_MILE 5280
#define INCHES_IN_FEET 12
#define MAX_MPG 100
#define SECS_PER_MIN 60
#define INTERVAL_MIN 5
#define MAX_WINDOW 30
#define NUM_INTERVALS 6

/*****************************************
*   List function prototypes in header   *
*****************************************/
void printmenu(void);
void tripfueleff(int triplentgh , double radius);
void printrating(double mpg);
void work(int timetowork, int startdrive, int remeberitem);
void printlogo(int outerradius, int innerradius);
void printfuelgraph(FILE* ifp);
double findfueleff(double radius, double numrevs, double gas);
int multgame(int problemcount);

int main(int argc, char** argv)
{

double radius, mpg, numrevs, gas;
int triplentgh, timetowork, startdrive, rememberitem, outerradius, innerradius, problemcount;
// int multgame, problemcount;



	printmenu();
	return 0;
}

/***************************************************************************
*                                                                          *
*  Pre-condition:  None                                                    *
*                                                                          *
*  Post-condition: Prints out a menu of choices for the user.              *
*                                                                          *
***************************************************************************/
void printmenu(void){
     
	int choice, triplentgh, timetowork, startdrive, rememberitem;
    int outerradius, innerradius, problemcount;
	double radius, mpg, numrevs, gas;
    FILE* ifp;
    char fileName[50];

	do 
	{
		printf("Menu\n\n");
		printf("1. Calculate fuel efficiency for a trip\n");
		printf("2. Determine whether or not to go back home for an item\n");
		printf("3. Print the car logo to the display\n");
		printf("4. Play the multiplication game\n");
		printf("5. Print a visual display of the recent fuel efficiency\n");
		printf("6. Exit the program\n");
		scanf("%d",&choice);

		switch(choice)
		{
			case 1: tripfueleff (triplentgh, radius);
				break;
			case 2: getitem(timetowork, startdrive, rememberitem);
				break;
			case 3: printlogo(outerradius, innerradius);
				break;
			case 4: multgame(problemcount);
				break;
			case 5: printfuelgraph(ifp); //FILE* ifp);
				break;
			case 6: printf("Exiting program!\n");
				exit(0);
				break;
			default: printf("Invalid choice!\n");
				break;
		}

	} while (choice != 6);

}
/***************************************************************************
*                                                                          *
*  Pre-conditions: triplentgh: is a positive multiple of 5                 *
*                  radius    : represents the lentgh of the radius of      *
*                              the wheels of the car, in inches.           *
*                                                                          *
*  Post-condition: Asks the user about the number of revolutions and       *
*                  fuel used for each 5 minute interval of the trip and    *
*                  calculates and prints out the fuel efficiency for each  *
*                  of these intervals and the whole trip overall.          *
*                  Finally, the overall rating for the trip's              *
*                  fuel efficiency is printed.                             *
*                                                                          *
*  Notes:          This function corresponds to Program 3A                 *
*                  This program invokes  (uses/calls) the findfueleff      *
*                  and printrating functions.                              *
*                                                                          *
***************************************************************************/
void tripfueleff(int triplentgh, double radius)
{

    // Here we add local variables 
    double circumference, distance, totaldistance = 0;
    double gallons, mpg, finalmpg, totalgallons = 0;
    int numRotations, interval;
    
    // Get user's initial input for the whole trip.
    printf("How long is your trip, in minutes?\n");
    scanf("%d", &triplentgh);
    printf("What is the radius of your tires, in inches?\n");
    scanf("%lf", &radius);
    printf("\n");
    
    // Loop through each 5 minute time interval of the trip.
    for (interval=1; interval<=triplentgh/5; interval++) {
        
        /*******************************************************************
        * Note: All of the code in here is from program #1C with minor     *
        *        changes to print out information about the time interval. *
        *******************************************************************/
        
        /*************************************************
        *  Get the user input for this 5-minute period.  *
        *************************************************/
        printf("During time interval #%d, ", interval);
        printf("how many revolutions did your car's tires make?\n");
        scanf("%d", &numRotations);
        
        printf("During time interval #%d, ", interval);
        printf("how many gallons of gas did your car use?\n");
        scanf("%lf", &gallons);
        
        /***************************************************
        *  Calculates the tires' circumference, in miles.  *
        *  We calculate in two steps inches->feet->miles.  *
        ***************************************************/
        circumference = 2*PI*radius/INCHES_IN_FEET/FEET_IN_MILE;
    
        /**********************************************
        * Calculates the distance traveled in miles.  *
        **********************************************/
        distance = numRotations*circumference;
        
        /**********************************
        * Update whole trip totals here.  *
        **********************************/
        totaldistance += distance;
        totalgallons += gallons;
        
        /**************************************************
        * Fuel efficiency is just distance divided by the *
        * amount of gas used.                             *
        **************************************************/
        mpg = distance/gallons;
    
        /****************************************
        * Output the result for this interval.  *
        ****************************************/
        printf("Time %d-%d minutes: ", 5*(interval-1), 5*interval);
        printf("Your car averaged %.2lf miles per gallon.\n\n", mpg);
    }
    /***********************************************************
    * Calculate the final fuel efficiency for the whole trip.  *
    ***********************************************************/
    finalmpg = totaldistance/totalgallons;
    
    printf("For the whole trip, your car averaged %.2lf mpg.\n", finalmpg);

/*******************************************************************************
*                                                                              *
*  Pre-conditions: radius:  represents the length of the radius of a           *
*                           car wheel, in inches,                              *
*                  numrevs: represents the number of revolutions the           *
*                           wheels of the car have gone through in a           *
*                           period of time,                                    *
*                  gas:     represents the amount of gas, in gallons,          *
*                           the car used in that same period of time.          *
*                                                                              *
*  Post-conditions: Returns the fuel efficiency of the car during the          *
*                   interval specified by the input parameters,                *
*                   in miles per gallon.                                       *
*                                                                              *
*******************************************************************************/
double findfueleff(double radius, double numrevs, double gas) {
      
    double circumference, distance;
    
    /**************************************************   
    * Calculates the tires' circumference, in miles.  *
    * We calculate in two steps inches->feet->miles.  *
    **************************************************/
    circumference = 2*PI*radius/INCHES_IN_FEET/FEET_IN_MILE;
    
    /**********************************************
    * Calculates the distance traveled in miles.  *
    **********************************************/
    distance = numrevs*circumference;
    
    /**************************************
    *  Regular case where gas is used.    *
    **************************************/
    if (gas > 0)
        return distance/gas;
    
    /*******************************************************    
    * Moved but didn't use gas, return maximum efficiency. *
    *******************************************************/
    else if (distance > 0)
        return MAX_MPG;
    
    /*************************************************    
    *  Didn't move and didn't use gas, so return 0.  *
    *************************************************/
    else
        return 0;

	printf("\n\nPress ENTER to continue.");
	
	/**********************************************************************
    * system pause holds display of the program on screen for user to see *
    **********************************************************************/ 
    system("PAUSE");
    return ;
}		

/*******************************************************************************
*                                                                              *
*  Pre-conditions: timetowork:     represents the amount of time,              *
*                                  in minutes, it takes to get to work.        *
*                  startdrivetime: represents how many minutes before          *
*                                  work you start to drive there.              *
*                  rememberitem:   represents how many minutes into            *
*                                  the drive you remeber that you left         *
*                                  an item at home.                            *
*                                                                              *
*  Post-condition: Determines whether or not the user should go back home      *
*                  to pick up the item (i.e. if there is time to pick up       *
*                  the item), and prints the appropriate message.              *
*                  Note: This function corresponds to Program 2B.              *
*                                                                              *
*******************************************************************************/
void getitem(timetowork, startdrive, rememberitem)
// begin body of program
{
    int timeDrive, timeStart, timeRemember, timeSpare;
    
    /*********************************************
    * Get all of the information from the user.  *
    *********************************************/
    printf("How long does it take to drive to work (in minutes)?\n");
    scanf("%d", &timeDrive);
    printf("How many minutes before work did you start?\n");
    scanf("%d", &timeStart);
    printf("How many minutes did it take to realize you forgot an item?\n");
    scanf("%d", &timeRemember);
    
    /***************************************************************************
    * This is how much time you have left, if you pick up the item.            *
    * You spend time driving to work, plus time starting AND coming back home. *
    * This is why we multiply the time it took to remember by two.             *
    ***************************************************************************/
    timeSpare = timeStart - timeDrive - 2*timeRemember;
    
    /*******************************************************
    * A non-negative number here means we make it to work  *
    * on time AND get the item.                            *
    *******************************************************/
    if (timeSpare >= 0) {
       printf("Go back home and pick up the item.\n");
       printf("You will arrive at work with %d minutes to spare.\n", timeSpare);
    }
    
    /********************************************************
    *  Otherwise, we just go straight to work, and the time * 
    *  we get there is just based on when we started and    *
    *  how much time it takes to go there straight.         *
    ********************************************************/
    else {
       printf("Just go to work, you'll have to do without the item.\n");
       printf("You will arrive at work with %d minutes to spare.\n", timeStart-timeDrive);
    }
    
    system("PAUSE");
    return ;
}

/***************************************************************************
*                                                                          *
*  Pre-conditions: 2 < innerradius < outerradius < 30                      *
*                                                                          *
*  Post-condition: A wheel with an outer radius of outerradius             *
*                  number of characters and inner radius of                *
*                  innerradius number of characters  will be               *
*                  printed to the screen.                                  *
*                  Note: This corresponds to Program 3B.                   *
*                                                                          *
***************************************************************************/
void printlogo(outerradius, innerradius)

{
	
    int row, col, bigR, smallR;
    
    /*********************************************
    *  Get the size of both radii from the user. *
    *********************************************/
    printf("Enter outer radius.\n");
    scanf("%d", &bigR);
    printf("Enter inner radius.\n");
    scanf("%d", &smallR);
    
    /************************************************************
    *  Loop through each row of the grid, from -bigR to +bigR.  *
    ************************************************************/
    for (row=-bigR; row<=bigR; row++) {
        
        /*****************************************************************
        *  Loop through each column of the grid, with the same indexes.  *
        *****************************************************************/
        for (col=-bigR; col<=bigR; col++) {
            
            /********************************************
            *  Prints out a axle character, since it's  * 
            *  within a unit of the center.             *
            ********************************************/
            if (row*row+col*col <= 1)
               printf("+");
            
            /***********************************************
            *  Prints out the wheel character, since it's  *   
            *  within smallR units of the center.          *
            ***********************************************/
            else if (row*row+col*col < smallR*smallR)
               printf("$");
               
            /***********************************************   
            *  Prints out the tire character, since it's   *
            *  within bigR units of the center.            *
            ***********************************************/
            else if (row*row+col*col < bigR*bigR)
               printf("*");
            /*************************  
            *  This space is empty!  *
            *************************/
            else
               printf(" ");
        }
        printf("\n");
    }
    
    system("PAUSE");
    return ;
}

/***************************************************************************
*                                                                          *
*  Pre-conditions: problemcount > 0                                        *
*                                                                          *
*  Post-condition: Executes the multiplication game with problemcount      *
*                  problems and returns a number of seconds it took        *
*                  to correctly solve the problemcount problems.           *
*                  Note: This function corresponds to Program 3C.          *
*                                                                          *
***************************************************************************/
void multgame(problemcount)
// start body of program
{

    int numProblems, correct, answer;
    int start, end, opOne, opTwo, i;
    
    /*************************************
    * Seed the random number generator.  *
    *************************************/
    srand(time(0));
    
    /*********************************************
    * Get the number of problems the user wants. *
    *********************************************/
    printf("How many problems do you want?\n");
    scanf("%d", &numProblems);
    
    /***********************
    *  Get the start time. *
    ***********************/
    start = time(0);
    
    /*******************************
    *  Loop through each problem.  *
    *******************************/
    for (i=0; i<numProblems; i++) {
    
    /*********************************************************
    *  error in input - too many problems requested by user  *
    *********************************************************/
    if ( numProblems > 50 ){
      printf("\nYou requested more than 50 problems!\n");
        break;
        }
        
        /****************************************************
        * Generate the two operands for the multiplication. *
        * between 0 and 12 inclusive                        *
        ****************************************************/
        opOne = rand()%13;
        opTwo = rand()%13;
        
        /************************************************
        *  We will break out when we answer correctly.  *
        ************************************************/
        while (1) {
            
            /*********************************************************
            *  Print out the problem and read in the user's answer.  *
            *********************************************************/
            printf("Answer: %dx%d = ", opOne, opTwo);
            scanf("%d", &answer);
            
            /******************
            *  Wrong answer!  *
            ******************/
            if (answer != opOne*opTwo) 
                printf("Incorrect, try again.\n");
            
            /****************************************
            *  We got it, so get out of this loop!  *
            ****************************************/
            else
                break;      
        } // end-while
        
        printf("\n");
    } // end-for
    
    /**********************
    *  Get the end time.  *
    **********************/
    end = time(0);
    
    printf("You completed %d problems in %d seconds.\n", numProblems, end-start); 
    
    system("PAUSE");
    return ;
}

/***************************************************************************
*                                                                          *
*  Pre-conditions: ifp points to the begining of a valid                   *
*                  file containing data about this trip.                   *
*                  The format of this file specifies the                   *
*                  the format described in program 4.                      *
*                                                                          *
*  Post-condition: A graph will be printed out to the screen               *
*                  in the format specified by program 4b,                  *
*                  exactly how an 2007 Prius prints its fuel               *
*                  efficiency graph.                                       *
*                                                                          *
***************************************************************************/
void printfuelgraph(void)
// start body 
{
  
   double totalRevs = 0;
    double gallons, mpg, finalmpg, totalgallons = 0, numRotations;
    int triplength, interval, i, radius;
    char filename[100];
    double results[NUM_INTERVALS];
    
    /*****************************************************************************
    *  Initialize our array to store fuel efficiency for five-minute intervals.  *
    *****************************************************************************/
    for (i=0; i<NUM_INTERVALS;i++) results[i] = 0;
    FILE *ifp;

    /************************   
    *  Get the input file.  *
    ************************/
    printf("What file stores the car data?\n");
    scanf("%s", filename);
    ifp = fopen(filename, "r");
    
    /****************************************************************
    *  Read in the length of the trip and the radius of the wheel.  *
    ****************************************************************/
    fscanf(ifp,"%d", &triplength); 
    fscanf(ifp, "%d", &radius);
    
    /*************************************
    *  Loop through each time interval.  *
    *************************************/
    for (interval=0; interval<triplength/5; interval++) {
        
        /*******************************************
        *  These are the critical values to track  * 
        *  for each five-minute interval.          *
        *******************************************/
        totalRevs = 0;
        totalgallons = 0;
        
        /***************************************************
        *  Go through exactly five minutes worth of data.  *
        ***************************************************/
        for (i=0; i<INTERVAL_MIN*SECS_PER_MIN; i++) {
        
            /*********************************
            * Get the data for this second.  *
            *********************************/
            fscanf(ifp,"%lf", &numRotations);
            fscanf(ifp, "%lf", &gallons);
            
            /****************************************************
            *  Adjust our totals for the five minute interval.  *
            ****************************************************/
            totalRevs += numRotations;
            totalgallons += gallons;
            
        }
        
        /******************************************************
        *  Calculate and store the result for this interval.  *
        ******************************************************/
        results[interval] = findfueleff(radius, totalRevs, totalgallons);
    }
    
    /*************************
    *  Print out the chart.  *
    *************************/
    printChart(results);
    
    /**************************
    *  Close the input file!  *
    **************************/
    fclose(ifp);
    
    system("PAUSE");
    return ;
}

void printChart(double mpg[]) {
     
     int i, j;
     
    /******************************************
    * Loop through each row, starting at 19.  *
    ******************************************/
    for (i=19; i>=0; i--) {
        
        /*********************************
        *  Print the correct row label.  *
        *********************************/
        printf("%-4d", 5*i);
        
        /****************************
        *  Go through each column.  *
        ****************************/
        for (j=0; j<NUM_INTERVALS; j++) {
            
            /***************************************
            *  This bar is high enough, so print.  *
            ***************************************/
            if (mpg[j] > 5*i)
                printf("*****");
            
            /********************************************************    
            *  Otherwise, we must print spaces to fill the column.  *
            ********************************************************/
            else
                printf("     ");
            
            /*******************************    
            *  The space between columns.  *
            *******************************/
            printf(" ");
        }
        printf("\n");
        
    }
    
    /***************************************************************
    *  We can just hard-code this, since this is always the same.  *
    ***************************************************************/
    printf("    -----------------------------------\n");
    printf("    00-05 05-10 10-15 15-20 20-25 25-30\n");
}
/********************************************************************************
*                                                                               *
*  Pre-conditions: radius:  represents the length of the radius                 *
*                           of a car wheel, in inches,                          *
*                  numrevs: represents the number of revolutions                *
*                           the wheels of the car have gone through             *
*                           in a period of time, and                            *
*                  gas:     represents the amount of gas, in gallons,           *
*                           the car used in that same period of time.           *
*  Post-conditions: Returns the fuel efficiency of the car during the interval  *
*                   specified by the input parameters, in miles per gallon.     *
********************************************************************************
double findfueleff(double radius, double numrevs, double gas) {
      
    double circumference, distance;
    
    /***************************************************   
    *  Calculates the tires' circumference, in miles.  *
    *  We calculate in two steps inches->feet->miles.  *
    ***************************************************
    
    circumference = 2*PI*radius/INCHES_IN_FEET/FEET_IN_MILE;
    
    /***********************************************
    *  Calculates the distance traveled in miles.  *
    ***********************************************
    distance = numrevs*circumference;
    
    /************************************
    *  Regular case where gas is used.  *
    ************************************
    if (gas > 0)
        return distance/gas;
    
    /*********************************************************    
    *  Moved but didn't use gas, return maximum efficiency.  *
    *********************************************************
    else if (distance > 0)
        return MAX_MPG;
    
    /*************************************************    
    *  Didn't move and didn't use gas, so return 0.  *
    *************************************************
    else
        return 0;*/
 }




Is This A Good Question/Topic? 0
  • +

Replies To: Assignment in C with functions and menus

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,428
  • Joined: 23-August 08

Re: Assignment in C with functions and menus

Posted 17 November 2010 - 12:00 PM

First, you failed to prototype your functions before you tried to use them.

Second,
void getitem(timetowork, startdrive, rememberitem)

where are the datatypes for those arguments? You know like you have here:
double findfueleff(double radius, double numrevs, double gas)

?
Was This Post Helpful? 1
  • +
  • -

#3 rl1961  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-October 10

Re: Assignment in C with functions and menus

Posted 17 November 2010 - 12:07 PM

C:\Users\Local\Temp\ccEVcaaa.o(.text+0xe7) In function `printmenu':
[Linker error] undefined reference to `getitem'
[Linker error] undefined reference to `printlogo'
[Linker error] undefined reference to `multgame'
[Linker error] undefined reference to `printfuelgraph'
C:\Users\Local\Temp\ccEVcaaa.o(.text+0x6b1) In function `printfuelgraph.4':
[Linker error] undefined reference to `printChart'
C:\Users\Local\Temp\ccEVcaaa.o(.text+0x6b1) ld returned 1 exit status


/*****************************************
*   List function prototypes in header   *
*****************************************/
void printmenu(void);
void tripfueleff(int triplentgh , double radius);
void printrating(double mpg);
void work(int timetowork, int startdrive, int remeberitem);
void printlogo(int outerradius, int innerradius);
void printfuelgraph(FILE* ifp);
double findfueleff(double radius, double numrevs, double gas);
int multgame(int problemcount);


Doesn't this segment before the main prototype the function?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,315
  • Joined: 25-December 09

Re: Assignment in C with functions and menus

Posted 17 November 2010 - 12:19 PM

Quote

[Linker error] undefined reference to `getitem'
[Linker error] undefined reference to `printlogo'
[Linker error] undefined reference to `multgame'
[Linker error] undefined reference to `printfuelgraph'


These are linker errors: It means that the linker can not find the function definitions.

Check your braces {}, insure that you have a closing brace for every opening brace.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,428
  • Joined: 23-August 08

Re: Assignment in C with functions and menus

Posted 17 November 2010 - 12:24 PM

Well getitem and printChart are certainly missing from those prototypes.
Was This Post Helpful? 1
  • +
  • -

#6 rl1961  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-October 10

Re: Assignment in C with functions and menus

Posted 17 November 2010 - 01:07 PM

View PostJackOfAllTrades, on 17 November 2010 - 11:24 AM, said:

Well getitem and printChart are certainly missing from those prototypes.



I appreciate all the help. I have fixed the getitem and printChart errors but I am still getting errors in my code at line 590: 590 C:\Dev-Cpp\carcomp.c subscripted value is neither array nor pointer

/**************************
*  Program Assignment 5   *
*  carcomp1.c             * 
*                         *
*  November 17, 2010      *
*                         *
**************************/ 

/**************************
*   Include Standard C    *
*   Header files          *
**************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

/************************************************************
*                     Define Constant                       *
*                         Macros                            *
*  PI = 3.24159                                             *
*  INCHES_IN_FEET = 12 inches in a foot                     *
*  FEET_IN_MILE = 5280 feet in a mile                       *
*  MAX_MPG = 100 if car moved & gas is equal to 0           *
*  SECS_PER_MIN = 60 seconds per minute                     *
*  INTERVAL_MIN = 5 minute intervals                        *
*  MAX_WINDOW = 30 minute maximum for trip                  *
*  NUM_INTERVALS = 6 30 min max divided by 5 min intervals  *
************************************************************/
#define PI 3.14159
#define FEET_IN_MILE 5280
#define INCHES_IN_FEET 12
#define MAX_MPG 100
#define SECS_PER_MIN 60
#define INTERVAL_MIN 5
#define MAX_WINDOW 30
#define NUM_INTERVALS 6

/*****************************************
*   List function prototypes in header   *
*****************************************/
void printmenu(void);
void tripfueleff(int triplentgh , double radius);
void printrating(double mpg);
void getitem(int timetowork, int startdrive, int rememberitem);
void printlogo(int outerradius, int innerradius);
void printfuelgraph(FILE* ifp);
double findfueleff(double radius, double numrevs, double gas);
int multgame(int problemcount);

int main(int argc, char** argv)
{

double radius, mpg, numrevs, gas;
int triplentgh, timetowork, startdrive, rememberitem, outerradius, innerradius, problemcount;
// int multgame, problemcount;



	printmenu();
	return 0;
}

/***************************************************************************
*                                                                          *
*  Pre-condition:  None                                                    *
*                                                                          *
*  Post-condition: Prints out a menu of choices for the user.              *
*                                                                          *
***************************************************************************/
void printmenu(void){
     
	int choice, triplentgh, timetowork, startdrive, rememberitem;
    int outerradius, innerradius, problemcount;
	double radius, mpg, numrevs, gas;
    FILE* ifp;
    char fileName[50];

	do 
	{
		printf("Menu\n\n");
		printf("1. Calculate fuel efficiency for a trip\n");
		printf("2. Determine whether or not to go back home for an item\n");
		printf("3. Print the car logo to the display\n");
		printf("4. Play the multiplication game\n");
		printf("5. Print a visual display of the recent fuel efficiency\n");
		printf("6. Exit the program\n");
		scanf("%d",&choice);

		switch(choice)
		{
			case 1: tripfueleff (triplentgh, radius);
				break;
			case 2: getitem(timetowork, startdrive, rememberitem);
				break;
			case 3: printlogo(outerradius, innerradius);
				break;
			case 4: multgame(problemcount);
				break;
			case 5: printfuelgraph(ifp); //FILE* ifp);
				break;
			case 6: printf("Exiting program!\n");
				exit(0);
				break;
			default: printf("Invalid choice!\n");
				break;
		}

	} while (choice != 6);

}
/***************************************************************************
*                                                                          *
*  Pre-conditions: triplentgh: is a positive multiple of 5                 *
*                  radius    : represents the lentgh of the radius of      *
*                              the wheels of the car, in inches.           *
*                                                                          *
*  Post-condition: Asks the user about the number of revolutions and       *
*                  fuel used for each 5 minute interval of the trip and    *
*                  calculates and prints out the fuel efficiency for each  *
*                  of these intervals and the whole trip overall.          *
*                  Finally, the overall rating for the trip's              *
*                  fuel efficiency is printed.                             *
*                                                                          *
*  Notes:          This function corresponds to Program 3A                 *
*                  This program invokes  (uses/calls) the findfueleff      *
*                  and printrating functions.                              *
*                                                                          *
***************************************************************************/
void tripfueleff(int triplentgh, double radius)
{

    // Here we add local variables 
    double circumference, distance, totaldistance = 0;
    double gallons, mpg, finalmpg, totalgallons = 0;
    int numRotations, interval;
    
    // Get user's initial input for the whole trip.
    printf("How long is your trip, in minutes?\n");
    scanf("%d", &triplentgh);
    printf("What is the radius of your tires, in inches?\n");
    scanf("%lf", &radius);
    printf("\n");
    
    // Loop through each 5 minute time interval of the trip.
    for (interval=1; interval<=triplentgh/5; interval++) {
        
        /*******************************************************************
        * Note: All of the code in here is from program #1C with minor     *
        *        changes to print out information about the time interval. *
        *******************************************************************/
        
        /*************************************************
        *  Get the user input for this 5-minute period.  *
        *************************************************/
        printf("During time interval #%d, ", interval);
        printf("how many revolutions did your car's tires make?\n");
        scanf("%d", &numRotations);
        
        printf("During time interval #%d, ", interval);
        printf("how many gallons of gas did your car use?\n");
        scanf("%lf", &gallons);
        
        /***************************************************
        *  Calculates the tires' circumference, in miles.  *
        *  We calculate in two steps inches->feet->miles.  *
        ***************************************************/
        circumference = 2*PI*radius/INCHES_IN_FEET/FEET_IN_MILE;
    
        /**********************************************
        * Calculates the distance traveled in miles.  *
        **********************************************/
        distance = numRotations*circumference;
        
        /**********************************
        * Update whole trip totals here.  *
        **********************************/
        totaldistance += distance;
        totalgallons += gallons;
        
        /**************************************************
        * Fuel efficiency is just distance divided by the *
        * amount of gas used.                             *
        **************************************************/
        mpg = distance/gallons;
    
        /****************************************
        * Output the result for this interval.  *
        ****************************************/
        printf("Time %d-%d minutes: ", 5*(interval-1), 5*interval);
        printf("Your car averaged %.2lf miles per gallon.\n\n", mpg);
    }
    /***********************************************************
    * Calculate the final fuel efficiency for the whole trip.  *
    ***********************************************************/
    finalmpg = totaldistance/totalgallons;
    
    printf("For the whole trip, your car averaged %.2lf mpg.\n", finalmpg);

/*******************************************************************************
*                                                                              *
*  Pre-conditions: radius:  represents the length of the radius of a           *
*                           car wheel, in inches,                              *
*                  numrevs: represents the number of revolutions the           *
*                           wheels of the car have gone through in a           *
*                           period of time,                                    *
*                  gas:     represents the amount of gas, in gallons,          *
*                           the car used in that same period of time.          *
*                                                                              *
*  Post-conditions: Returns the fuel efficiency of the car during the          *
*                   interval specified by the input parameters,                *
*                   in miles per gallon.                                       *
*                                                                              *
*******************************************************************************/
double findfueleff(double radius, double numrevs, double gas) {
      
    double circumference, distance;
    
    /**************************************************   
    * Calculates the tires' circumference, in miles.  *
    * We calculate in two steps inches->feet->miles.  *
    **************************************************/
    circumference = 2*PI*radius/INCHES_IN_FEET/FEET_IN_MILE;
    
    /**********************************************
    * Calculates the distance traveled in miles.  *
    **********************************************/
    distance = numrevs*circumference;
    
    /**************************************
    *  Regular case where gas is used.    *
    **************************************/
    if (gas > 0)
        return distance/gas;
    
    /*******************************************************    
    * Moved but didn't use gas, return maximum efficiency. *
    *******************************************************/
    else if (distance > 0)
        return MAX_MPG;
    
    /*************************************************    
    *  Didn't move and didn't use gas, so return 0.  *
    *************************************************/
    else
        return 0;

	printf("\n\nPress ENTER to continue.");
	
	/**********************************************************************
    * system pause holds display of the program on screen for user to see *
    **********************************************************************/ 
    system("PAUSE");
    return ;
}		

/*******************************************************************************
*                                                                              *
*  Pre-conditions: timetowork:     represents the amount of time,              *
*                                  in minutes, it takes to get to work.        *
*                  startdrivetime: represents how many minutes before          *
*                                  work you start to drive there.              *
*                  rememberitem:   represents how many minutes into            *
*                                  the drive you remeber that you left         *
*                                  an item at home.                            *
*                                                                              *
*  Post-condition: Determines whether or not the user should go back home      *
*                  to pick up the item (i.e. if there is time to pick up       *
*                  the item), and prints the appropriate message.              *
*                  Note: This function corresponds to Program 2B.              *
*                                                                              *
*******************************************************************************/
void getitem(timetowork, startdrive, rememberitem)
// begin body of program
{
    int timeDrive, timeStart, timeRemember, timeSpare;
    
    /*********************************************
    * Get all of the information from the user.  *
    *********************************************/
    printf("How long does it take to drive to work (in minutes)?\n");
    scanf("%d", &timeDrive);
    printf("How many minutes before work did you start?\n");
    scanf("%d", &timeStart);
    printf("How many minutes did it take to realize you forgot an item?\n");
    scanf("%d", &timeRemember);
    
    /***************************************************************************
    * This is how much time you have left, if you pick up the item.            *
    * You spend time driving to work, plus time starting AND coming back home. *
    * This is why we multiply the time it took to remember by two.             *
    ***************************************************************************/
    timeSpare = timeStart - timeDrive - 2*timeRemember;
    
    /*******************************************************
    * A non-negative number here means we make it to work  *
    * on time AND get the item.                            *
    *******************************************************/
    if (timeSpare >= 0) {
       printf("Go back home and pick up the item.\n");
       printf("You will arrive at work with %d minutes to spare.\n", timeSpare);
    }
    
    /********************************************************
    *  Otherwise, we just go straight to work, and the time * 
    *  we get there is just based on when we started and    *
    *  how much time it takes to go there straight.         *
    ********************************************************/
    else {
       printf("Just go to work, you'll have to do without the item.\n");
       printf("You will arrive at work with %d minutes to spare.\n", timeStart-timeDrive);
    }
    
    system("PAUSE");
    return ;
}

/***************************************************************************
*                                                                          *
*  Pre-conditions: 2 < innerradius < outerradius < 30                      *
*                                                                          *
*  Post-condition: A wheel with an outer radius of outerradius             *
*                  number of characters and inner radius of                *
*                  innerradius number of characters  will be               *
*                  printed to the screen.                                  *
*                  Note: This corresponds to Program 3B.                   *
*                                                                          *
***************************************************************************/
void printlogo(outerradius, innerradius)

{
	
    int row, col, bigR, smallR;
    
    /*********************************************
    *  Get the size of both radii from the user. *
    *********************************************/
    printf("Enter outer radius.\n");
    scanf("%d", &bigR);
    printf("Enter inner radius.\n");
    scanf("%d", &smallR);
    
    /************************************************************
    *  Loop through each row of the grid, from -bigR to +bigR.  *
    ************************************************************/
    for (row=-bigR; row<=bigR; row++) {
        
        /*****************************************************************
        *  Loop through each column of the grid, with the same indexes.  *
        *****************************************************************/
        for (col=-bigR; col<=bigR; col++) {
            
            /********************************************
            *  Prints out a axle character, since it's  * 
            *  within a unit of the center.             *
            ********************************************/
            if (row*row+col*col <= 1)
               printf("+");
            
            /***********************************************
            *  Prints out the wheel character, since it's  *   
            *  within smallR units of the center.          *
            ***********************************************/
            else if (row*row+col*col < smallR*smallR)
               printf("$");
               
            /***********************************************   
            *  Prints out the tire character, since it's   *
            *  within bigR units of the center.            *
            ***********************************************/
            else if (row*row+col*col < bigR*bigR)
               printf("*");
            /*************************  
            *  This space is empty!  *
            *************************/
            else
               printf(" ");
        }
        printf("\n");
    }
    
    system("PAUSE");
    return ;
}

/***************************************************************************
*                                                                          *
*  Pre-conditions: problemcount > 0                                        *
*                                                                          *
*  Post-condition: Executes the multiplication game with problemcount      *
*                  problems and returns a number of seconds it took        *
*                  to correctly solve the problemcount problems.           *
*                  Note: This function corresponds to Program 3C.          *
*                                                                          *
***************************************************************************/
void multgame(problemcount)
// start body of program
{

    int numProblems, correct, answer;
    int start, end, opOne, opTwo, i;
    
    /*************************************
    * Seed the random number generator.  *
    *************************************/
    srand(time(0));
    
    /*********************************************
    * Get the number of problems the user wants. *
    *********************************************/
    printf("How many problems do you want?\n");
    scanf("%d", &numProblems);
    
    /***********************
    *  Get the start time. *
    ***********************/
    start = time(0);
    
    /*******************************
    *  Loop through each problem.  *
    *******************************/
    for (i=0; i<numProblems; i++) {
    
    /*********************************************************
    *  error in input - too many problems requested by user  *
    *********************************************************/
    if ( numProblems > 50 ){
      printf("\nYou requested more than 50 problems!\n");
        break;
        }
        
        /****************************************************
        * Generate the two operands for the multiplication. *
        * between 0 and 12 inclusive                        *
        ****************************************************/
        opOne = rand()%13;
        opTwo = rand()%13;
        
        /************************************************
        *  We will break out when we answer correctly.  *
        ************************************************/
        while (1) {
            
            /*********************************************************
            *  Print out the problem and read in the user's answer.  *
            *********************************************************/
            printf("Answer: %dx%d = ", opOne, opTwo);
            scanf("%d", &answer);
            
            /******************
            *  Wrong answer!  *
            ******************/
            if (answer != opOne*opTwo) 
                printf("Incorrect, try again.\n");
            
            /****************************************
            *  We got it, so get out of this loop!  *
            ****************************************/
            else
                break;      
        } // end-while
        
        printf("\n");
    } // end-for
    
    /**********************
    *  Get the end time.  *
    **********************/
    end = time(0);
    
    printf("You completed %d problems in %d seconds.\n", numProblems, end-start); 
    
    system("PAUSE");
    return ;
}

/***************************************************************************
*                                                                          *
*  Pre-conditions: ifp points to the begining of a valid                   *
*                  file containing data about this trip.                   *
*                  The format of this file specifies the                   *
*                  the format described in program 4.                      *
*                                                                          *
*  Post-condition: A graph will be printed out to the screen               *
*                  in the format specified by program 4b,                  *
*                  exactly how an 2007 Prius prints its fuel               *
*                  efficiency graph.                                       *
*                                                                          *
***************************************************************************/
void printfuelgraph(void)
// start body 
{
  
   double totalRevs = 0;
    double gallons, mpg, finalmpg, totalgallons = 0, numRotations;
    int triplength, interval, i, radius;
    char filename[100];
    double results[NUM_INTERVALS];
    
    /*****************************************************************************
    *  Initialize our array to store fuel efficiency for five-minute intervals.  *
    *****************************************************************************/
    for (i=0; i<NUM_INTERVALS;i++) results[i] = 0;
    FILE *ifp;

    /************************   
    *  Get the input file.  *
    ************************/
    printf("What file stores the car data?\n");
    scanf("%s", filename);
    ifp = fopen(filename, "r");
    
    /****************************************************************
    *  Read in the length of the trip and the radius of the wheel.  *
    ****************************************************************/
    fscanf(ifp,"%d", &triplength); 
    fscanf(ifp, "%d", &radius);
    
    /*************************************
    *  Loop through each time interval.  *
    *************************************/
    for (interval=0; interval<triplength/5; interval++) {
        
        /*******************************************
        *  These are the critical values to track  * 
        *  for each five-minute interval.          *
        *******************************************/
        totalRevs = 0;
        totalgallons = 0;
        
        /***************************************************
        *  Go through exactly five minutes worth of data.  *
        ***************************************************/
        for (i=0; i<INTERVAL_MIN*SECS_PER_MIN; i++) {
        
            /*********************************
            * Get the data for this second.  *
            *********************************/
            fscanf(ifp,"%lf", &numRotations);
            fscanf(ifp, "%lf", &gallons);
            
            /****************************************************
            *  Adjust our totals for the five minute interval.  *
            ****************************************************/
            totalRevs += numRotations;
            totalgallons += gallons;
            
        }
        
        /******************************************************
        *  Calculate and store the result for this interval.  *
        ******************************************************/
        results[interval] = findfueleff(radius, totalRevs, totalgallons);
    }
    
    /*************************
    *  Print out the chart.  *
    *************************/
    printChart(results);
    
    /**************************
    *  Close the input file!  *
    **************************/
    fclose(ifp);
    

//void printChart(double mpg[]) 
{
     
     int i, j;
     
    /******************************************
    * Loop through each row, starting at 19.  *
    ******************************************/
    for (i=19; i>=0; i--) {
        
        /*********************************
        *  Print the correct row label.  *
        *********************************/
        printf("%-4d", 5*i);
        
        /****************************
        *  Go through each column.  *
        ****************************/
        for (j=0; j<NUM_INTERVALS; j++) {
            
            /***************************************
            *  This bar is high enough, so print.  *
            ***************************************/
            if (mpg[j] > 5*i)
                printf("*****");
            
            /********************************************************    
            *  Otherwise, we must print spaces to fill the column.  *
            ********************************************************/
            else
                printf("     ");
            
            /*******************************    
            *  The space between columns.  *
            *******************************/
            printf(" ");
        }
        printf("\n");
        
    }
    
    /***************************************************************
    *  We can just hard-code this, since this is always the same.  *
    ***************************************************************/
    printf("    -----------------------------------\n");
    printf("    00-05 05-10 10-15 15-20 20-25 25-30\n");
  }
      system("PAUSE");
    return ;
}
  




Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,315
  • Joined: 25-December 09

Re: Assignment in C with functions and menus

Posted 17 November 2010 - 01:20 PM

Quote

Check your braces {}, insure that you have a closing brace for every opening brace.


You are missing at least 1 closing brace at about line 200.

Jim
Was This Post Helpful? 1
  • +
  • -

#8 rl1961  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-October 10

Re: Assignment in C with functions and menus

Posted 17 November 2010 - 01:34 PM

It has been a long day... I will run through the code again...Thank you for your time and patience!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1