Bool function and user input not working more than once

My code prompts users to enter a rectangle and unless they say stop to keep entering, and print an error message if the name is invalid. The problem is when the user is prompted to enter the second rectangle, it immediately prompts the error message and then allows the user to enter the rectangle. Heres the loop and function. Theres more to the loop after but it's not relevant in this case.

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
37
38
39
40
41
42
43
44
while(userIn != "stop")
    {
      cout << endl << "Thank you! ";
      bool read = read_rect(promptNextRec, invError, usedError, userIn, list);
      while(read == false)
 {
   cout << endl << "Try again! ";
   read = read_rect(promptNextRec, invError, usedError, userIn, list);
 }

bool read_rect(const string prompt, const string invError, const string usedError, string & userName, vector <Rectangle> & list)
{
  cout << prompt;
  getline(cin, userName);
  if(userName == "stop")
    {
      return true;
    }
  else if(userName.substr(0,4) != "rec ")
    {
      cout << invError;
      return false;
    }
  else
    {
      int j(0);
      for(int i = 0; i < list.size(); i++)
        {
   if(userName == "rec " + list[i].getName())
          {
            j++;
          }
        }
      if(j != 0)
        {
          cout << usedError;
          return false;
        }
      if(j == 0)
        {
          return true;
        }
    }
}


What is the problem here?
Last edited on
Please post the minimal code to reproduce the error (especially main()).

Is it really necessary to pass prompt and error strings?
Topic archived. No new replies allowed.