I have this code:
#include <iostream> #include <string> using namespace std; class Event{ public: Event(int x, int y, string z){ setEvent(x, y, z); }//Constructor void setEvent(int a, int b, string c){ if(a >= 0){ if(a < b){ if(b <= 24){ start_time = a; end_time = b; event_name = c; } else cout <<"The end time for the event needs to be <=24 hours"; } else cout <<"The start time for the event needs to be smaller than the end time"; } else cout <<"The start time for the event needs to be >=0 hours"; }//Code to set an event and check if the event is valid within the precondition void rename(string r){//Code to rename event event_name = r; } string duration(){ int time_length = end_time - start_time; if(time_length == 1) return "1 hour";//I am stuck over here!!! else return time_length "hour"; } private: int start_time; int end_time; string event_name; }; If you look void duration() in the public class, I am trying to make return return text in one part and a var and text in another part. But I am unable to make it work.
main.cpp:30:16: error: could not convert 'time_length' from 'int' to 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' else return time_length "hour"; ^~~~~~~~~~~ main.cpp:30:28: error: expected ';' before string constant else return time_length "hour"; ^~~~~~
Is there a way to make return work or any alternative to fix this problem/code.
voidfunction. 2nd return something like astd::pair<int, std::string>for example.returntext?return std::to_string(time_length) + " hour";