| | |
Having trouble passing bool type to function....need help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2005
Posts: 5
Reputation:
Solved Threads: 0
Im having trouble sending a bool type to a function which returns a char string to a string.
I have provided the code below
Im using Bloodshed DevC++ 4.9.9.2
Your help will be greatly appreciated.
Cheers Toshkin!!!
I have provided the code below
C++ Syntax (Toggle Plain Text)
/* Name: bool2.cpp Copyright: Author: Toshkin Date: 06/09/05 21:56 Description: passing string to boolean function returning boolean to */ #include <iostream> #include <cstring> using namespace std; bool inout(char *string); // function prototype char outin(char *string,bool *bl); // int main() { char str1[5]; bool b; cout <<"Please enter the word \"in\" or \"out\": "; cin >>str1; /*if (strcmp (str1,"in") == 0) { // redundant code.... b = true; } else { b = false; } */ b = inout(str1); cout << boolalpha << b << endl; cout <<"str1: "<<str1<<endl; outin(str1,b); cout <<"str1: "<<str1<<endl; system("PAUSE"); return 0; } bool inout(char *string) // this works { if (strcmp(string,"in") == 0){ return true; } else if (strcmp(string,"out") == 0){ return false; } } // end inout char outin(char *string,bool *bl) // this doesn't work { if (bl==1){ string = "in"; } else if (bl==0) { string = "out"; } return string; }
Im using Bloodshed DevC++ 4.9.9.2
Your help will be greatly appreciated.
Cheers Toshkin!!!
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
•
•
Join Date: Jun 2005
Posts: 16
Reputation:
Solved Threads: 1
•
•
•
•
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
C++ Syntax (Toggle Plain Text)
char outin(char *string,bool *bl) // this doesn't work { if (bl==1){ string = "in"; } else if (bl==0) { string = "out"; } return string; }
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
•
•
•
•
Originally Posted by drock9975
both strcpy and stricpy are in <cstring>
"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
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.
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.
C++ Syntax (Toggle Plain Text)
char* outin(char *string,bool *bl) { if (bl==1){ string = "in"; } else if (bl==0) { string = "out"; } return string; }
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.
C++ Syntax (Toggle Plain Text)
char* outin(bool bl) { char* string = 0; if (bl==1){ string = "in"; } else if (bl==0) { string = "out"; } return string; }
![]() |
Similar Threads
- Passing 2D Array of Pointers into a function (C++)
- Passing a matrix from main function to user defined function. (C++)
- Passing array of structures into a function (C++)
- Still need info on calling a function from bool! (C++)
Other Threads in the C++ Forum
- Previous Thread: .NETframework
- Next Thread: Please Help Me
Views: 3447 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






