the following program has a stupid bug:
#include%26lt;iostream%26gt;
#include%26lt;map%26gt;
using namespace std;
int main(void)
{
FILE *fin=fopen("gift1.in","r"), *fout=fopen("gift1.out","w");
int n;
fscanf(fin,"%d\n",%26amp;n);
char name[14][n];
map%26lt;char *,int%26gt; m;
for(int i=0;i%26lt;n;i++){
fscanf(fin,"%s\n",%26amp;name[i]);
fprintf(fout,"%s\n",name[i]);
}
map%26lt;char *,int%26gt;::iterator it;
fprintf(fout,"\n");
for(int i=0;i%26lt;n;i++)
m[name[i]]=0;
for(it=m.begin();it!=m.end();it++)
{
fprintf(fout,"%s =0\n",it-%26gt;first);
}
return 0;
}
//END
When the input is just fine, like:
5
Dave
Laura
Owen
Vick
Amr
It also prints the input correctly, but when I put the names into the map%26lt;char *,int%26gt; m, laura turns to lauraowen! I don't know why, just run the program with the default input and you will see!
Problem with Map in C++, map%26lt;char *,int%26gt;?
I suspect this declaration: char name[14][n]; has the subscripts reversed. Consider this: If each entry in the name array were 5 characters long (5 is the number of names), "Laura" would fit with no room for the null-byte terminator.
Hope that helps.
pear
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment