User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,065 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,281 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2055 | Replies: 5
Reply
Join Date: Sep 2005
Posts: 5
Reputation: Toshkin78 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Toshkin78 Toshkin78 is offline Offline
Newbie Poster

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

  #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

/*
  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!!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,450
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
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

  #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?
Reply With Quote  
Join Date: Jun 2005
Posts: 16
Reputation: drock9975 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
drock9975 drock9975 is offline Offline
Newbie Poster

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

  #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

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
Reply With Quote  
Join Date: Apr 2004
Posts: 3,450
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
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

  #4  
Sep 17th, 2005
Originally Posted by drock9975
both strcpy and stricpy are in <cstring>
Not necessarily -- stricpy is not standard.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,561
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

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

  #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.
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.
char* outin(bool bl)
{
    char* string = 0;
     if (bl==1){
            string = "in";
            }
     else if (bl==0) {
          string = "out";
          }
     return string;
}
Reply With Quote  
Join Date: Sep 2005
Posts: 5
Reputation: Toshkin78 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Toshkin78 Toshkin78 is offline Offline
Newbie Poster

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

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

Cheers Toshkin!!!

PS. Sorry for the late reply.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:51 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC