Hi all.

I recently began to give C++ a go again after failing on the first attempt.

I quickly ran into the same problem that led me to C# the first time around.

Strings, damn strings.

I simply cannot get my head around arrays of charactors, they stress me out, and I just want to return a string from a function. Last time around I never even new there were programming forums where people help each other out, and I gave up pretty easily.

I would appreciate any help, and even a frank reply if C++ just does not deal with strings.

I simply want a C++ compiled dll file to hold some strings, and return them when called.

Take my imaginary code here.

string __stdcall myfunc(string s)
{
	if (s == "banana")
	{
	     return "Good String";
	}
	return "Bad String";
}

Is there any sort of includable library I can add to make something like this work?

I have searched the forum but just keep coming up with char[]

Thanks for any replies
Suze

Looks fine to me.

#include<iostream>
#include<String>
using namespace std;
string __stdcall myFunc(string s)
{
	if (s == "banana")
	{
		return "Good String";
	}
	return "Bad String";
}
int main(){
	string s = "banana";
	cout << myFunc(s) << endl;
}

***Maybe you forgot the "#include<String>" header?

#include <iostream>
#include <string>
using namespace std;


string myfunc(string s){
{
	if (s.compare("banana") == 0)
	{
	     return "Good String";
	}
	return "Bad String";
}

int main (int argc, char* argv[]){
string s = "oranges"
string a = ""
a = myfunc(s);
cout << a;
}

The compare returns a non 0 if the case doesn't match as well. In order to compare two strings and ignore upper/lower case you need the toupper or tolower function.

Edit:
What the poster above me said as well. I thought the == was overloaded to work as compare in c++.

Edited 6 Years Ago by kes166: n/a

I thank you both for you replies, I'm sure I will use them to get the results I'm after.

Although I did add the namespace and included string, and also tried cstring, but the var just kept erroring saying "int assumed" :(

I just came back to post I'd found my solution and I nearly kicked myself when I found how easy it was, well it is half a solution I think.

LPSTR __stdcall retstr(void)
{
	LPSTR s = "hello";
	return s;
}

worked for returning the string, now your examples will help me compare one with an argument.

I really appreciate you time and advice.

Suze

Hi again I think I can call this matter solved <unsure>

I tried

LPSTR __stdcall retstr(LPSTR s)
{
	if (s == "test")
	{
	LPSTR ms = "hello";
	return ms;
	}
	return "goodbye";
}

With no luck, just kept on gettin "goodbye" returned :(

I was thinking about the solutions you provided and attempted but my dll is not made with classes and the IDE complained about it not being a class type so s.compare() was out unless I completely remade the dll using classes, (Im just using a dllmain with some simple declarations and functions)

Then it occured to me that Im not bound by the argument type, just the return so I used a double as an argument, and Its working as expected.

I have one last qusetion about this if someone has the time and knowledge.

Is there a limit on the length of my string I wish to return?

Edited 6 Years Ago by Suzie999: code tags

This question has already been answered. Start a new discussion instead.