Trouble while submitting answer in C++ [TIDRICE]

I submitted the following answer for the problem TIDRICE:

#include <iostream>
#include <map>

int main(void) {
    std::map <std::string, char> votes;
	std::map <std::string, char>::iterator traverse;
	int t, n, c, i;
	char name[20], vote;
	std::cin>>t;
	while(t--) {
		std::cin>>n;
		for(i=0; i<n; i++) {
			std::cin>>name>>vote;
			votes[name] = vote;
		}
		c=0;
		for(traverse=votes.begin(); traverse!=votes.end(); ++traverse)
			if(traverse->second=='+')
				c++;
			else
				c--;
		std::cout<<c<<"\n";
	}
	return 0;
}

It is running perfectly well on ideone, but is giving WA on codechef. Now, I always use C, and this is my first submission in C++. I tried submitting the answer both in C++ 4.3.2 and C++ 4.8.1, but got WA in both the cases. So, I checked some other solutions for this problem in C++, and most had included “stdio.h” and used “scanf” and “printf” instead of “cin” and “cout”. So, are there some guidelines which I need to follow while submitting my answers in C++?

Also, what is the difference between submitting an answer in C++ 4.3.2 and 4.8.1. Which one should I follow?

Thanks!

the problem with ur code is that u havent cleared the map after every test-case…which results in extra names in the map for the further cases…just add this statement before the end of ur while loop…

votes.clear();

this should solve ur problem…:slight_smile:

EDIT:

For ur second ques…there are no such guidelines…all headers of C can be used in C++…also instead of writing “std::” before every cin count map, etc…u can just write “using namespace std;” just before writing ur code, outside the main fxn (ideally where all the header files are written). As it can be seen clearly 4.8.1 is an upgraded version of 4.3.2 it has many new features(C++11 features) which u can see here…!!!

1 Like

That did it! Thanks! But what about C++ 4.3.2 vs 4.8.1? Which one should I use? And which I/O method is preferred: cin, cout or scanf, printf?

scanf printf is preferred as it is faster than cin cout…also u can use ne of the 2 compilers…only thing i have noticed is that my c++4.3.2 code when submitted on c++4.8.1 takes a bit more time and memory…but the difference is not significant…!!!

@walterwhite: if the given answer solves your query, please accept it and show some love by voting it up. :slight_smile: