parsing time in command line arguments

hey guys,
thnks for your help last time but my professor just increased the problem by saying the time value should be passed as command line arguments
can anybdy tell the code to parse time as command line arguments

p s: i dont have any idea about that, so havnt written any code
What do you mean by "parsing time"? Are you trying to set the computer's clock? Or calculate the number of days between two dates? Or see if a file is older than the time the user supplies? Or what?
for example in the command prompt in windows
if i write UPDATE TIME 09:13:12
then this parameter should get passed to my code where i have to store it in a file.
i have no idea how some parameters are passed from command line to a code
You can use those mystical argc and argv arguments for main:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main( //Put on separate lines, so the comments are neater
   int argc, //The number of arguments passed; always at least 1
   char* argv[] //Also char** argv; contains the actual arguments as an array of C-strings
){
   std::cout << "Time to print some arguments:\n";
   for(int I(0); I < argc; ++I)
      std::cout << '\t' << *argv[I] << '\n';
   return 0;
}


You will notice that the first argument will always be the name of the program.
Last edited on
actually i know zip about command line and argc argv :(
i ll appreciate if you can give me some code on how to pass time parameters through these argc and argv
When you type in your command prompt/terminal:
MyProgram 09:13:12

Those two arguments are already passed to main(). So argc == 2, *argv[0] == "MyProgram", and *argv[1] == "09:13:12".
alrite i ll try to use it in my code
thanks:)
it is working correctly :)
but what if i have to check the time is in format HH:MM:SS
??
#include<iostream>
#include <sstream>
#include<string>
#include<cstdlib>
using namespace std;

int main( int argc, char* argv[])
{
bool isValidTime(int, int, int);
const char delim = ':';
string in;
stringstream ss;
char c1, c2;
int hr, min, sec;
if (argv[0]== " SET_TIME")
{
in= argc[1];}
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)));
}


this is the code which i have written..but is not working






















You don't need to check the value of argv[0] as that will be the name of the program you have compiled. If you are passing the time as @Daleth suggested then you will have two arguments so you should check that argc == 2 and then attempt to extract the time from the second argv i.e. argv[1] (it's zero based). Why do you need to parse the time, can't you just use it or did your professor tell you that you must validate the time as being a valid time? Try to write separate functions that just do exactly what their name implies. So if you must validate the time argv then:

 
bool is_valid_time(const char*);


Would be a suitable function template, so you then need to write the code and pass the argv[1] through to it. If it's valid then carry on with your program, otherwise if its wrong inform the user and exit(1) from your program.

HTH
Last edited on
Learn to format your code, then it's easier to comment on.
http://www.cplusplus.com/articles/z13hAqkS/

You also don't have any comments, so I moved things around so I could read it easier. I really only changed 2 things where you see the commented out lines. My program was not named set_time so I removed those lines, the main reason it did not work is you had a extra
}
above return 0;


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

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

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

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

//if (argv[0]== " SET_TIME") 
//{	
//in= argc[1];
//}

in = argv[1];
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;
	}

cout << "Time is " << hr << " " << min << " " << sec << endl;
//}
return 0;
}
Last edited on
Topic archived. No new replies allowed.