Solved

Passing parameters..

Posted on 2000-03-29
10
177 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
multi threaded debug dll option in visual studio 2 95
C++ Language error 28 186
FMX enumerated colours 2 84
Microsoft C++ code failing in executable that worked 9 84
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

919 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now