Passing parameters..

there is a data in form of time in a file (eg. 10:23:43:34 + 3:34:21:23) and the program has to read the two times and add them. if i make a function and read the times values separately
like
infile >> dd;
infile >> hh;
infile >> mm;
infile >> ss;
for the first time and
infile >> dd1;
infile >> hh1;
infile >> mm2;
etc for the second file then i'll have to pass twelve parameters to the function add(). {4 for the first time, 4 for the second time and 4 reference variable which old the sum}

is there any other was of getting around the problem with using functions?
manjitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jan LouwerensSoftware EngineerCommented:
yeah, you should create a class Time (or whatever you want to call it) that contains all pertinent info, as well as functions for adding/subtracting/etc those times... then your function(s) can just take 1 paramter (time2 - since time1 will be the 'this' object on which you call the function - ie: time1.add(time2). These functions can either return the result, or modify the original, whichever implementation you choose
0
manjitAuthor Commented:
i have to do this without classes or arrays which would have made life easier
0
nietodCommented:
>> classes or arrays which would have made life easier
You could use a strict to store the time information, like

struct
{
 int Hours;
 int Minutes;
 double Seconds;
};

(which is pretty much what was suggested, but the struct is not a "real" class but just a POD data type.  This might be allowed or it might not.  I'm not sure what your requirements are.

If it is not allowed, you could pass string that contain this information to the procedure, i.e. pass a string like "10:23:43:34", but I think that is a little messy.  I would probably pass the seperate values instead.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

graham_kCommented:
doesn't <time.h> already offer a function to add 2 times? Would you be allowed to use that ? (or is your assignment to write a function which does the same? )

If you're just worried about the number of parameters, why not read in two complete lines? You can pass these as params to addTIme() and have the fn() parse them.
0
RONSLOWCommented:
You can use global variables to hold the data, then ther are no parameters to pass.  This is ugly and bad coding style, but certainly elimiates the need for parameters to the function.

If you don't want 12 parameters, then you can get by with 8 .. have the first four as reference parameters with the initial values being the first time, and the second four being the second time.  Then you just add on to the first values.

The best idea is to put the date into a struct and pass that in by const-reference (for the two times) and reference (to return the sum) .. or have it return a struct instead.  In which case, the Add function can just become the operator+ of the struct you are using.  But you said you cannot do this (is this some sort of uni/college assignment?)

0
ozoCommented:
you could pass a struct tm
0
manjitAuthor Commented:
Sorry guys it was just 12 parameters to be passed and no other way ..!!
thanx for all your answers
0
graham_kCommented:
"no other way ..!! "  what about reading in 2 lines, passing them & cutting them up within the called fn() ??  or, indeed the other suggestions - global vars, etc?
0
manjitAuthor Commented:
Well there was no other way. This was an university low level C++ class assignement.. i have already done C++ before so i could have used classes etc,
but the instructor hasn't taught the rest of the class anything about classes or struct. And global variables as you know is bad programming. SO couldn't use that eigther !!
0
RONSLOWCommented:
8 parameters is quite fine as well.

And globals will work as well.

And even passing the lines and parsing them within the function.

If they aren't acceptable to your instructor, then that doesn't mean we shouldn't get the credit for answering your question.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.