Return x and rand()problems

I'm having some issues and I'd like to learn why!

just to clarify I'm taking a course but my endgame is to learn programming I'm doing this to learn and don't care about assignments or grades so this isn't a "do my homework" kind of thing.
The more you can teach me about what I've done wrong or could have done more efficient all the better"

problem 1:
in my function which is designed to take seconds and turn in to a HH/MM/SS format, when I try to return the values of my variables outside the function
I get: error: "hours" not declared in this scope"

which would seem like I'm not returning any values at all and I'm not sure why

(this whole code is a work in progress btw so ignore the mess)

problem 2:

I've never worked with rand() before so I could be doing some obvious faults here.

anyway I now try to have a two random numbers generated in seconds to be translated by my function as a start and an end time(a virtual race)

however I'm not sure that rand() can handle the amount of seconds in a day
and if it can I'm getting pretty much the same number all the time anyway

also startTime is always bigger than endTime eventhough endTimes scope is supposed to be bigger.

EDIT: not sure what I did but startTime and endTime no longer generates values.
So raceStart and raceEnd are unused now and the program returns nothing
but it's very late so I will leave it to you wizzards to grant me thy wisdom

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <cstdlib>
#include <ctime>


//convert seconds in to HH/MM/SS format(I'm sure you can do this way easier with //% but I don't know how
long SecToH(double sec){

    int temp;

    double hours = ((sec/60)/60);

    temp= hours;
    float minutes = (hours-temp)*60;

    temp = minutes;
    int seconds = ((minutes-temp)*60)+0.5;

    return hours; //function won't return the value of hours, seconds or minutes
    return seconds;
    return minutes;


    //std::cout<<seconds<<" / "<<int(minutes)<< " / "<<int(hours)<<std::endl;
}

int main(){

    //Random start and end values
    srand((unsigned)time(0));
    long startTime;
    long endTime;
    const int daySec=864000;
    const int secHour = 3600;

    for(int i=0;i>10;i++){

    //generate start and end time in seconds
    //EDIT: no longer generates values and don't know why
    startTime = rand()% daySec;
    endTime = rand()%startTime+secHour;

    //translate seconds to HH/MM/SS
    //EDIT: unused and don't know why
    int raceStart = SecToH(startTime);
    int raceEnd = SecToH(endTime);

    //check start time - end time
    std::cout<< startTime<< " "<<endTime<<std::endl;

    //Error: "hours" was no declared in this scope" - despite "return" in
function why?
    std::cout<< i << "\n" << hours <<" / "<< minutes << " / "<< seconds <<std::endl;

    }

  return 0;
}









1) You did not declare any variables named 'hours' or 'minutes' or 'seconds' in your main() function (nor in the global scope).

2) http://www.cplusplus.com/faq/beginners/random-numbers/

You can std::cout << RAND_MAX << "\n"; to see what your maximum random value is, but it really is fairly variable.

The FAQ is all about using rand(), but many of the principles apply to C++ as well. (Sorry, the C++ part of the FAQ is not written yet.)

In any case, I would recommend ditching rand() and using C++'s random number generators.
http://www.cplusplus.com/reference/random/

In particular, check out the uniform_int_distribution<> (http://www.cplusplus.com/reference/random/uniform_int_distribution/) to get a random integer in a specific range.

Make yourself a Mersenne twister and discard( 100000 ) or so to get it warmed-up.

Hope this helps.
Alright cheers I had hoped rand() would work as it was very simple will have a look at other options

anyway I've always been a bit confused by the scope of variables.
I know that my variables in my function is in a local scope but if I declare them in the main function then the ones in my function and the global scope would not be the same.

so when I do "return hours;" can't I then fetch those values outside the function?

asking the questions first I'm going back to read up on scope and functions right now ^^
Sorry, I didn't look very hard at your original code.

Anything you place between curly braces { } is in it's own scope, and cannot be seen outside those curly braces.

A function can only return a single thing.
If you want a function to tell you more than one thing, you must either:
- use reference arguments (references or pointers)
- return a structured type that contains those values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cmath>
#include <iomanip>
#include <iostream>

void SecToHMS( double sec, double& h, double& m, double& s )
{
  h   = (long)sec / 3600;
  sec = std::fmod( sec, 3600 );

  m   = (long)sec / 60;
  sec  = std::fmod( sec, 60 );

  s    = sec;
}

int main()
{
  double hours, minutes, seconds;

  SecToHMS( 1234567.89, hours, minutes, seconds );
  
  std::cout << std::setfill( '0' );
  std::cout << hms.hours << ":" << std::setw(2) << hms.minutes << ":" << std::fixed << std::setw(6) << std::setprecision(3) << hms.seconds << "\n";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cmath>
#include <iomanip>
#include <iostream>

struct HMS
{
  double hours, minutes, seconds;
  HMS( double h = 0, double m = 0, double s = 0 ): hours(h), minutes(m), seconds(s) { }
};

HMS SecToHMS( double sec )
{
  HMS hms;

  hms.hours = (long)sec / 3600;
  sec = std::fmod( sec, 3600 );

  hms.minutes = (long)sec / 60;
  sec = std::fmod( sec, 60 );

  hms.seconds = sec;

  return hms;
}

int main()
{
  HMS hms;  // This is a _different_ variable than the one on line 12!

  hms = SecToHMS( 1234567.89 );

  std::cout << std::setfill( '0' );
  std::cout << hms.hours << ":" << std::setw(2) << hms.minutes << ":" << std::fixed << std::setw(6) << std::setprecision(3) << hms.seconds << "\n";
}

Hope this helps.

[edit] Fixed the stupid errors I made
Last edited on
that do explain a lot!... hoped I wouldn't have to use pointers... this is supposed to be a much more basic assignment for those who's never done any programming at all before.

I'm really supposed to call it all in main and feed all input myself with cin, but I'm using this course simply for practice.
They may think using pointers to be overkill but oh well might as well learn something from it.

few more questions if you would oblige!

in your first code(since I wont be using classes), I was using modulo at first myself but I got the error:
error: invalid operands of types 'double' and 'int' to binary 'operator%'

also getting it with your code, why? they are all pointing to doubles

EDIT: references works wonders... it was my plan B but as stated they may question the complexity of what's supposed to be a very basic program
Last edited on
Alas, the problem is that I typed that in off the top of my head. % is an integer remainder operator. I should have used fmod().

I'll fix it above.
learning new things every day hehe I'll check in to what fmod() is aswell

well I asked and random generated start and end times weren't accepted all input data should be through cin

so both problems solved and case closed thanks!

rest should be fairly simple
Did you bother to read the thread?
Topic archived. No new replies allowed.