I think there may be a math error in my program and I may not be seeing it. Is it possible for someone to go over it just to make sure it is ok. I have done everything I could think of to try to fix it, but it is eluding me. Thanks. I think the problem is near line 121.

#include<iostream>
#include<cmath>

void printMenu();//Function to get information from user
void getInputS(double& feet,double& inches);//Gathers the feet and inches from the user.
void getInputM(double& meters,double& cm);//Gathers the meters and centimeters from the user.
void standtometcon(double& feet,double& inches,double& meters,double& cm);//Declaration for standard conversion.
void mettostandcon(double& feet,double& inches,double& meters,double& cm);//Declaration for Metric conversion.
void printOutput(double& feet,double& inches);//Prints out the data after the calculations have been made.
void printOutput1(double& meters,double& cm);//Prints out the data after the calculations have been made.
const double meters_per_feet = 0.3048;//Constant for meters per foot.
const double feet_per_meter = 3.2808398950131235;//Constant for feet in a meter.
char getResponse();//Function that will ask the user to decide about repeating the program.


    int main()
    {
        //Variables for the program.
        double feet,inches,meters,cm;//Variables for the program.
        char rerun;
    do//Beginning of the do-while loop.
    {
         
         printMenu();//The only function call in the entire program that exists in the main body.
                     
    }while (rerun == 'y'); //End of do-while loop.     
    return 0;    
    }
    
    void printMenu()//This function gathers the needed requirments of feet and inches. It also calls all other functions that are used in this function other than itself.
    {   
        using namespace std;//Used per function no longer used globally.
        //Variables that are used locally in this function.
        int response = 0;
        double feet, meters, inches, cm;
        char rerun;

         do//Do-while loop that begins the if-else loop.
         {
              //These two cout statements asks the user which conversion he or she would like to perfom.        
              cout << "\nPress 1 to convert from standard to meteric: "<< endl;
              cout << "\nPress 2 to convert from meteric to standard: "<< endl;
              cout << endl;
              cin >> response;
         //Beginning of if-lse loop that encompasses most of the work that this program performs.     
         if( response == 1)
         {
              cout << "\nThis is where we convert from standard to meteric." << endl;//This staes to the user which conversion will take place.
              getInputS(feet,inches);//This is the function call that calls the function that gathers the information needed to determine meters and centimeters from feet and inches.
              standtometcon(feet,inches,meters,cm);//This is the function call that calls the function that converts the units from standard to meters.
              printOutput(meters,cm);//This is the function call that calls the function to print out the results. 
              rerun = getResponse();//This is the function call that calls the funtion that asks if the user wants to repeat the program.
              return;
         }
         else( response == 2);
         {
              cout << "\nThis is where we convert from meteric to standard." << endl;//This staes to the user which conversion will take place.
              getInputM(meters,cm);//This is the function call that calls the function that gathers the information needed to determine feet and inches from meters and centimeters.
              mettostandcon(feet,inches,meters,cm);//This is the function call that calls the function that converts the units from meters to standard.
              printOutput1(feet,inches);//This is the function call that calls the function to print out the results.
              rerun = getResponse();//This is the function call that calls the funtion that asks if the user wants to repeat the program.
              return;       
         }
         }while(response == 1 || response == 2);//End of do-while loop

         return ;
    }
    void getInputS(double& feet,double& inches)//Function definition for get input for feet and inches. It asks the user to input the number of feet and inches.
    { 
         using namespace std;//Used per function no longer used globally.
         cout << "\nPlease enter the amount of feet: ";
         cin  >> feet;
         cout << "\nPlease enter the amount of inches: ";
         cin  >> inches;
         cout << endl;
         return;
    }
    
    void standtometcon(double& feet,double& inches,double& meters,double& cm)//This is the function definition for the standard to meteric conversion.
    {
         using namespace std;//Used per function no longer used globally. 
	     double cominches,commeters;//These variables are local to this function.
	     cominches = (feet * 12) + inches;//This algorithm converts feet into inches by multiplying feet by 12 and then adding the additional inches.
	     commeters = (cominches / 12) * meters_per_feet;//This algorithm divides the totaled inches by 12 and then multiplies it by the number of meters in a foot.
         double number = commeters;//This sets number equal to commeter to use in the modf function. Allowing the algorithm to operate properly.
         cm = modf(number, &meters) * 100;//This is the modf function that splits the number with a decimal into two variables. EX. 3.12345 = 3.00000 + 0.12345
         
         /*
         Testing the algorithm to get accurate numbers from program.
         cout << feet << " feet and " << inches << " inches"<< " and " << meters << " and " << cm << endl;
         cout << cominches << " = cominches" << endl;
         cout << commeters << " = commeters" << endl;
         cout << number << " = number"  << endl;
         cout << cm << " = cm" << endl;
         cout << meters << " = meters" << endl;
         */
         return;
    }

    void getInputM(double& meters,double& cm)//This function gathers the users input for meters and centimeters.
    {
         using namespace std;//Used per function no longer used globally.
         cout << "\nPlease enter the amount of meters: ";
         cin  >> meters; 
         cout << "\nPlease enter the amount of centimeters: ";
         cin  >> cm;
         cout << endl;
         return;
    }
    
    void mettostandcon(double& feet,double& inches,double& meters,double& cm)//This is the function that will convert meteris into standard.
    {
        
         using namespace std;//Used per function no longer used globally.
         double comcm,comfeet;//These variables are local to this function.
	     comcm = (meters * 100) + cm;//This algorithm converts meters into centimeters by multiplying meters by 100 and then adding the additional centimeters.
	     comfeet = (comcm / 100) * feet_per_meter;//This algorithm divides the totaled centimeters by 100 and then multiplies it by the number of feet in a meter.
	    
         double number = comfeet;//This sets number equal to comfeet to use in the modf function. Allowing the algorithm to operate properly.
         inches = modf(number, &feet);//This is the modf function that splits the number with a decimal into two variables. EX. 3.12345 = 3.00000 + 0.12345
        
         /*
         Testing to get accurate numbers from program.
         cout << feet << " feet and " << inches << " inches"<< " and " << meters << " and " << cm << endl;
         cout << comcm << " = comcm" << endl;
         cout << comfeet << " = comfeet" << endl;
         cout << number << " = number"  << endl;
         cout << inches << " = inches" << endl;
         cout << feet << " = feet" << endl;
         */     
         
         return;
        
    }

    void printOutput(double& meters,double& cm)//This function will print out the results of the convers from standard to meteric.
    {
         using namespace std;//Used per function no longer used globally.
         cout <<"\nThere are " << meters << " meters and " << cm << " centimeters total." << endl;
         cout << endl;
         return;
    }

    void printOutput1(double& feet,double& inches)//This function will print out the results of the convers from meteric to standard.
    {
         using namespace std;//Used per function no longer used globally.
	     cout <<"\nThere are " << feet << " feet and " << inches << " inches total." << endl;
	     cout << endl;
	     return;
    }
    
        char getResponse()//This is the function that will ask for input from the user so the program will either rerun of terminate..
    {
        using namespace std;//Used per function no longer used globally.
    	char response,rerun;//Local variables to this function.
    	do//Beginning of the do-while loop.
    	{
            cout << "\nDo you want to continue? (y for yes, n for no): ";//Prompt asking if continuation is wanted.
            cin  >> response;

            if (response != 'y' && response != 'n' )//The if-loop to make sure the user inputs a y or n to get appropriate input.
            {
                cout << "\nInvalid input, enter y or n" << endl;//Output asking user to make sure to use correct input. 
            }
            else if (response == 'y')
            {
                printMenu();//If user chooses y then the program will rerun.
            }
            else
            {
                 cout << "\nGoodbye!";//If user chooses n then program terminates.
            }     
        }while(response != 'y' && response != 'n');
        response = rerun;
        return rerun;
    }

Edited 6 Years Ago by jmcorpse: n/a

Not sure if this is your issue but be aware that if you execute the following.

double cm = 0;
double meters = 3.54;
cm = modf(meters, &meters);

The values of meters and cm will be 3 and 0.54 respectavely. I see you multiplying by 100 when you extract the cm's from meters but not when you extract inches from feet, this could be the problem but as you didnt tell us what the error is from what you expect to what you get im just speculating.

Edited 6 Years Ago by Kanoisa: n/a

This article has been dead for over six months. Start a new discussion instead.