0
votes

I am attempting a simple time display program in C++.


Edited

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

class vClock
{
    public:

        // constructor
        vClock(int = 0, int = 0);

        // mutable member functions
        void set_time(int, int);
        void time_ahead(int);

        // constant function
        string time_notation(int) const;
        void show_time() const;

    private:
         int hour;
         int minute;
         int offset_hour;
         int offset_minute;
         int maxhour;
         int maxminute;
         int carrier;
};

// member function implementation 

vClock::vClock(int hr, int min)
{
    hour = hr;
    minute = min;
    maxhour = 24;
    maxminute = 60;
    carrier = 0;
}

void vClock::set_time(int hr, int min)
{
    hour = hr;
    minute = min;
}

void vClock::time_ahead(int add_minute)
{
            // suppose to be a short cut for all cases

        carrier = ((add_minute + minute) / maxminute);
        offset_hour = hour + carrier;
        offset_minute = (add_minute + minute) - (carrier * maxminute);

        cout << "After " << add_minute << "minutes, the time will be "
             << setfill('0') << setw(2) << offset_hour << ":" << setw(2) << offset_minute << time_notation(offset_hour)<< endl;

    return;
}

string vClock::time_notation(int hr) const
{
    if(hour < 12)
        cout << "AM";
    if (hour >= 12)
        cout << "PM";
}

void vClock::show_time() const{

    cout << setfill('0') 
          << setw(2) << hour << ':'
          << setw(2) << minute 
          << time_notation(hour) << endl;
    return;

}

// member functions implementation END


int main()
{
    vClock sample;

    sample.set_time(0,59);
//  sample.show_time();

    sample.time_ahead(118);

    return EXIT_SUCCESS;
}

It seems like time_notation is being evaluated before the cout statement?

*AM*After 118minutes, the time will be 02:57


solved

I cannot compile it anymore - my IDE will crash, after I added the time_notation(offset_hour) to time_ahead() and show_time() (located in the last line of the two function bodies).

Before implementing that function and use it in other functions, the compiling was all right. The program worked fine.

Is it illegal?

I received a very long error message

clock-time.cpp:65: error: no match for 'operator<<' in '(+std::operator<< [with _CharT = char, _Traits = std::char_traits](((std::basic_ostream >&)(+std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)(+(+std::operator<< [with _CharT = char, _Traits = std::char_traits](((std::basic_ostream >&)(+std::operator<< [with _CharT = char, _Traits = std::char_traits](((std::basic_ostream >&)(+std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)(+(+std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)(&std::cout)), ((const char*)"After ")))->std::basic_ostream<_CharT, _Traits>::operator<< with _CharT = char, _Traits = std::char_traits)), ((const char*)"minutes, the time will be ")))), std::setfill with _CharT = char))), std::setw(2)))->std::basic_ostream<_CharT, _Traits>::operator<< vClock%3a%3aoffset_hour">with _CharT = char, _Traits = std::char_traits)), ((const char*)":")))), std::setw(2)))->std::basic_ostream<_CharT, _Traits>::operator<< vClock%3a%3aoffset_minute">with _CharT = char, _Traits = std::char_traits << ((vClock*)this)->vClock::time_notation(((vClock*)this)->vClock::offset_hour)'

I am using MinGW C++ compiler, and my IDE is jGRASP. Any help is appreciated.

Thank you!

2

2 Answers

1
votes

That's because time_notation returns void. A void cannot be printed as it is an incomplete type.

Not sure though why IDE should crash.

Fix probably is to change time_notation to return 'string' type.

0
votes

time_notation returns void. What do you expect it to print?