Problem with a do while loop

Hello, I'm trying to work this out.
I can't find any answers specific to this problem, and I'm not a C++ Guru so I don't know all the different ways of getting my desired outcome.
Basically I want this program to ask for a name, with multiple words, and then confirm the correctness of the output, and if the user doesn't like the output, redirect them to the beginning (or at least to ask for input to define string name again).
The error that I'm getting is: "'answer' was not declared in this scope"
So I'm assuming it's got something to do with the placement.
How can I fix this code? Or is there another way to get the same outcome?
Thanks in advance

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cwchar>
#include <string>
#include <windows.h>
using namespace std;

int main(int argc, char** argv)
{
        do
        {
	string name;
	cout << "What is your name, adventurer?" << endl;
	cin >> ws;		
	getline(cin, name);
	cout << endl;
		
	cout << "Is this the name you desire?" << name << endl;
	char answer;
	cout << "y/n" << endl;
	cin >> answer;
        }
        while(answer == 'n');
}
Declare both name and answer outside of the loop, i.e. before the do.
Thanks for your response keskiverto, I've re-written the code, the way I think you're telling me, this is what I got:

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
34
35
36
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cwchar>
#include <string>
#include <windows.h>
using namespace std;

int main(int argc, char** argv)
{
    string name;
    cout << "What is your name, adventurer?" << endl;
    cin >> ws;
    getline(cin, name);
    cout << endl;

    char answer;
    cout << "Is this the name you desire?" << name << endl;
    cout << "y/n";
    cin >> answer;

    do
    {
        string name;
        cout << "What is your name, adventurer?" << endl;
        cin >> ws;
        getline(cin, name);
        cout << endl;

        cout << "Is this the name you desire?" << name << endl;
        char answer;
        cout << "y/n" << endl;
        cin >> answer;
    }
    while(answer == 'n');
}


It now compiles with no errors, however, when I run the program it asks for a name twice. Do you have any more advice as to how I can correct the code?
Thanks again.
I said declare. Keep the use inside the loop. You still declare those variables inside the loop body too, which is an error.
.
string name;
This is a declaration. It introduces a variable, which has name 'name' and type 'string'.
Okay, I got it worked out now.
For reference in case anyone is wondering, this is the code that produced the desired outcome:

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cwchar>
#include <string>
#include <windows.h>
using namespace std;

int main(int argc, char** argv)
{
	string name;
	char answer;
    do
    {
        cout << "What is your name, adventurer?" << endl;
        cin >> ws;
        getline(cin, name);
        cout << endl;

        cout << "Is this the name you desire?" << name << endl;
        cout << "y/n" << endl;
        cin >> answer;
    }
    while(answer == 'n');
}

Thanks for the help keskiverto, it helped a lot.
Topic archived. No new replies allowed.