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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cwchar>
#include <string>
#include <windows.h>
usingnamespace 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'.