Welcome! Log In Create A New Profile

Advanced

char * vs <string>

Posted by sirius 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
char * vs <string>
March 06, 2006 01:12PM
Okay, so I have got everything working like a charm, client, server, the whole toot, everything compiles, server listens, client connects, whoopee.

Then I run into this issue of passing the currency codes as character arrays. I mean, this is basic C and I'm lost smile

The problem is, you can't say:

if (fromCode == toCode)

because they're not strings. So, duh, just

#include <string>
string str1 = fromCode;
string str2 = toCode;

Right? Nope. Doesn't work. For some reason, the include is ignored in this environment.

I've set up a test program where I have a function like this:

void myFuncion(char *testStr) {
string myStr = testStr;
cout << myStr;
};

This works. You can say:

myFunction("Hello"winking smiley;

And you'll get the right result. Ie, you can initialize a string object with a character array (which we already know).

But it doesn't work when you port this into the currency_impl.cc file. You put #include <string> into the file and then the rest, and the compiler throws an undeclared identifier error on the keyword string.

Does anyone have any ideas how to solve this, either by getting string functionality into currency_impl.cc, or do I have to write a string comparison routine to test character arrays?
Re: char * vs <string>
March 07, 2006 09:23AM
OK, I sorted this. The answer is strcmp, which correctly compares the strings. My whole thing is working now.
Re: char * vs <string>
March 08, 2006 10:24AM
Hi,

Also make sure that you include using namespace std; if you would like to use <string>. Otherwise you can also use std::string.
Re: char * vs <string>
March 08, 2006 11:21AM
You beat me to it. Use std::string. There is nothing peculiar about "this environment".

Sorry, only registered users may post in this forum.

Click here to login