Hiya Guys!!!

Am hoping you guys can tell me what I'm doing wrong. My Code compiles, and seems to run OK up until it gets to the "if" statement towards the end. Instead of waiting for a response it just closes.

int main()
{
	cout << "\n\t\tWelcome to Doc's gas figure out thingy!\n";

	Car car;

	bool another = true;
	while(another)
	{
		Car next;
		next.drive();
		
		cout << "\n\n\n\t\t\tHere are your results ";
		next.print_result();

		cout << "\n\n\t\t\tAnother Car? (y/n): ";
		string response;
		getline(cin, response);
		if (response == "y")
			another = true;

		else
		
	}
	
	
	return(0);

I've tried ... if (response != "y") another = false ... as well with the same result.
Any assistance would be greatly appreciated.

OOps ... the else statement was another failed attempt at getting it to work, and I know its not really needed... and currently it isn't populated. Oh and if you guys need the member functions as well please let me know and I'll include them, I just thought that most of the time complete work wasn't desired. (As this is a homework assignment).

Thanks guys for your time!!!

Doc

I fixed up your if statement at the bottom so if you do not type in y or Y it will end.

#include <iostream>

using namespace std;

int main()
{
	cout << "\n\t\tWelcome to Doc's gas figure out thingy!\n";

	Car car;

	bool another = true;
	while(another)
	{
		Car next;
		next.drive();

		cout << "\n\n\n\t\t\tHere are your results ";
		next.print_result();

		cout << "\n\n\t\t\tAnother Car? (y/n): ";
		string response;
		getline(cin, response);
		if ( response != "y" && response != "Y" )
			another = false;

	}


	return 0;
}

I think this calls for a do/while loop :

bool driveAnotherCar = false;
do{
  /* drive car code here*/
  char ch = 0;
  cin >> ch;
  if( tolower(ch) == 'y') driveAnotherCar = false;
}while(driveAnotherCar)
This article has been dead for over six months. Start a new discussion instead.