Why compiler is giving me error on this do-while statement in C++?

I am trying the follow program. When I run this program compiler gives me a error that y and Y are not declared in this scope. So, I am following all the instructions which are given in book "Problem Solving With C++ by Walter Savitch" then why I am facing this error?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{
	int end_p;
	char ans;

	do
	{
		cout << "Hello.\n";
		cout << "Do you want another greeting?\n";
		cout << "Type y for Yes and n for No and press enter key.\n";
		cin >> ans;
	} while ( (ans == y) || (ans == Y) );

	cout << "Good bye\n";
	cin >> end_p;

	 return 0;
}
Because you dont have a variable called y or Y declared in the scope.
If you want to get to the characters y and Y use ' '.

} while ( (ans == 'y') || (ans == 'Y') );

Note: If you ever want to do the same for something longer than one character, such as the word Yes, then you'lll need " ".
 
} while ( ans == "Yes"); // assuming ans is of type string 
Last edited on
Thanks @TarikNeaj Problem solved.
Topic archived. No new replies allowed.