Connecting Tech Pros Worldwide Help | Site Map

passing a parameter

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 05:31 PM
muser
Guest
 
Posts: n/a
Default passing a parameter

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;

  #2  
Old July 19th, 2005, 05:31 PM
Kevin Goodsell
Guest
 
Posts: n/a
Default Re: passing a parameter

muser wrote:
[color=blue]
> How can I pass the parameter " long part_num[6], into a case
> statement.[/color]

I don't understand that question. You don't pass parameters to "case
statements" (there's really no such thing as a case statement).
[color=blue]
> 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)[/color]

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.
[color=blue]
> {
>
> int weightingfactor;
> int remainder;
> int weightitem1, weightitem2, weightitem3, weightitem4, weightitem5;[/color]

Why not

int weightitem[5];

or

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

?
[color=blue]
> int product;
> int Mod11 = 11;[/color]

Is this really any better than just using the magic number 11? Use a
descriptive name or don't use a name at all.
[color=blue]
> int checkdigit;
> char partnum[6];[/color]

Prefer std::string to char arrays when dealing with strings.
[color=blue]
>
> strncpy(partnum, &record[7], 6);[/color]

Are you quite sure you can index that far into record?
[color=blue]
> partnum[6] = '\0';[/color]

This is illegal. The last valid index for partnum is 5.
[color=blue]
> part_num[6] = atol( partnum );[/color]

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).
[color=blue]
> 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);[/color]

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.
[color=blue]
>
> product = (weightitem1 + weightitem2 + weightitem3 + weightitem4 +
> weightitem5);
>
> remainder = (product % Mod11);
>
> checkdigit = (Mod11 - remainder);
> if(!part_num[6] == checkdigit)[/color]

Another bad index, probably.
[color=blue]
> return false;[/color]

Indent that so people can see it goes with the 'if'. Better yet, enclose
it in {}.
[color=blue]
>
>
> return true;
>
> }
> case 'i':
> case 'I':
> case 'r':
> case 'R':
> ProcessIRecord( prnfile, temp1 );
> CheckDigit( temp1 );
> break;[/color]

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.

  #3  
Old July 19th, 2005, 05:31 PM
White Wolf
Guest
 
Posts: n/a
Default Re: passing a parameter

muser wrote:[color=blue]
> How can I pass the parameter " long part_num[6], into a case
> statement. Case statement follows the function CheckDigit.[/color]
[SNIP]

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

--
WW aka Attila


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.