crosses intializtion erorr?

in this code var seems to only work when declared outside of the switch, and whenever i tried to declare it in say case 1 of switch , then i get crosses intializtion erorr....
 avatar
unknown
c_cpp
3 years ago
933 B
4
Indexable
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
/*
sample input:
7
1 Jesse 20
1 Jess 12
1 Jess 18
3 Jess
3 Jesse
2 Jess
3 Jess
*/
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
    int q=0,type=0,y=0;
    string x;
    std::map<string,int> data;
    cin>>q;
    std::map<string,int>::iterator var;
    while(q--){
        cin>>type>>x;
        switch (type)
        {
        case 1:
            cin>>y;
            var=data.find(x);
            if(var!=data.end()) data[x]=y+data[x];
            else data.insert(make_pair(x,y));
            break;
        case 2:
            data.erase(x);
            break;
        case 3:
            var = data.find(x);
            var!=data.end()
            ?cout<<var->second<<endl
            :cout<<"0"<<endl;
            break;
        }
    }
return 0;
}
Editor is loading...