• 0

More C++ Problems...


Question

Garrr, I just can't get a grasp on these really little details.

What's wrong with this code?

The error is:

lab4_3.obj : error LNK2019: unresolved external symbol "void __cdecl secs(double &,double)" (?secs@@YAXAANN@Z) referenced in function "void __cdecl time(int)" (?time@@YAXH@Z)

Debug/lab4_3.exe : fatal error LNK1120: 1 unresolved externals

#include <iostream>
using namespace std;

int milliseconds;
double seconds;
int minutes;
int hours;

void secs(double& seconds, double milliseconds);
void time(int milliseconds);

int main()

{

	//int hhmmss;
	//int time;

	cout << "Please enter the number of milliseconds you want converted." << endl;
	cin >> milliseconds;

	cout << "You entered " << milliseconds << " milliseconds." << endl;
	cout << "Converted into hh:mm:ss format, that is equivalent to:" << endl;

	time(milliseconds);

	return 0;
}



void time(int milliseconds)

{

	double ss;
	int hh;
	int mm;

	secs(ss, milliseconds);

	ss = (milliseconds / 1000);// - (hours * 60 * 60) - (minutes * 60);
	hh = (seconds / 60 / 24);
	mm = (seconds / 60) - (hours * 60);

	cout << "In time func -> " << ss << " seconds is " << milliseconds << " milliseconds." << endl;
	cout << milliseconds << endl;
	cout << hh << "::" << mm << "::" << ss << endl;
	cout << hh << "::" << mm << "::" << ss << endl;
}


void secs(double& seconds, int milliseconds)

{
	seconds = milliseconds / 1000;
	return;
}


void hrs(int& hours, double seconds)

{
	hours = seconds / 60 / 60;
}


void mins(int& minutes, double seconds)

{
	minutes = seconds / 60;
}

Basically I'm trying to convert milliseconds into hh:mm:ss format...

Link to comment
Share on other sites

18 answers to this question

Recommended Posts

  • 0

Thanks, that worked... but why would that matter?

I always thought that C++ would auto-convert double to int...

I messed around with my code some more...

#include <iostream>
using namespace std;

int milliseconds;
double seconds;
int minutes;
int hours;

void secs(double& seconds, double milliseconds);
void time(int milliseconds);

int main()

{

	//int hhmmss;
	//int time;

	cout << "Please enter the number of milliseconds you want converted." << endl;
	cin >> milliseconds;

	cout << "You entered " << milliseconds << " milliseconds." << endl;
	cout << "Converted into hh:mm:ss format, that is equivalent to:" << endl;

	time(milliseconds);

	return 0;
}



void time(int milliseconds)

{

	double ss;
	int hh;
	int mm;

	ss = (milliseconds / 1000) - (hrs * 60 * 60) - (mins * 60);
	hh = (secs / 60 / 24);
	mm = (secs / 60) - (hrs * 60);

	cout << hh << "::" << mm << "::" << ss << endl;
}


void secs(double& seconds, double milliseconds)

{
	seconds = milliseconds / 1000;
	return;
}


void hrs(int& hours, double seconds)

{
	hours = seconds / 60 / 60;
}


void mins(int& minutes, double seconds)

{
	minutes = seconds / 60;
}

Now there are a few problems with this version. Basically I want to get the hours, seconds, and minutes and put them into the hh:mm:ss mode...

I'm getting a few redefinition errors, and a "'/' : illegal, left operand has type 'void (__cdecl *)(double &,double)'" error on lines 49 and 50...

How could I fix this up?

Link to comment
Share on other sites

  • 0
I always thought that C++ would auto-convert double to int...
It does, put only when you pass the parameters. In declarations it's another matter. You can have multiple declarations of a function with different parameters in C++ so it doesn't cast, it looks for the one with those parameters.
I'm getting a few redefinition errors, and a "'/' : illegal, left operand has type 'void (__cdecl *)(double &,double)'" error on lines 49 and 50...

You're trying to divide the address of a function...You either need parameters to secs or you're using the wrong name.

hh = (secs / 60 / 24);

mm = (secs / 60) - (hrs * 60);

Link to comment
Share on other sites

  • 0

So...

void secs(double& seconds, double& milliseconds)

{
	seconds = milliseconds / 1000;
	return;
}


void hrs(int& hours, double& seconds)

