Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > passing a parameter

Reply
Thread Tools

passing a parameter

 
 
muser
Guest
Posts: n/a
 
      09-14-2003
How can I pass the parameter " long part_num[6], into a case
statement. Case statement follows the function CheckDigit.

i.e. CheckDigit( something, temp1 );

Thank you for your help in advance.


bool CheckDigit(long part_num[6], char* record)
{

int weightingfactor;
int remainder;
int weightitem1, weightitem2, weightitem3, weightitem4, weightitem5;
int product;
int Mod11 = 11;
int checkdigit;
char partnum[6];




strncpy(partnum, &record[7], 6);
partnum[6] = '\0';
part_num[6] = atol( partnum );



weightingfactor = 6;

weightitem1 = (part_num[1] * weightingfactor);

weightingfactor = 5;

weightitem2 = (part_num[2] * weightingfactor);

weightingfactor = 4;

weightitem3 = (part_num[3] * weightingfactor);

weightingfactor = 3;

weightitem4 = (part_num[4] * weightingfactor);

weightingfactor = 2;

weightitem5 = (part_num[5] * weightingfactor);




product = (weightitem1 + weightitem2 + weightitem3 + weightitem4 +
weightitem5);

remainder = (product % Mod11);

checkdigit = (Mod11 - remainder);
if(!part_num[6] == checkdigit)
return false;


return true;

} case 'i':
case 'I':
case 'r':
case 'R':
ProcessIRecord( prnfile, temp1 );
CheckDigit( temp1 );
break;
 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-14-2003
muser wrote:

> How can I pass the parameter " long part_num[6], into a case
> statement.


I don't understand that question. You don't pass parameters to "case
statements" (there's really no such thing as a case statement).

> Case statement follows the function CheckDigit.
>
> i.e. CheckDigit( something, temp1 );
>
> Thank you for your help in advance.
>
>
> bool CheckDigit(long part_num[6], char* record)


Please not that you are not, in fact, passing an array to this function.
The type of part_num is actually "long *". Therefore it may be prudent
to also pass in the length of the array pointed to by part_num. The 6 in
this case has no meaning (the compiler ignores it).

Also, prefer std::string to char* for dealing with strings.

> {
>
> int weightingfactor;
> int remainder;
> int weightitem1, weightitem2, weightitem3, weightitem4, weightitem5;


Why not

int weightitem[5];

or

std::vector<int> weightitem(5);

?

> int product;
> int Mod11 = 11;


Is this really any better than just using the magic number 11? Use a
descriptive name or don't use a name at all.

> int checkdigit;
> char partnum[6];


Prefer std::string to char arrays when dealing with strings.

>
> strncpy(partnum, &record[7], 6);


Are you quite sure you can index that far into record?

> partnum[6] = '\0';


This is illegal. The last valid index for partnum is 5.

> part_num[6] = atol( partnum );


If part_num is actually 6 elements long as you suggest, then this is
illegal. The last valid index is 5.

Also, atol is a very unsafe function. strtol would be better. using
strings and streams (istringstream for this conversion) would be better
still. Another good option would be something like boost::lexical_cast
or your own conversion function (using one of the safe methods).

> weightingfactor = 6;
> weightitem1 = (part_num[1] * weightingfactor);
> weightingfactor = 5;
> weightitem2 = (part_num[2] * weightingfactor);
> weightingfactor = 4;
> weightitem3 = (part_num[3] * weightingfactor);
> weightingfactor = 3;
> weightitem4 = (part_num[4] * weightingfactor);
> weightingfactor = 2;
> weightitem5 = (part_num[5] * weightingfactor);


OK.... Why did you not use an array or vector, then loop to do this? Or
at least eliminate weightingfactor by multiplying by the magic number
directly.

>
> product = (weightitem1 + weightitem2 + weightitem3 + weightitem4 +
> weightitem5);
>
> remainder = (product % Mod11);
>
> checkdigit = (Mod11 - remainder);
> if(!part_num[6] == checkdigit)


Another bad index, probably.

> return false;


Indent that so people can see it goes with the 'if'. Better yet, enclose
it in {}.

>
>
> return true;
>
> }
> case 'i':
> case 'I':
> case 'r':
> case 'R':
> ProcessIRecord( prnfile, temp1 );
> CheckDigit( temp1 );
> break;


I'm afraid I still don't understand what you are asking. But your
CheckDigit call here is missing a parameter.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
 
 
 
White Wolf
Guest
Posts: n/a
 
      09-14-2003
muser wrote:
> How can I pass the parameter " long part_num[6], into a case
> statement. Case statement follows the function CheckDigit.

[SNIP]

You cannot. The switch in C and C++ supports only integer values, not
arrays.

--
WW aka Attila


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
decltype as a template parameter when containing reference to anothertemplate parameter. Isti C++ 2 04-19-2010 10:01 PM
without declare parameter [double square(parameter)] return 0 in main WanHongbin@gmail.com C Programming 5 10-01-2008 03:31 AM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
Parameter List / Parameter Block / Anything patterns... mast2as@yahoo.com C++ 4 03-29-2007 09:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57