Kindly Help on Functions

Dear gurus,

I am trying to combine two functions in one program. I have them in separate and have no idea how to combine them into one. Your help would be most appreciated!!

Here are the functions:

Function No.1


#include <iostream>
#include <string>
#include <sstream>

using namespace std;

bool isValidTime(int, int, int);
const char delim = ':';

int main(int argc, char *argv[]) {
string in;
stringstream ss;
char c1, c2;
int hr, min, sec;

cout << "Enter HH:MM:SS : " << endl;
while (true) {
cout << "> ";
getline(cin,in);
ss.clear(); ss.str(in);
if ((ss >> hr >> c1 >> min >> c2 >> sec) &&
(c1 == delim) && (c1 == c2) &&
isValidTime(hr, min, sec)) {
cout << "ok" << endl;
} else {
cout << "invalid time" << endl;
}
}
return 0;
}

bool isValidTime(int hr, int min, int sec) {
return ( ((hr >= 0) && (hr < 24)) &&
((min >= 0) && (min < 60)) &&
((sec >= 0) && (sec< 60)));
}



Function No.2

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

bool isValidVelocity(int, int);
const char delim = '/';

int main(int argc, char *argv[]) {
string in;
stringstream ss;
char c1, c2;
int km, hour;

cout << "Enter km/hour : " << endl;
while (true) {
cout << "> ";
getline(cin,in);
ss.clear(); ss.str(in);
if ((ss >> km >> c1 >> hour) &&
(c1 == delim) &&
isValidVelocity(km, hour)) {
cout << "ok" << endl;
} else {
cout << "Please re-enter and limit the maximum velocity to 100km/hr" << endl;
}
}
return 0;
}

bool isValidVelocity(int km, int hour) {
return ( (km*hour<=100 ));
}


Kindly assist. Thank you.
suggest give the second function what it expects...namely hours from the first function and km from the body of the second main.....(not velocity km/hr)
Thank you for the response sir. But I do not understand what you mean. I am sorry as I am a newbie here. Currently this program outputs as follow for Function No.1:

Enter HH:MM:SS :
>23:59:59 //this is what I input
ok //this is the output


And the Function No.2 outputs as follow:

Enter km/hour :
>50/2 //this is what I input
ok //this is the output


Above is the output when both this program are run separately. But I need both the function work on one single program as follow:

Enter HH:MM:SS :
>23:59:59 //this is what I input
ok //this is the output
Enter km/hour :
>50/2 //this is what I input
ok //this is the output



Please help as the above example. Thank you very much.


Last edited on
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
#include <iostream>
#include <string> 
#include <sstream>
using namespace std ; 


bool isValidTime(int, int, int);
bool	isValidVelocity(int, int);


int _tmain(int argc, _TCHAR* argv[])
{

	string in;
		stringstream ss;
		char c1, c2;
		int hr, min, sec;

		cout << "Enter HH:MM:SS : " << endl;
while (true) 
	{
		cout << "> ";
		getline(cin,in);
		ss.clear(); 
		ss.str(in);
		ss >> hr >> c1 >> min >> c2 >> sec;
		if ((c1 == ':') && (c1 == c2) && 	isValidTime(hr, min, sec)) 
			{
					cout << "Time ok" << endl;
			} else if (  (c1 == ':')  && (c1 == c2) && 	isValidVelocity(hr, min) )
				{
					cout << "valid speed " << endl;
				}
			else 
			{
				cout << "Invalid time or  speed " << endl;
			}
      }
	return 0;
}
bool isValidTime(int hr, int min, int sec) 
	{
		return ( (   (hr >= 0) && (hr < 24)          )           && (           (min >= 0) && (min < 60)          ) && (       (sec >= 0) && (sec< 60)      ));
}

bool isValidVelocity(int km, int hour) {
return ( (km*hour<=100 ));
}
Topic archived. No new replies allowed.