When i run my program, it's suppose to say Good if you write your name correctly and meant to repeat itself if you didn't, for some reason it always say Good even though you wrote your name incorrectly. If you guys can, can you tell me how to fix the problem.
#include <iostream>
#include <string>
int main ()
{
std::string name;
read_the_users_name: // ask the user to enter the name
{
std::cout << "What is your name? ";
std::getline( std::cin, name );
check_if_the_name_is_correct : // ask the user if it was entered correctly
{
const std::string yes = "yes" ;
const std::string no = "no" ;
std::cout << "Are you sure it is " << name << "? " ;
std::string answer ;
std::getline( std::cin, answer ) ;
if( answer == yes )
{
std::cout << "Good!\n" ;
}
elseif( answer == no )
{
std::cout << "So, " ;
goto read_the_users_name ; // ask the user to re-enter the name
}
else
{
std::cout << "please type yes or no\n" ;
goto check_if_the_name_is_correct ; // ask for an yes ot no answer
}
}
}
}
Now, try to replace the labels and gotos with control structures.
Does it make your code easier to read and understand?
Yes: Keep the changes and throw away the gotos
No: Roll back the changes and revert to the gotos
Okay, i'll try using Control Structures but 1 question, What does the word "const" do?, i'm curious as i usually skim through the C++ manual and miss out important code.