yes or no loop back

Hello I am trying to write a code to answer a yes or no question. If yes go to next task and if no go back to the beginning. But what I have is this, it will keep going to the next task no mater what I pick. Any help is greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using namespace std;
int main()
{
    string a, b, c, location;
    char answer;

    while ( answer == 'n'){

    cout << " \n give me first street name: ";
    cin >> a;
    cout << " \n give me second street name: ";
    cin >> b;
    c = a + " & " + b;
    cout << c  ;
    cout << " is that right(y/n) " << endl;
    cin >> answer;
    }
    if (answer != 'N' && answer != 'n')
    cout<<" \n oh no " << endl;
    else if (answer != 'Y' && answer != 'y')
    cout<<" \n ok " << endl;
       
}

thanks again.
!= means does not equal. answer == 'y' will get
ok
as output

use == instead of != I think you might be confusing yourself.
Hello, here is some code that will help you figure out how to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;
int main()
{
	//intitialize variable
	char answer = 'n';

	//while loop to continue till they are satisfied with their input
	while (answer == 'n' || answer == 'N')
	{
		//do something
		cout << "Do something here" << endl;

		//ask if it was right
		cout << "Is that right(y/n)?" << endl;

		//user input
		cin >> answer;

		//if statmen to output wether they are going to continue or not
		if (answer == 'y' || answer == 'Y')
		{
			cout << "We will contiue then" << endl;
		}
		else
		{
			cout << "Ok, we'll try again" << endl;
		}
	}

	return 0;
}
Yes it worked, thank you Joe and KingKush for your reply.
Make use of the toupper function to halve the logic:

1
2
3
4
5
6
7
8
9
10
#include <cctype>

char answerAsUpperCase = std::toupper(answer);

while ( answerAsUpperCase == 'N')
	{
// ....

if (answerAsUpperCase == 'Y')
		{


http://en.cppreference.com/w/cpp/string/byte/toupper
Topic archived. No new replies allowed.