Having trouble passing bool type to function....need help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 5
Reputation: Toshkin78 is an unknown quantity at this point 
Solved Threads: 0
Toshkin78 Toshkin78 is offline Offline
Newbie Poster

Having trouble passing bool type to function....need help

 
0
  #1
Sep 16th, 2005
Im having trouble sending a bool type to a function which returns a char string to a string.

I have provided the code below

  1. /*
  2.   Name: bool2.cpp
  3.   Copyright:
  4.   Author: Toshkin
  5.   Date: 06/09/05 21:56
  6.   Description: passing string to boolean function returning boolean to
  7. */
  8.  
  9. #include <iostream>
  10. #include <cstring>
  11. using namespace std;
  12.  
  13.  
  14.  
  15. bool inout(char *string); // function prototype
  16. char outin(char *string,bool *bl); //
  17. int main()
  18. {
  19. char str1[5];
  20. bool b;
  21. cout <<"Please enter the word \"in\" or \"out\": ";
  22. cin >>str1;
  23. /*if (strcmp (str1,"in") == 0) { // redundant code....
  24.   b = true;
  25.   }
  26.   else {
  27.   b = false;
  28.   }
  29.   */
  30. b = inout(str1);
  31. cout << boolalpha << b << endl;
  32. cout <<"str1: "<<str1<<endl;
  33. outin(str1,b);
  34. cout <<"str1: "<<str1<<endl;
  35. system("PAUSE");
  36. return 0;
  37. }
  38.  
  39. bool inout(char *string) // this works
  40. {
  41.  
  42. if (strcmp(string,"in") == 0){
  43. return true;
  44. }
  45. else if (strcmp(string,"out") == 0){
  46. return false;
  47. }
  48.  
  49. } // end inout
  50.  
  51. char outin(char *string,bool *bl) // this doesn't work
  52. {
  53.  
  54. if (bl==1){
  55. string = "in";
  56. }
  57. else if (bl==0) {
  58. string = "out";
  59. }
  60. return string;
  61. }

Im using Bloodshed DevC++ 4.9.9.2
Your help will be greatly appreciated.
Cheers Toshkin!!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Having trouble passing bool type to function....need help

 
0
  #2
Sep 16th, 2005
You can't return arrays in C or C++. Nor can you copy arrays using assignment [edit]use strcpy to copy strings[/edit]. Is there some reason you aren't using std::string?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 16
Reputation: drock9975 is an unknown quantity at this point 
Solved Threads: 1
drock9975 drock9975 is offline Offline
Newbie Poster

Re: Having trouble passing bool type to function....need help

 
0
  #3
Sep 17th, 2005
Originally Posted by Toshkin78
Im having trouble sending a bool type to a function which returns a char string to a string.

I have provided the code below

  1.  
  2. char outin(char *string,bool *bl) // this doesn't work
  3. {
  4.  
  5. if (bl==1){
  6. string = "in";
  7. }
  8. else if (bl==0) {
  9. string = "out";
  10. }
  11. return string;
  12. }

Im using Bloodshed DevC++ 4.9.9.2
Your help will be greatly appreciated.
Cheers Toshkin!!!

since you are passing the string in by pointer and not by value, you can directly alter the string in the function... however you can not simply do string = whatever... you need to do a strcpy(string, whatever) or if you want, use stricpy(string, whatever, length) and then remember to put the null terminator at the end... then your function doesn't need to return anything... both strcpy and stricpy are in <cstring> and you'll need to #include that

also when comparing a bool to a value you can simply write true or false instead of 0 and 1... works better for clarity purposes... makes it easier to read

hope this helps
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Having trouble passing bool type to function....need help

 
0
  #4
Sep 18th, 2005
Originally Posted by drock9975
both strcpy and stricpy are in <cstring>
Not necessarily -- stricpy is not standard.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,648
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Having trouble passing bool type to function....need help

 
0
  #5
Sep 18th, 2005
This will work -- the function must return char* which is a pointer just like the parameter. But there is no point passing the parameter if the function is going to change and return the pointer.
  1. char* outin(char *string,bool *bl)
  2. {
  3.  
  4. if (bl==1){
  5. string = "in";
  6. }
  7. else if (bl==0) {
  8. string = "out";
  9. }
  10. return string;
  11. }

This would be more logical. And why are you passing the bool by reference instead of by value? The function isn't changing the value so you might as well pass by value.
  1. char* outin(bool bl)
  2. {
  3. char* string = 0;
  4. if (bl==1){
  5. string = "in";
  6. }
  7. else if (bl==0) {
  8. string = "out";
  9. }
  10. return string;
  11. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 5
Reputation: Toshkin78 is an unknown quantity at this point 
Solved Threads: 0
Toshkin78 Toshkin78 is offline Offline
Newbie Poster

Re: Having trouble passing bool type to function....need help

 
0
  #6
Oct 17th, 2005
Just a quick thanks for all the guys.

Cheers Toshkin!!!

PS. Sorry for the late reply.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3447 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC