I wrote this program about four hours ago and everything but my if loop is working. I wrote a separate one in a test file and it works, for the life of me I can't get it to work in this program. When inputting anything other than a y or n it shuts down when it should ask for a valid input like y or n. Any help would be appreciated.

#include<iostream>
#include<cmath>
//Function declarations.
void getinput(double& feet,double& inches);//Asks for the input from the user.
void metconv(double feet,double inches,double& meters,double& cm);//The function that converts feet and inches to meters and centimeters.
void printOutput(double meters,double cm);//Prints out the data after the calculations have been made.
const double meters_per_foot = 0.3048;//Constant for meters in a foot.
char getResponse();//This will rerun or terminate the program based on the users input.

    using namespace std; 
    int main()
    {
    double feet,inches,meters,cm;//Variables for the program.
	char yes_no = 'y';//Variable for the response to rerun or terminate loop.
    do//Beginning of the do-while loop.
    {
	    getinput(feet,inches);//This displays the user prompts for the getinput function.
	    metconv(feet,inches,meters,cm);//This will display the information that is processed for the conversion function.
	    printOutput(meters,cm);//This is display the total meters and centimeters.
	    yes_no = getResponse();
        
    }while(yes_no == 'y');//End of the do-while loop.
	return 0;
    }
    
    void getinput(double& feet,double& inches)//This function gathers the needed requirments of feet and inches.
    {
        using namespace std; 
    	cout << "\nPlease enter the feet length: ";
	    cin  >> feet;//This will be used for thecalcultions.
	    cout << "\nPlease enter the inches length: ";
	    cin  >> inches;//This will be used for thecalcultions.
	    return;
    }

    void metconv(double feet,double inches,double& meters,double& cm)//This is the conversion function that will convert the feet and inches into meters and centimeters.
    {
        using namespace std; 
	    double cominches,commeters;//These variables are local to this function.
	    cominches = (feet * 12) + inches;//This algorithm converts feet into inches.
	    commeters = (cominches / 12) * meters_per_foot;//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.
    }

    void printOutput(double meters, double cm)//This is the function that prints out the output of the conversion function. Using meters and centimeters.
    {
        using namespace std; 
		cout <<"\nThe conversion is " << meters << " meters and " << cm << " centimeters total." << endl;
    }
    char getResponse()
    {
         char c;
         do
         {
             cout << "Do you want to continue? (y for yes, n for no): ";
             cin >> c;
             if (c != 'y' && c != 'n' )
                 cout << "Invalid input, enter y or n" << endl;
         }while(c != 'y' && c != 'n');

         return c;
    }

So where exactly is your 'if loop'? Which line?

Sorry it's on line 61 and 62.

When you enter a value for the character c you also send the newline character into the stream. Could it be that the newline character is causing the problems?

I just tired this and it works

#include <iostream>

int main()
{
    char c;
    do
    {
	std::cout << "Do you want to continue? (y for yes, n for no): ";
	std::cin >> c;

	if (c != 'y' && c != 'n' )
	    std::cout << "Invalid input, enter y or n" << std::endl;
    }
    while(c != 'y' && c != 'n');

    return 0;
}

That works, but it doesn't work inside the function that it is embedded in.

I'm not seeing anything wrong with that function, but one of the other functions may be throwing it off. Try commenting out the other cin funtion, and assign the values and run the program. If that doesn't work, comment out each function one at a time until the getResponse function works.

I closed everything and restarted it and it worked. Maybe there was something inbuffer throwing it off.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.