I can't seem to figure out what I am doing wrong here. I can either get my loop to play indefinitely, not replay at all, or not initiate in the begging.

I'm very new to programming & would appreciated any help.

Code:

int main()
{
	int weight ; // weight in pounds
	int height ; // height in inches
	float BMI ; // Body Mass Index
	bool DoItAgain ;
	char Repeat ;

	do
	{  My Programming code here which is a BMI Calculator
                     with several IF options
                    
                     if (BMI >= 40.0)
		cout << "You are severely obese." << endl ;
	
	
	cout << " " << endl ;
	cout << "Would you like to enter a new height and weight? (y or n): " ;
	cin >> Repeat ;

	}
	while (DoItAgain = true) ;

	if ((Repeat == 'n'|| Repeat == 'N'))
	DoItAgain = false ;
	cout << "Goodbye" << endl ;
	return 0 ;
}

then I have a function subprogram code which works fine.

char Repeat ;

.
.
cin >> Repeat ;

Possibly you take BMI as input which leaves a '\n' in buffer which is accepted by Repeat and you don't get a yes / no chance. Either clear the buffer using standard C++ way or use strings string Repeat;

while (DoItAgain = true) ;

You are missing something here which you didn't miss it here. if ((Repeat == 'n'|| Repeat == 'N'))

It keeps recycling the program in an infinite loop. I am afraid I don't understand how to use strings or how to clear the buffer (or what that means for that matter).

humm...couple of thing might help you.
1)you never prompt the user to enter a BMI
2)you never asked for height or weight before asking for new ones.
3)you need to set DoItAgain to false inside the loop to end the loop. your little if statement needs to be inside the loop.

hope that helps

There is a thread about clearing buffers as a sticky somewhere.

Think about the two differences in C++ with regard to 'assigning a value' and 'comparing two values'.

In the do..while loop you want to compare DoItAgain with 'true'.
But in your code you assign DoItAgain the value 'true' (By saying 'DoItAgain = true' ).
Now how could you fix this and instead compare DoItAgain and true?


Also, in future please use code tags when you are posting part of your source code.. it makes it much more readable for us.

thank you for all your help. I finally managed to get my code working properly last night.
Sara

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.