if/else format within while loop

chief picture chief · Oct 22, 2010 · Viewed 9.6k times · Source
while(true)
{ 
  cout << "Name: ";
  getline(cin, Name);
  if(Name == "Stop")
    break;

  cout << "Additional Name - Y/N: ";
  getline(cin, additional);
  if (additional == "Y") 
    cout << "Additional Name: ";
  getline(cin, Name2);
  else
    cout << "Location: ";
  getline(cin, Location);
  if(Location == "Stop")
    break;
}


chief.cpp: In member function ‘void BookList::Fill()’:
chief.cpp:128: error: ‘else’ without a previous ‘if’

After the user enters the first name, I would like to give the option to enter a second name. If "N" just skip down to Location, if "Y" go to Name2 and then on to Location.

Answer

Diego Sevilla picture Diego Sevilla · Oct 22, 2010

You have to enclose the statements between the if and the else within brackets { ... }.