Skipped Iterator For Loop

My for loop is skipped when debugging, and even when I step-into I could not figure out why. Program requires you to add at least one person first. Thankyou.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
#include <map>
#include <iterator>

using namespace std;

int main()
{
    while(true)
    {
        cout << "Welcome to the main menu!" << endl;
        cout << endl << "Log in (1), Create Account (2)" << endl;
        map<string,string> accountTable;
        int x;
        cin >> x;
        if (x == 1)
        {
            cout << "Username: ";
            string username;
            cin >> username;
            for (map<string,string>::iterator itr = accountTable.begin(); itr != accountTable.end(); ++itr)
            {
                if (itr->first == username)
                {
                    cout << "Password: ";
                    string password;
                    cin >> password;a
                    if (itr->second == password)
                    {
                        cout << "Options: Leave (1), Delete Account (2)" << endl;
                        cin >> x;
                        if (x == 1)
                        {
                            break;
                        }
                        else if (x == 2)
                        {
                            accountTable.erase(itr);
                        }
                    }
                    else
                        cout << "Wrong password. Exiting\n";
                }
            }
        }
        if (x == 2)
        {
            cout << "Username: ";
            string username;
            cin >> username;
            cout << "Password: ";
            string password;
            cin >> password;
            accountTable[username] = password;
        }
    }
}
closed account (E0p9LyTq)
What is the output you are getting?

Your container initialization is in the wrong place. Every time you go through your while loop it creates/recreates accountTable. Move map<string,string> accountTable; outside your while(true) loop, just above it.

8
9
10
11
12
13
int main()
{
   map<string,string> accountTable;

   while(true)
   {


Look at line 28, cin >> password;a. Typo when you copied your source?
Last edited on
Yes, typo. I've consistently noticed that I too often assume that where the code isn't working properly is where the problem is. Shifting the map to the proper location fixed my issue completely.
Topic archived. No new replies allowed.