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