C++ Trouble Shooting

geekybear

Senior member
Oct 4, 2001
283
0
0
My program works...except the value for my output is not what it should be. I should be getting the value of 48250 when input the numbers 13 24 10. Instead, I'm gettting -85899... Would someone out there be so kind as to take a look at my code and let me know if they see what is causing this error? I've gone over it line by line and am still having no luck. Your help is much appreciated.

#include <iostream>
using namespace std;

int totalElapsedSeconds(int& , int&, int&);
void elapsedSinceMidnight(int);

int main(){


int hrs;
int mins;
int secs;
int elapsedseconds;

cout<<"Enter the time in hours, minutes, and seconds."<<endl;
cin>>hrs>>mins>>secs;

totalElapsedSeconds(hrs, mins, secs);
cout<<elapsedseconds<<" seconds have elapsed since midnight."<<endl;



return EXIT_SUCCESS;
}


int totalElapsedSeconds(int& hrs, int& mins, int& secs) {

int elapsedseconds=0;

elapsedseconds=elapsedseconds+(hrs*3600);
elapsedseconds=elapsedseconds+(mins*60);
elapsedseconds=elapsedseconds+secs;

return elapsedseconds;
}


void elapsedSinceMidnight(int elapsedseconds) {


int milhrs=0;
int milmins=0;
int milsecs=0;


milmins=elapsedseconds/60;
milsecs=elapsedseconds%60;
milhrs=milmins/60;
milmins=milmins%60;

cout<<"The time in military time is "<<milhrs<<" : "<<milmins<<" : "<<milsecs<<endl;



}
 

arcain

Senior member
Oct 9, 1999
932
0
0
When you call totalElapsedSeconds, you to not store the return value. Also your "int elapsedseconds;" in main is never assigned a value before you print it out.
 

imhotepmp

Golden Member
Mar 23, 2000
1,418
0
76
Look at 'elapsedseconds'

although you return the value, you do not set any varibale in the main funtion equal to it.

totalElapsedSeconds(hrs, mins, secs);
cout<<elapsedseconds<<" seconds have elapsed since midnight."<<endl;


this shoould be changed to

elapsedseconds=totalElapsedSeconds(hrs, mins, secs);
and then cout...


the problem here is 'scope'. Meaning that the 'elapsedseconds' in main, and the 'elapsedseconds' in the other function are two separate variables. The copy in the other function is erased, once the function returns. Your actually outputting the unassigned variable in the main function. Try this

int main:
int elapsedseconds=x;
totalElapsedSeconds(hrs, mins, secs);
then cout<<elapsedsecond<<endl;

it should output whatever x is, which tells you are indeed outputting the variable in the main function.

Hope this helps and made sense :)

Imhotep MP






 

macka

Senior member
Apr 30, 2001
262
0
0
agree with arcain.... when using reference variables... &int... you dont need to return the value.. and elapsedseconds isn't assigned anything before you print it... also... for the reference.. i dont remember.. but you might want to check the syntax.. i think it should be (int &hrs, int &mins, int &secs).. not sure though
 

arcain

Senior member
Oct 9, 1999
932
0
0
The problem I alluded to is the same imhotepmp fixed (I just don't like fixing other people's code when helping them, I prefer to let them figure it out themselves.. though that really depends on how patient I am feeling at the time, and whether or not I like the person :)).

In regards to the references, either syntax is correct ("int &a" vs "int& a"). Also in this case the only benefits of pass by reference is in performance gains, as the arguments are not modified in the function and thus do not need to be passed by reference. A better way would be to pass them as const references ("const int &a"), this way you get the performance benefits, along with the compiler enforcing the const-ness of the variables.
 

amishman

Member
Aug 1, 2001
66
0
0
Well guys I have a seperate question, but may be one reason for an error...

An int value is at max 32,767 or something like that?

Well you are trying a number too high for an int to hold in your function.

For example 10 hours * 3600 = 36,000
too high for an integer, you are prolly getting an overflow.

Am I wrong in this integer limit being this low???
If not try a double instead of an int.
 

kev0ut

Banned
Oct 9, 2001
202
0
0
Step 1) Change the following:

totalElapsedSeconds(hrs, mins, secs);
cout<<elapsedseconds<<" seconds have elapsed since midnight."<<endl;

...to:

cout<<totalElapsedSeconds(hrs, mins, secs)<<" seconds have elapsed since midnight."<<endl;

//because calling an int function RETURNS a value, it does not store it in memory

Step 2) Change all your ints to longs, int can't count that high