Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing parameters..

Posted on 2000-03-29
10
Medium Priority
?
191 Views
Last Modified: 2010-08-05
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?
0
Comment
Question by:manjit
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 2666994
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
 

Author Comment

by:manjit
ID: 2667013
i have to do this without classes or arrays which would have made life easier
0
 
LVL 22

Expert Comment

by:nietod
ID: 2667043
>> 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:graham_k
ID: 2667294
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2667325
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
 
LVL 85

Expert Comment

by:ozo
ID: 2667382
you could pass a struct tm
0
 

Author Comment

by:manjit
ID: 2667539
Sorry guys it was just 12 parameters to be passed and no other way ..!!
thanx for all your answers
0
 
LVL 6

Expert Comment

by:graham_k
ID: 2668394
"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
 

Author Comment

by:manjit
ID: 2669929
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
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 2672641
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question