-1

We just started working on functions in class this week. For this assignment we had to write some function that won't accept 0 or anything above 24 hours. While I think I have that part working, I'm running into issues when I try to use said function in main.

Below is my code so far:

#include<iostream>

using namespace std;

int hr = 0;
int mn = 0;
int sec = 0;
int totalSec = 0;

time(int hr, int mn, int sec, int totalSec)
{
    if (cin.fail() == 0)
    {
        cout << "you cant have zeros" << endl;
    }

    else if (hr > 24)
    {
        cout << "You cant go over 24 hours"<< endl;
    }

    else
    {
        // hours * minutes * seconds then add minutes * seconds 
        // added to the seconds for totalSec
        totalSec = (hr * 60) * 60 + (mn * 60) + sec;
        return totalSec;
    }
}

int main()
{
    cout << "What time is it"<<endl;
    cin >> hr;
    cin >> mn; 
    cin >> sec;
    cout << hr << " Hours," << mn << " Minutes," << sec << " Seconds past midnight!"<< endl;
    totalSec = time(int totalSec);
    cout << " or " << totalSec << " seconds till midnight." << endl;
}
Isaac Corbrey
  • 553
  • 3
  • 20
Gus Robins
  • 31
  • 7
  • 4
    remove `int` in `totalSec = time(int totalSec);` – Eugene Feb 05 '21 at 20:10
  • 1
    My advice is to remove all of the global variables and make them local variables in `int main()` – drescherjm Feb 05 '21 at 20:13
  • 2
    Please [don't do `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). If you fix the *first* problem then you would call [`std::time`](https://en.cppreference.com/w/cpp/chrono/c/time). – Some programmer dude Feb 05 '21 at 20:13
  • 3
    There's also a lot of other problems with the shown code, which makes it looks like you're just guessing around a bit and hoping that things work. Get [a few decent books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or take a couple of classes, and learn properly. – Some programmer dude Feb 05 '21 at 20:15
  • thanks for the input I found the problem I was having just after I posted. I was in fact reduced to guessing what my issue was this is the 4th week of my 1st C++ class so I have no idea what's going on thus the questions and "other problems". thanks for the comments. – Gus Robins Feb 05 '21 at 20:24
  • `time(int hr, int mn, int sec, int totalSec){` even if your compiler allows you to declare a function without a return type you should still give it one so `time(int hr, int mn, int sec, int totalSec){` should become `int time(int hr, int mn, int sec, int totalSec){` – drescherjm Feb 05 '21 at 20:47
  • Your `return totalSec;` is inside your else {} block remember that if your function is declared to return a value you must return a value for all code paths. It is undefined behavior to break your promise to the compiler. – drescherjm Feb 05 '21 at 20:48

2 Answers2

1

It would be useful if you post the error message you are getting.

Anyway, looks like your time function does not have the return type. Since you are returning an int, it should be:

int time(int hr, int mn, int sec, int totalSec){
    // Content of the function...
}

Further, I would recommend adding a return clause (or moving the existing one) at the end of the file, after the else clause. It is a good practice.

0

Remove the int totalSec argument from the time() function, and in main() the totalSec = time(int totalSec); needs to be totalSec = time(hr,mn,sec);. These changes produce the output I needed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Gus Robins
  • 31
  • 7