0

What is wrong with the code? I wan't to insert five names into a map and maintain a count of all the unique names and finally print. This code crashes after first input is entered. Also tried scanf(). Couldn't debug further!!

#include<stdio.h>
#include<map>
#include<iostream>
#include<string>

using namespace std;

int main()
{
    map<char*, int> VoteCount;
    map<char*, int>::iterator it;
    char* str;
    int i, count;

    for(i = 1; i < 6; i++)
    {
        cin >> str;
        it = VoteCount.find(str);
        if (it != VoteCount.end()) //if name already present increment the count and insert <name, earlier_count+1>
        {
            count = it->second;
            VoteCount.erase(it);
            VoteCount.insert(pair<char*,int>(str,count+1));

        }
        else //insert the first element <name,count>
        {
            VoteCount.insert(pair<char*,int>(str,1));

        }
    }


    printf("Voting list:\n");
    for (it=VoteCount.begin(); it!=VoteCount.end(); it++)
            cout << it->first << " => " << it->second << '\n';


}
Shibir Basak
  • 101
  • 7

0 Answers0