{
	hours = seconds / 60 / 60;
}


void mins(int& minutes, double& seconds)

{
	minutes = seconds / 60;
}



void time(int milliseconds)

{

	double ss;
	int hh;
	int mm;

	ss = (milliseconds / 1000) - (hours * 60 * 60) - (minutes * 60);
	hh = (seconds / 60 / 24);
	mm = (seconds / 60) - (hours * 60);

	cout << hh << "::" << mm << "::" << ss << endl;
}

I tried that... basically I'm using seconds now, as it's been defined in the other functions, but now I'm not getting an output for the HH and MM sections...

Link to comment
Share on other sites

  • 0

So I now have this... compiling gives me no errors... but I know something is wrong with this code. Basically the output is giving me junk values all the time, and I'm having trouble understanding how I introduce the seconds, hours, and minutes statements into the time function.

#include <iostream>
using namespace std;

void secs(int& seconds, int& milliseconds)

{
	seconds = milliseconds / 1000;
}


void hrs(int& hours, int seconds)

{
	hours = seconds / 60 / 60;
}


void mins(int& minutes, int seconds)

{
	minutes = seconds / 60;
}



void time(int milliseconds, int hours, int seconds, int minutes)

{

	double ss;
	int hh;
	int mm;

	ss = (milliseconds / 1000) - (hours * 60 * 60) - (minutes * 60);
	hh = (seconds / 60 / 24);
	mm = (seconds / 60) - (hours * 60);

	cout << hh << "::" << mm << "::" << ss << endl;
}

int main()

{

	int milliseconds, hours, seconds, minutes;

	cout << "Please enter the number of milliseconds you want converted." << endl;
	cin >> milliseconds;

	cout << "You entered " << milliseconds << " milliseconds." << endl;
	cout << "Converted into hh:mm:ss format, that is equivalent to:" << endl;

	time(milliseconds, hours, seconds, minutes);

	return 0;
}

Link to comment
Share on other sites

  • 0

ss = (milliseconds / 1000) - (hours * 60 * 60) - (minutes * 60);
hh = (seconds / 60 / 24);
mm = (seconds / 60) - (hours * 60);

should be

ss = (milliseconds / 1000) + (milliseconds % 1000)/100.0;
hh = (seconds / 3600);
mm = (seconds % 3600 / 60);

Major mathematical errors in yours.

Link to comment
Share on other sites

  • 0

Oops... my bad on the math. Wasn't thinking too clearly there...

However, the problem persists, I'm still getting odd outputs.

For example...

Please enter the number of milliseconds you want converted.
19394531
You entered 19394531 milliseconds.
Converted into hh:mm:ss format, that is equivalent to:
-238609::-17::19399.3

There's an example of the output I get...

Link to comment
Share on other sites

  • 0

hours, minutes, and seconds are not initialized.

I don't think you even need to have them as parameters if you aren't going to pass them back to the calling function. You should be using ss in your calculations, not seconds.

Link to comment
Share on other sites

  • 0
hours, minutes, and seconds are not initialized.

I don't think you even need to have them as parameters if you aren't going to pass them back to the calling function. You should be using ss in your calculations, not seconds.

584722493[/snapback]

Could you show me an example of the fixed code?

I'm a major n00b at this, and I need things explained to me at a pretty dumbed-down level. :p

Link to comment
Share on other sites

  • 0
Could you show me an example of the fixed code?

I'm a major n00b at this, and I need things explained to me at a pretty dumbed-down level. :p

584722522[/snapback]

// function could look like your original
// void time(int milliseconds)

ss = (milliseconds / 1000) + (milliseconds % 1000)/100.0;
hh = (ss / 3600);
mm = (ss % 3600 / 60);

Link to comment
Share on other sites

  • 0

#include <iostream>
using namespace std;
void time(int milliseconds)

{

double ss;
int hh;
int mm;

ss = (milliseconds / 1000) % 60 + (milliseconds%1000)/100.0;
hh = (milliseconds / 1000 / 3600);
mm = milliseconds / 1000 % 3600 / 60;

cout << hh << "::" << mm << "::" << ss << endl;
}

int main()

{

int milliseconds, hours, seconds, minutes;

cout << "Please enter the number of milliseconds you want converted." << endl;
cin >> milliseconds;

cout << "You entered " << milliseconds << " milliseconds." << endl;
cout << "Converted into hh:mm:ss format, that is equivalent to:" << endl;

time(milliseconds);

return 0;
}

If you want to print out in that function still.

Link to comment
Share on other sites

  • 0

Thing is... I have to somehow use these functions.

I need to have a pass-by-reference for seconds, minutes, and hours...

void secs(int& seconds, int& milliseconds)

{
	seconds = milliseconds / 1000;
}


void hrs(int& hours, int seconds)

{
	hours = seconds / 60 / 60;
}


void mins(int& minutes, int seconds)

{
	minutes = seconds / 60;
}

Link to comment
Share on other sites

  • 0

OK, I changed part of my code to this... I'd like to just get this out of the way:

void secs(int& seconds, int milliseconds)

{
	seconds = milliseconds / 1000;
}


void hrs(int& hours, int seconds)

{
	hours = seconds / 60 / 60;
}


void mins(int& minutes, int seconds)

{
	minutes = seconds / 60;
}



void time(int milliseconds, int hours, int seconds, int minutes)

{

	secs(seconds, milliseconds);
	hrs(hours, seconds);
	mins(minutes, seconds);

}

The "time" function is called later in main... but the program doesn't output the time function properly...

am I doing something wrong with my pass-by-reference variables?

Link to comment
Share on other sites

  • 0

#include <iostream>
using namespace std;

void secs(int& seconds, int milliseconds)

{
	seconds = milliseconds / 1000;
}


void hrs(int& hours, int seconds)

{
	hours = seconds / 60 / 60;
}


void mins(int& minutes, int seconds)

{
	minutes = seconds / 60;
}



void time(int milliseconds, int &hours, int &seconds, int &minutes)

{

	secs(seconds, milliseconds);
	hrs(hours, seconds);
	mins(minutes, seconds);

}

int main()

{

	int milliseconds, hours, seconds, minutes;

	cout << "Please enter the number of milliseconds you want converted." << endl;
	cin >> milliseconds;

	cout << "You entered " << milliseconds << " milliseconds." << endl;
	cout << "Converted into hh:mm:ss format, that is equivalent to:" << endl;

	time(milliseconds, hours, seconds, minutes);

	return 0;
}

Same problem... the time function isn't being executed...

Link to comment
Share on other sites

  • 0
#include <iostream>
using namespace std;

void secs(int& seconds, int milliseconds)

{
	seconds = milliseconds / 1000;
}
void hrs(int& hours, int seconds)

{
	hours = seconds / 60 / 60;
}
void mins(int& minutes, int seconds)

{
	minutes = seconds / 60;
}
void time(int milliseconds, int &hours, int &seconds, int &minutes)

{

	secs(seconds, milliseconds);
	hrs(hours, seconds);
	mins(minutes, seconds);

}

int main()

{

	int milliseconds, hours, seconds, minutes;

	cout << "Please enter the number of milliseconds you want converted." << endl;
	cin >> milliseconds;

	cout << "You entered " << milliseconds << " milliseconds." << endl;
	cout << "Converted into hh:mm:ss format, that is equivalent to:" << endl;

	time(milliseconds, hours, seconds, minutes);

	return 0;
}

Same problem... the time function isn't being executed...

584723061[/snapback]

It is being executed, you aren't printing out the results.

After the call to time, put a statement that prints out the hours, minutes, and seconds.

time(milliseconds, hours, seconds, minutes);

cout << hours << ":" << minutes << ":" << seconds << endl;

Link to comment
Share on other sites

  • 0

Ok, there we go.

Now I think I just have to derive a good mathematical formula that can convert millisecs to hours, mins, and secs.

In order to avoid the problem of having something like "2 hours, 120 minutes, 120*60 seconds", do you think it would be smart to take the number of seconds, and SUBTRACT from that the number of hours and minutes converted into secs?

Then I can take the number of minutes, and subtract the number of hours converted into minutes from that...

Sounds good? You a mathematician? :p

Link to comment
Share on other sites

  • 0

AIEEE almost there!

I've nailed the hours and minutes... now I gotta get the seconds formula right.

By the way, I really appreciate you guys helping me out... <3

EDIT: By the way, quick question. How would I use the setprecision(3) function ONLY for seconds? I want seconds to go only to 3 decimal places... I know what the function does and all, I just don't know where to put it.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.