Solved

Passing parameters..

Posted on 2000-03-29
10
181 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
[X]
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
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
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!

 
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 84

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

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!

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 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