It is displaying only first name of first pair and second value of last pair
totally mismatch. Can any one help me ? -
#include<string>
-
#include<iostream>
-
#include<map>
-
-
#define SIZE 100
-
-
using namespace std;
-
-
class ObjectStore
-
{
-
map<char *, char *> smap;
-
-
public:
-
-
void setProperty(char *, char *, char *);
-
char* getProperty(char *, char *);
-
-
};
-
-
void ObjectStore::setProperty(char *area, char *property,
-
char* value)
-
{
-
strcat(area, property);
-
smap.insert( make_pair( area, value ) );
-
}
-
-
char* ObjectStore::getProperty(char* area, char* property)
-
{
-
char *tmp;
-
char name[SIZE];
-
strcat(name, area);
-
strcat(name, property);
-
map<char *, char *>::iterator iter = smap.begin();
-
-
while(iter != smap.end())
-
{
-
cout << " Name : "<<iter->first<<" Value : "<<iter->second;
-
iter++;
-
}
-
tmp = new char[SIZE];
-
// strcpy(tmp, iter->second);
-
return tmp;
-
}
-
-
int main()
-
{
-
char area[50], property[50], value[50], *tmp;
-
int i;
-
ObjectStore ost;
-
for(i=1;i<4;i++)
-
{
-
cout<<" Enter Area, Property and value :"<<endl;
-
cin>>area>>property>>value;
-
ost.setProperty(area, property, value);
-
cout<<" Inserting record number "<<i;
-
}
-
strcpy(area, "ice");
-
strcpy(property, "feels");
-
tmp = ost.getProperty("ice", "feels");
-
cout<<endl<<endl<<tmp;
-
//delete []tmp;
-
return 0;
-
}
5 2491
It is displaying only first name of first pair and second value of last pair
totally mismatch. Can any one help me ? -
#include<string>
-
#include<iostream>
-
#include<map>
-
-
#define SIZE 100
-
-
using namespace std;
-
-
class ObjectStore
-
{
-
map<char *, char *> smap;
-
-
public:
-
-
void setProperty(char *, char *, char *);
-
char* getProperty(char *, char *);
-
-
};
-
-
void ObjectStore::setProperty(char *area, char *property,
-
char* value)
-
{
-
strcat(area, property);
-
smap.insert( make_pair( area, value ) );
-
}
-
-
char* ObjectStore::getProperty(char* area, char* property)
-
{
-
char *tmp;
-
char name[SIZE];
-
strcat(name, area);
-
strcat(name, property);
-
map<char *, char *>::iterator iter = smap.begin();
-
-
while(iter != smap.end())
-
{
-
cout << " Name : "<<iter->first<<" Value : "<<iter->second;
-
iter++;
-
}
-
tmp = new char[SIZE];
-
// strcpy(tmp, iter->second);
-
return tmp;
-
}
-
-
int main()
-
{
-
char area[50], property[50], value[50], *tmp;
-
int i;
-
ObjectStore ost;
-
for(i=1;i<4;i++)
-
{
-
cout<<" Enter Area, Property and value :"<<endl;
-
cin>>area>>property>>value;
-
ost.setProperty(area, property, value);
-
cout<<" Inserting record number "<<i;
-
}
-
strcpy(area, "ice");
-
strcpy(property, "feels");
-
tmp = ost.getProperty("ice", "feels");
-
cout<<endl<<endl<<tmp;
-
//delete []tmp;
-
return 0;
-
}
Yeah, you are using char* but not allocating them. Doing this will make the pointers all point to the same value at the end of your loop. Use strings everywhere instead.
Adrian
Yeah, you are using char* but not allocating them. Doing this will make the pointers all point to the same value at the end of your loop. Use strings everywhere instead.
Adrian
If I use strings instead of Char * then, I cannot use strcat() and strcpy()
functions. These functions work only with char *.
Please suggest me the ultimate approach for this.
Regards,
Girish.
If I use strings instead of Char * then, I cannot use strcat() and strcpy()
functions. These functions work only with char *.
Please suggest me the ultimate approach for this.
Regards,
Girish.
string equivalents: -
char cstring1[]="Hello ", cstring2[]="there!", cstring3[100];
-
strcpy(cstring3, cstring1);
-
strcat(cstring3, cstring2);
-
- string cppstring1 = "Hello ", cppstring2 = "there!", cppstring3;
-
cppstring3 = cppstring1;
-
cppstring3 += cppstring2; // or
-
cppstring3 = cppstring3 + cppstring2;
-
Adrian
string equivalents: -
char cstring1[]="Hello ", cstring2[]="there!", cstring3[100];
-
strcpy(cstring3, cstring1);
-
strcat(cstring3, cstring2);
-
- string cppstring1 = "Hello ", cppstring2 = "there!", cppstring3;
-
cppstring3 = cppstring1;
-
cppstring3 += cppstring2; // or
-
cppstring3 = cppstring3 + cppstring2;
-
Adrian
It is working fine. Thanks a million.
Girish.
It is working fine. Thanks a million.
Girish.
Glad to help.
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
by: forums_mp |
last post by:
I've got an STL class (see below) with two functions to store and
retrieve data - msg structs.
The "Store" function when called will copy the received message
(depending on which message) into...
|
by: matvdl |
last post by:
I have a system that was originally developed in asp - the pages are saved in
SQL (there are over 10,000 pages) and saved to a temp directory in the server
when requested by a client.
I have...
|
by: yccheok |
last post by:
I have an immutable object, where I do not provide implementation on
=operator. However, I am facing a problem when trying to use it with
stl map. stl map requires the object to have =operator...
|
by: Jan |
last post by:
Hi there,
i've got an STL map with something like this ( map<string, Object*>
xyz; )
What happens when I call xyz.clear()?
Is only the map cleared or the map and the Objects, so that the memory...
|
by: vin b |
last post by:
Hi,
How would I invoke a method on a reference to an STL object (STL map in
this
case)
In this contrived example, I want to invoke the insert() method on
localmap?
Thanks, Vin
|
by: slonial |
last post by:
Hi,
I am using two STL maps as data member for COM server with
VC++6.0.These two maps are unrelated in the sense that they are storing
different data.
first map is of type(6 MB in size)...
|
by: Allerdyce.John |
last post by:
I have a a stl map which use stl pair as the key
class A;
type map< pair<int, int>, A> MyMap;
if I have function which add entry to the map:
void func(int x, int y, MyMap& map, A& a) {...
|
by: olanglois |
last post by:
Hi,
I was asking myself to following question. What is better to erase an
element from a STL map:
calling (option #1)
size_type erase(const key_type& k)
or calling (option #2)
|
by: rockkyy |
last post by:
Hi all,
ok here it goes..
I am going to implement a STL MAP.. say 'mapToken'..this is a GLOBAL map.
This mapToken has many nodes with its keys ranging from t1,t2,t3 ....t100 .
Now in...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |