Function Return an entire word

How can i return a word?

Page 1 of 1

13 Replies - 8781 Views - Last Post: 03 October 2010 - 05:28 AM Rate Topic: -----

#1 surreal_kc  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 27-September 10

Function Return an entire word

Posted 30 September 2010 - 11:49 PM

I currently am following these lessons on a very nice tut site. The only thing is, after i learn the material i like to push it further and see what else i can do. I hit a road block however. I need a function to return a single word. I have an enum with 4 const in it. North, East, South, West. I need my program to output these as words, not as integrals.

This is what i have in place for now, it gets the job done, but seems sloppy:
int main(){
	using namespace std;

	CTank qTank = {1, 1, keNorth};

	cout << "Loc: (" << qTank.miX << ", " << qTank.miY << ")" <<endl;
	cout << "Facing: ("; ConvertEnum(qTank.meDir); cout << ")" <<endl;

	cin.get();
	return 0;
}


void ConvertEnum(int iDir){
	switch(iDir){
	case 0:
		{
		   std::cout << "North";
		   break;
		}
	case 1:
		{
		   std::cout << "East";
		   break;
		}
	case 2:
		{
		   std::cout << "South";
		   break;
		}
	case 3:
		{
		   std::cout << "West";
		   break;
		}
	default:
		{
		break;
		}
	}
}



I would like to know if it is possible to print out enum's actual word for the const variable, and also since im on this topic, if there is a way to return a string, or array of chars from a Function.

Is This A Good Question/Topic? 0
  • +

Replies To: Function Return an entire word

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Function Return an entire word

Posted 01 October 2010 - 12:16 AM

Do you mean something like this.


#include <iostream>

char* function(int);
using namespace std;
int main()
{
int num;
cout<<"Enter a number";
cin>>num;
cout<<function(num);
}

char* function(int num)
{
 char* array[6]={"North","East","South","West"};

  return array[num];
}



I don't think you can print the word using enums, only the number.

This post has been edited by simeesta: 01 October 2010 - 12:18 AM

Was This Post Helpful? 1
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Function Return an entire word

Posted 01 October 2010 - 01:05 AM

What is your namespace line doing inside main()?
Get it out of there!

View Postsurreal_kc, on 01 October 2010 - 03:49 PM, said:

int main(){
	using namespace std;
}




View Postsurreal_kc, on 01 October 2010 - 03:49 PM, said:

if there is a way to return a string ... from a Function.

If you want to see if something will work in code write some code and see if your idea works.
Try this:
#include <iostream>
#include <string>
using namespace std;   

string addX(string);

int main(){
	string theString = "aaa";
	cout << "theString = " << theString << endl;
	theString = addX(theString);
	cout << "theString = " << theString << endl;
	
	return 0;
}

string addX(string inString){
	inString.append("X");
	return inString;	
}



Compile it. Run it.
Does it work?
Do you understand how it works?
Could you have written that? I am confident you could have.

EDIT
You have two or three other interesting questions in there but answering them all at once is too much trouble to be bothered with. You may well find that you do better to ask one, clear, well thought through, question at a time. That way you'll know what question is being answered and are more likely, over time, to get all your questions answered in meaningful ways.

This post has been edited by janotte: 01 October 2010 - 01:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 surreal_kc  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 27-September 10

Re: Function Return an entire word

Posted 01 October 2010 - 02:20 AM

View Postjanotte, on 01 October 2010 - 12:05 AM, said:

What is your namespace line doing inside main()?
Get it out of there!

View Postsurreal_kc, on 01 October 2010 - 03:49 PM, said:

int main(){
	using namespace std;
}


This is very interesting to me that you said that. If anyone else with fewer post counts then yourself said this to me i would have brushed them off. However, you come across as some one who knows a lot. So, why would i want to place namespace outside of the local scope? I was told in my tutorials at www.xoax.net to always localize all the things i can. That including using namespce std. They have yet to let me down once.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Function Return an entire word

Posted 01 October 2010 - 03:21 AM

View Postsimeesta, on 01 October 2010 - 01:16 AM, said:

char* function(int num)
{
 char* array[6]={"North","East","South","West"};

  return array[num];
}



Good. I do similar. Unfortunately, you have a few issues.

The biggy is just declaring char; there really is no guarantee that volatile memory location will be valid outside the function. Having questionable size is a red flag. Also, you might as well check if your number is valid.

e.g.
const char *getDirection(int i) {
	static const int size = 4;
	static const char *values[size]={"North","East","South","West"};
	return (i<0 || i>=size) ? NULL : values[i];
}


Was This Post Helpful? 1
  • +
  • -

#6 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Function Return an entire word

Posted 01 October 2010 - 03:44 AM

View Postbaavgai, on 01 October 2010 - 11:21 AM, said:

View Postsimeesta, on 01 October 2010 - 01:16 AM, said:

char* function(int num)
{
 char* array[6]={"North","East","South","West"};

  return array[num];
}



Good. I do similar. Unfortunately, you have a few issues.

The biggy is just declaring char; there really is no guarantee that volatile memory location will be valid outside the function. Having questionable size is a red flag. Also, you might as well check if your number is valid.

e.g.
const char *getDirection(int i) {
	static const int size = 4;
	static const char *values[size]={"North","East","South","West"};
	return (i<0 || i>=size) ? NULL : values[i];
}


My code compiled with g++ and ran correctly but gave a few warnings. So i assumed it worked, even with the warnings. Thanks!

This post has been edited by simeesta: 01 October 2010 - 03:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Function Return an entire word

Posted 01 October 2010 - 04:05 AM

I used the same compiler. The warnings you got were probably complaining about not using string in C++. For some reason it's less bitchy about const char * than it is about char *.

C and C++ will happily compile code that will crash. As long all the logic is good, it's happy. Passing back a pointer to a volatile location is one of those C gotchas that doesn't always show up immediately. Though modern compilers are pretty good about catching it.

Since char * are special cases, they often get away with more. The following code illustrates the point:
#include <iostream>

using namespace std;

int *getNum() {
	int n = 42;
	return &n;
}

int main() {
	int *n = getNum();
	cout << *n << endl;
	
	int a,b,c,d;
	
	cout << *n << endl;
	return 0;
}



baavgai@dic:~/Dev/C$ g++ t.cc
t.cc: In function ‘int* getNum()’:
t.cc:6: warning: address of local variable ‘n’ returned
baavgai@dic:~/Dev/C$ ./a.out 
42
7641336



Now, the char * thing is rather odd. Technically, a string literal doesn't move, so it might be considered safe to use that way. However, it's best not to be in the habit.

Make the above n static and it compiles without complaint and returns consistent results.
Was This Post Helpful? 1
  • +
  • -

#8 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Function Return an entire word

Posted 01 October 2010 - 04:09 AM

View Postbaavgai, on 01 October 2010 - 12:05 PM, said:

I used the same compiler. The warnings you got were probably complaining about not using string in C++. For some reason it's less bitchy about const char * than it is about char *.

C and C++ will happily compile code that will crash. As long all the logic is good, it's happy. Passing back a pointer to a volatile location is one of those C gotchas that doesn't always show up immediately. Though modern compilers are pretty good about catching it.

Since char * are special cases, they often get away with more. The following code illustrates the point:
#include <iostream>

using namespace std;

int *getNum() {
	int n = 42;
	return &n;
}

int main() {
	int *n = getNum();
	cout << *n << endl;
	
	int a,b,c,d;
	
	cout << *n << endl;
	return 0;
}



baavgai@dic:~/Dev/C$ g++ t.cc
t.cc: In function ‘int* getNum()’:
t.cc:6: warning: address of local variable ‘n’ returned
baavgai@dic:~/Dev/C$ ./a.out 
42
7641336



Now, the char * thing is rather odd. Technically, a string literal doesn't move, so it might be considered safe to use that way. However, it's best not to be in the habit.

Make the above n static and it compiles without complaint and returns consistent results.

Thanks for the clarification.
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Function Return an entire word

Posted 01 October 2010 - 04:30 AM

View Postsurreal_kc, on 01 October 2010 - 06:20 PM, said:

... always localize all the things i can. That including using namespce std.


You don't yet quite understand what they are telling you in that tutorial. But you will. Do it the way you are until it hurts you and then you'll understand and then you'll think about doing it differently.

Do be aware that you are using a tutorial that teaches "void main()" is valid C++ so a pinch of salt with everything they say is in order. "void main()" is wrong. Always wrong. Always use "int main()".
Read here the words of the man who invented C++:
http://www.research.....html#void-main
Can I write "void main()"?
The definition
void main() { /* ... */ }
is not and never has been C++, nor has it ever been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1.


But, as I said at the beginning of this post, this is not so much that what they are telling you is wrong as that you don't yet understand what they are telling you. But it will come, in time.

EDIT
It suddenly occurred to me you need not wait to learn this lesson.
Take the code I gave you and move the namespace line to where you think it should be (inside main()). Change nothing else. Now compile it (or at least try to).
Do you feel the pain?

This post has been edited by janotte: 01 October 2010 - 04:47 AM

Was This Post Helpful? 1
  • +
  • -

#10 surreal_kc  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 27-September 10

Re: Function Return an entire word

Posted 01 October 2010 - 03:19 PM

View Postjanotte, on 01 October 2010 - 03:30 AM, said:

View Postsurreal_kc, on 01 October 2010 - 06:20 PM, said:

... always localize all the things i can. That including using namespce std.

Do be aware that you are using a tutorial that teaches "void main()" is valid C++ so a pinch of salt with everything they say is in order. "void main()" is wrong. Always wrong. Always use "int main()".
Read here the words of the man who invented C++:
http://www.research.....html#void-main
Can I write "void main()"?
The definition
void main() { /* ... */ }
is not and never has been C++, nor has it ever been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1.


What you have shown me is blowing my mind! I LOVE reading about c++ stuff and didn't know there was documents on it like this. I am currently reading as much of it as i can when i have a free min. I am very interested in what i am reading too! I mean.. my skills in c++ are very questionable but.. this stuff pulls me in so much. Also, in your posts, i am having a difficult time understanding your mood as you type. Can you go ahead and start saying if you are pissed or not when your reply to my questions? Thanks =D lol
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Function Return an entire word

Posted 02 October 2010 - 05:10 AM

View Postsurreal_kc, on 02 October 2010 - 07:19 AM, said:

Can you go ahead and start saying if you are pissed or not when your reply to my questions? Thanks =D lol


1 - I am never "pissed" when i am writing here. It's not worth that much to me. After all you could be a very well trained labrador for all I know so what's the point? In fact you don't know I'm not 6 people taking it in turns to log-on, or a fairly capable AI, or a little green man from BJ6. Let it be that way and save your emotional energy for the 'real' people in your life. They deserve it, strangers on the web do not.

2 - Here's a ninja trick. Whenever you see something in writing always assume the person meant it in the best possible light. Even if you suspect (or even know) sarcasm was intended, act as though the post/email/whatever is straight-forward praise or comment or whatever that means what it says on its face. This has two great effects:
( a ) You don't get offended for no reason when people are, in fact, being straight-forward.
( b ) Even better, it drives angry, sarcastic, negative people out of their cotton-picking minds when others refuse to play the game they'd like to play.

Win/win and saves so much energy for better things.
Was This Post Helpful? 1
  • +
  • -

#12 surreal_kc  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 27-September 10

Re: Function Return an entire word

Posted 02 October 2010 - 12:48 PM

View Postjanotte, on 02 October 2010 - 04:10 AM, said:

View Postsurreal_kc, on 02 October 2010 - 07:19 AM, said:

Can you go ahead and start saying if you are pissed or not when your reply to my questions? Thanks =D lol

Win/win and saves so much energy for better things.


You seriously blow my mind about how good you are with words haha. Okay, thank you for your help on here and my previous posts. It is very appreciated. And a final note, omg you are such an intelligent person. Wish more people online followed your rules.
Was This Post Helpful? 0
  • +
  • -

#13 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: Function Return an entire word

Posted 03 October 2010 - 04:44 AM

View Postsurreal_kc, on 01 October 2010 - 09:20 AM, said:

View Postjanotte, on 01 October 2010 - 12:05 AM, said:

What is your namespace line doing inside main()?
Get it out of there!

View Postsurreal_kc, on 01 October 2010 - 03:49 PM, said:

int main(){
	using namespace std;
}


This is very interesting to me that you said that.
I strongly disagree with adding using namespace std; in the global scope, since it can have some nightmare consequences with regards to name clashes; it completely defeats the purpose of namespaces in the first place, which is to prevent name clashes occurring between libraries, and between your own code.

localising using namespace declarations IS a good thing; better still is to write code with fully-qualified names, e.g. std::string, std::cout, etc. Although, if you do insist on it, then having using namespace std; neatly wrapped within a function is a much, much better alternative to having it at the top of your source file (or worse, in a header file).

This post has been edited by Bench: 03 October 2010 - 04:45 AM

Was This Post Helpful? 0
  • +
  • -

#14 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Function Return an entire word

Posted 03 October 2010 - 05:28 AM

View PostBench, on 03 October 2010 - 08:44 PM, said:

I strongly disagree with adding using namespace std; in the global scope, since it can have some nightmare consequences with regards to name clashes; it completely defeats the purpose of namespaces in the first place, which is to prevent name clashes occurring between libraries, and between your own code.

localising using namespace declarations IS a good thing; better still is to write code with fully-qualified names, e.g. std::string, std::cout, etc.

Agreed completely up to this point.
namespace std is a bad thing
Far better to use std::
But the next bit...

View PostBench, on 03 October 2010 - 08:44 PM, said:

Although, if you do insist on it, then having using namespace std; neatly wrapped within a function is a much, much better alternative to having it at the top of your source file (or worse, in a header file).

Again I agree with you that namespace std is a bad thing and that it is far better to use std:: and use namespaces for localising specific namespaces.
However I've got to say that the idea of wrapping namespace std "neatly inside functions" is a bizarre idea. Any other namespace, fair enough, but namespace std?

How exactly do you then handle a case that returns a string (as above)?
Well you use std::string - don't you?
Which means you have made the great leap to abandoning namespaces std so why "neatly wrap" namespaces std in any function at all? Just for the sake of maximising inconsistency and confusion in how you are writing your code? If you are using std:: then dump namespace std and feel the benefits. But to use std:: outside functions and embed "using namespace std" inside some functions fails my test for "neat".

So, best, abandon namespace std completely and use std:: everywhere.
Second best, use namespace std globally. (That does pretty much mean you have probably lost the potential of using other namespaces effectively - Ask yourself if you care and you'll know the answer of which to choose.)

As far as I am concerned, mixing std:: and occasional local namespace std declarations isn't really in the race. You obviously disagree and we'll just have to agree to disagree.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1