Click here to Skip to main content
15,905,148 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to use the MAP COUNT and have a fewproblems. It has to search data[] for keywords, can't seem to figure that out. And secound it's not debugging on the == parts, something about a value of type "const char *" cannot be assigned to an entity type of "int" and lastly it says that return value typedoes not match the function type. Any help is more than appreciated!

C++
#include <stdio.h>
#include <iostream>
#include <map>
using namespace std;
void printRawData(unsigned char *data, int length, int more)
{
	int i, c=0;
	printf("     -------------IP Data Begins-------------\n");
	for (i=0; i<length;>	{
		if ((data[i]>30 && data[i]<122) || 
			(((data[i]==10) || (data[i]==13) || (data[i]==123) || (data[i]==125))
            && (more>0)))
		{
			printf("%c", data[i]);
			//////////////////////////////////////////////////////
			map<char,int> mymap;
			char c;
			mymap [1] = "audio/basic";
			mymap [2] = "audio/x-aiff";
			mymap [3] = "audio/x-wav";
	
			for (c='1'; c<'3'; c++)
			{
			cout << c;
			if (mymap.count(c)>0)
				cout << " is an element of mymap.\n";
			else 
				cout << " is not an element of mymap.\n";
			}
			return 0;
			////////////////////////////////////////////////////
			c+=1;
        }
		else
		{
			//printf("[%i]", data[i]);
			c+=3;
			if (data[i]>9) c++;
			if (data[i]>99) c++;
                }
		if (c>=47)
		{
			printf("\n");
			c=0;
                }
       }
}</iostream></stdio.h>
Posted
Comments
Philippe Mori 1-Sep-11 21:06pm    
You have asked similar question many times. By doing that, it helps nobody. We have more question to read, you loose points, get bad report (duplicate) and you still have to sort it all by yourself to make something sensible out of that.

[EDIT]
My mistake, on first reading I missed that you are using
XML
map<char,int> mymap;
and try to perform assignment like
C#
mymap [1] = "audio/basic";

You may change key type, as Stefan says, and you also must change value type.


For "return value type does not match the function type" change
C++
return 0;

to
C++
return;
 
Share this answer
 
v2
[edit]The following was meant as an addition to Solution 1[/edit]
You enter values for the keys (char)(1), (char)(2) and (char)(3), but in the for loop you query values for the keys '1', '2' and '3' which are different altogether!

Either switch the keys for entering your values to '1', '2' and '3' or switch your for loop to run from 1 through 3 rather than '1' through '3'.

Hint try printing the actual numerical value of '1' like this:
C++
cout << (int)'1' << endl;

You might be surprised...
 
Share this answer
 
v2
You have a couple of problems here. In a map, the template parameters are the [key, value] types, so you have a map with a char key and int value.

Since char is an iteger type, it's perfectly legal to use numeric constants as keys, like you do here: mymap [1]. This refers to a map<char, int> element with the char key being the number 1 (which is not the same thing as the character '1').

As it happens, pointers are also integer types, and literal text strings (eg "audio/basic" for instance) are actually const char*.

So in the line mymap [1] = "audio/basic"; you have a map element where the char key is number 1, and the int value is the address of the string constant.

I suspect that what you wanted was a map with an int key and a text string value, like this:

C++
map<int, string> mymap;
mymap [1] = "audio/basic"; // int key 1, string value "audio/basic"


As others have pointed out, the for loop is using characters (c='1'; c<'3';) rather than numerical values (c = 1; c< 3;), which will mess things up, since 1 != '1'.

So with the changed mymap from above, the loop should be like this:
C++
for (int c = 1; c< 3; c++)
{
  cout << c;
  if (mymap.count(c)>0)
...
.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900