0

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.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by firstPerson
0

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

0

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;
}
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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.