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

  • A+
Category:Languages

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    } } 

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: