Passing parameters..

Posted on 2000-03-29
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
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?
Question by:manjit
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3

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

Author Comment

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

Expert Comment

ID: 2667043
>> classes or arrays which would have made life easier
You could use a strict to store the time information, like

 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.
Independent Software Vendors: 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!


Expert Comment

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.
LVL 10

Expert Comment

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?)

LVL 84

Expert Comment

ID: 2667382
you could pass a struct tm

Author Comment

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

Expert Comment

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?

Author Comment

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 !!
LVL 10

Accepted Solution

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.


Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 how to clear a vector as well as how to detect empty vectors in C++.

624 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