SirCobalt
SirCobalt

Reputation: 49

How to have a function have different return type? C++

I am doing an assignment and my prof gave us this task as part of the assignment. I am still relatively new to C++ so any help. I created the function however I don't understand how he wants to return a blank string if nothing is found but return an integer if it is found. Is that possible somehow without overloading functions? Right now I just have the function set to an integer type and instead of returning a blank string it returns 0 instead.

Here is the task that is part of the assignment. If you could explain your solution so that a dumb person like me can understand it that would be much appreciated.

FindWord – this member function will take a string as a parameter and search for it within the list. If the list is empty or it cannot be found, it returns a blank string value (“”). Otherwise, it returns the int value of the index (e.g. the array element number, such as 0, 1, 2, etc.). Remember that the == equality comparison operator works on strings that are handled as string objects (rather than char data elements) and that you do not need to be case sensitive.

EDIT: Here is my code I have so far

int WordList::FindWord(string word){

if ((listSize = 0)){ // return -1 if no list exists;
    return -1;
}
else {
    for (int i =0;i<listSize;i++){ // run loop to check for word/        
        if (word == list[i]) // if word is found then return index
            return i;
     }        
        return -1; // return -1 if word is not found
   }
}

Upvotes: 4

Views: 2444

Answers (5)

CVS
CVS

Reputation: 73

I saw an answer suggesting structure to be returned. How about union with integer and string values and place it as a return type

Upvotes: 0

P.W
P.W

Reputation: 26800

This can be done with C++17 features std::variant and std::get_if.

  • The search function will return std::variant<int, std::string>
  • In main use std::get_if to determine which of the values has been returned.

std::get_if returns pointer to the value stored in the pointed-to variant or null pointer on error.

#include <variant>
#include <string>
#include <iostream>
#include <list>

 std::variant<int, std::string> strSearch(const std::string& searchStr, const std::list<std::string>& searchList)
 {
     int i = 0;
     for(const auto& s: searchList) {               
        if(!s.find(searchStr))
            return i;
        i++;    
     }
     return "";     
 }

int main()
{
    std::list<std::string> strList = {"stack", "stackover", "stackoverflow"};
    std::string str = "stackoverflow";
    std::variant<int, std::string> v{strSearch(str, strList)};
    if(std::get_if<int>(&v))
        std::cout << "String found. Index in the List is:" << std::get<int>(v) << '\n';
    else    
        std::cout << "String not found"  << '\n';

    str = "stackunderflow";
    v = strSearch(str, strList);
    if(std::get_if<int>(&v))
        std::cout << "String found. Index in the List is:" << std::get<int>(v)  << '\n';
    else    
        std::cout << "String not found."  << '\n';    

}

See it live here.

Upvotes: 4

Matthieu Brucher
Matthieu Brucher

Reputation: 22033

You need to return two objects, and you cannot do this by overloading the function.

So you can use:

std::pair<int, std::string> search(const std::string&, const std::list<std::string>& l);

Or you can return the second object by passing a reference to it:

std::string search(const std::string&, const std::list<std::string>& l, it& index);

Another option in C++17 is the proper way of doing this:

std::optional<size_t> search(const std::string&, const std::list<std::string>& l);

You get an integer if the search was successful, otherwise you don't get a value. Returning an empty string is just...

Upvotes: 1

dungmv
dungmv

Reputation: 11

you can return void*, it will return everything you want

Upvotes: -1

Prodigle
Prodigle

Reputation: 1797

This is impossible within C++, Only one return type can be defined for a function.

There are 2 possible scenarios here:

1 Return a struct containing both an integer and a string and using the one that is set

E.G

struct returnData{
int returnCode;
std::string returnText;
}

returnData myFunc(std::string){}

2 would be using your search string potentially as an in/out parameter but this makes even less sense than the assignment so I won't go into it.

There's also the possibility of returning a void* ( a generic piece of data) and then casting it to a string or int but there would be no way to know which it should be.

I really don't know what exactly your professor is expecting from this assignment since returning a different value based on a parameter is literally impossible unless you're returning them both and ignoring one.

Upvotes: 1

Related Questions