Need help with displaying values from a map once

Hi guys, i`m having problems displaying my values from a map. It always gives me two outputs. How do i consolidate into one.

the output is always


The avaliable timings forDavid on Monday
0800
0900
1000
1100
1200
1300
1400
1600
1700
1800
The avaliable timings forDavid on Monday
0800
0900
1000
1100
1200
1300
1400
1700
1800

where as i want the output to be

The avaliable timings forDavid on Monday
0800
0900
1000
1100
1200
1300
1400
1700
1800

below is my code snippet, i used a comparison of values between a textfile and a map and erased the values in a map if the values are found in the text file

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
void Scheduler::query(string user)
{
    map <string,string> allocatedTime;
    //Monday
    allocatedTime.insert(pair<string,string>("0800","Monday"));
    allocatedTime.insert(pair<string,string>("0900","Monday"));
    allocatedTime.insert(pair<string,string>("1000","Monday"));
    allocatedTime.insert(pair<string,string>("1100","Monday"));
    allocatedTime.insert(pair<string,string>("1200","Monday"));
    allocatedTime.insert(pair<string,string>("1300","Monday"));
    allocatedTime.insert(pair<string,string>("1400","Monday"));
    allocatedTime.insert(pair<string,string>("1500","Monday"));
    allocatedTime.insert(pair<string,string>("1600","Monday"));
    allocatedTime.insert(pair<string,string>("1700","Monday"));
    allocatedTime.insert(pair<string,string>("1800","Monday"));
    
    //Tuesday
    allocatedTime.insert(pair<string,string>("0800","Tuesday"));
    allocatedTime.insert(pair<string,string>("0900","Tuesday"));
    allocatedTime.insert(pair<string,string>("1000","Tuesday"));
    allocatedTime.insert(pair<string,string>("1100","Tuesday"));
    allocatedTime.insert(pair<string,string>("1200","Tuesday"));
    allocatedTime.insert(pair<string,string>("1300","Tuesday"));
    allocatedTime.insert(pair<string,string>("1400","Tuesday"));
    allocatedTime.insert(pair<string,string>("1500","Tuesday"));
    allocatedTime.insert(pair<string,string>("1600","Tuesday"));
    allocatedTime.insert(pair<string,string>("1700","Tuesday"));
    allocatedTime.insert(pair<string,string>("1800","Tuesday"));
    
    
    multiset <string> allocatedDay;
    allocatedDay.insert("Monday");
    allocatedDay.insert("Tuesday");
    allocatedDay.insert("Wednesday");
    allocatedDay.insert("Thursday");
    allocatedDay.insert("Friday");
    allocatedDay.insert("Saturday");
    
    
    ifstream fileOpen;
    fileOpen.open("schedule.txt");
    string cName,getData,uName,topic,time,duration,day,person1,person2,getDay;
    int counter=0;
    cout<<"Please enter a colleague`s name:";
    cin>>cName;
    cout<<"Please enter a day:";
    cin>>getDay;
    
    while(!fileOpen.eof())
    {
        getline(fileOpen,getData);
        
        if(getData.find(user)!=string::npos)
        {
            stringstream getInfo(getData);
            getline(getInfo,uName,';');
            
            if(uName==user)
            {
                
                
                for(int i=0;i<=counter;i++)
                {
                    getline(getInfo,topic,';');
                   
                    getline(getInfo,day,';');
                    getline(getInfo,time,';');
                    getline(getInfo,duration,';');
                    getline(getInfo,person1,',');
                    getline(getInfo,person2,',');
                    
                    if(getData.find(cName)!=string::npos)
                    {
                        map <string,string>::iterator it;
                        
                        if(day==getDay)
                        {
                        allocatedTime.erase(allocatedTime.find(time));
                           
                        cout<<"The avaliable timings for"<<cName<<" on "<<getDay<<endl;
                          for(it=allocatedTime.begin();it!=allocatedTime.end();it++)
                        {
                            cout<<(*it).first<<endl;
                        }
                        
                        
                      }    
                      
                        
                    }
 
                    
                }
                counter++;
                counter--;
                
            }
            
            
            
        }
    }
    fileOpen.close();
        
}


Thanks alot for your help! =D
Last edited on
CLearner88,

Can you think of a way to do this with a loop, so you don't have so much code?


1
2
3
4
5
6
7
8
9
10
11
allocatedTime.insert(pair<string,string>("0800","Monday"));
    allocatedTime.insert(pair<string,string>("0900","Monday"));
    allocatedTime.insert(pair<string,string>("1000","Monday"));
    allocatedTime.insert(pair<string,string>("1100","Monday"));
    allocatedTime.insert(pair<string,string>("1200","Monday"));
    allocatedTime.insert(pair<string,string>("1300","Monday"));
    allocatedTime.insert(pair<string,string>("1400","Monday"));
    allocatedTime.insert(pair<string,string>("1500","Monday"));
    allocatedTime.insert(pair<string,string>("1600","Monday"));
    allocatedTime.insert(pair<string,string>("1700","Monday"));
    allocatedTime.insert(pair<string,string>("1800","Monday"));



What were you thinking here?

1
2
counter++;
                counter--;


The following does things 1 more time than what counter is - Is that what you want?
for(int i=0;i<=counter;i++)

The normal idiom is :

for(int i=0;i < counter;i++)

At that stage counter is zero and is not altered by ++ and -- I mentioned above, so that whole for loop isn't executed at all.

There you go, some stuff to think about. Good luck
oh okay thanks =)
No worries, interested to see how you go.
Topic archived. No new replies allowed.