473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

char conversion problem

is string converted to its integer equivalent by minusing it by 48?
the function is suppose to check the fifth digit of struct member
using the formula contained within the function.
The function isn't doing that at the moment, hence this post and the
querie above. string must be of type char.

bool checkdigitforcustomercode( char* string )
{

int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

for(int i =0; i < 5; ++i)
check1 += (string[i] - 48) * weightingfactor;/* problem area */
weightingfactor --;
remainder = check1 % 11;

checkdigit = 11 - remainder;

for(i=4; i<=5; i++)
if(!string[i] == checkdigit)
return false;
if(checkdigit == 11){
checkdigit = 'X';
}
else
if(checkdigit = 10){
checkdigit = '0';
}

return true;
}
Jul 22 '05 #1
18 1813

"muser" <ch**********@hotmail.com> wrote in message
news:f9**************************@posting.google.c om...
is string converted to its integer equivalent by minusing it by 48?


If you want to strip numbers from the ASCII code, I'd think you would want
to do it as follows...first, check that the char is in the range 48-57, then
take the char and "bit_and" it with binary integer 1111. The result will be
the decimal numeric equivalent to the ascii number. There's prob many
"better" ways to do this. However, if you need to process a bunch of data,
some similar method is likely efficient.

I'd use something like:

#include<iostream>

using namespace std;

int num_seeker(char charin){
return ((47 < charin) && (charin < 58)) ? charin & 15 : -1;
}

int main(){
char g;

for(int i = 0; i < 256; ++i){
g = i;
cout << i <<" "<< g <<" "<< num_seeker(g) << endl;
}

return 0;
}
Jul 22 '05 #2
"muser" <ch**********@hotmail.com> wrote...
is string converted to its integer equivalent by minusing it by 48?
No. But to convert a decimal digit in most codes (all codes that
I know of) to its decimal "value" you need to subtract '0' (zero
digit).

If you need to convert a string (are you talking of std::string?)
into its integer "equivalent", use 'std::istringstream'.
the function is suppose to check the fifth digit of struct member
using the formula contained within the function.
The function isn't doing that at the moment, hence this post and the
querie above. string must be of type char.
"Isn't doing" WHAT at the moment? What "formula" should it use?
If the code doesn't do what's expected of it, how should we be able
to recommend anything?

bool checkdigitforcustomercode( char* string )
{

int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

for(int i =0; i < 5; ++i)
check1 += (string[i] - 48) * weightingfactor;/* problem area */
If it's only the _fifth_ digit you need to check, why are you subtracting
_all_ of them?

If what you're trying to do here is to convert the five leading characters
of the 'string' array into a decimal number _manually_, that's not the right
way. The right way would be

check1 = (check1 * 10) + string[i] - '0';

(and (a) don't forget to initialise 'check1' to 0, and (b) there is a strong
assumption that all 'string' elements contain _decimal_digits_).
weightingfactor --;
remainder = check1 % 11;
Why "% 11"? Shouldn't it be "% 10"? And if you just need the 5th digit,
why not just take string[4] and look at it?

Your programming exercises are beyond me, to be honest with you. Where
are you getting all that stuff?

Try to do this: if you need something _corrected_, _describe_carefully_
what you think it should do. Please try to avoid using "the formula
contained within the function" description. If you didn't do it right,
there is _no_ formula.

checkdigit = 11 - remainder;

for(i=4; i<=5; i++)
if(!string[i] == checkdigit)
return false;
if(checkdigit == 11){
checkdigit = 'X';
}
else
if(checkdigit = 10){
checkdigit = '0';
}

return true;
}

Jul 22 '05 #3
"Joe C" <jk*****@bellsouth.net> wrote...

"muser" <ch**********@hotmail.com> wrote in message
news:f9**************************@posting.google.c om...
is string converted to its integer equivalent by minusing it by 48?
If you want to strip numbers from the ASCII code, I'd think you would want
to do it as follows...first, check that the char is in the range 48-57,

then take the char and "bit_and" it with binary integer 1111. The result will be the decimal numeric equivalent to the ascii number. There's prob many
"better" ways to do this. However, if you need to process a bunch of data, some similar method is likely efficient.

I'd use something like:

#include<iostream>

using namespace std;

int num_seeker(char charin){
return ((47 < charin) && (charin < 58)) ? charin & 15 : -1;
NEVER use straight values when working with characters, it's completely
unportable. You _ought_ to just use '0' and '9' (to form the expression

('0' <= charin && charin <= '9')

Better yet, use 'std::isdigit' function from <cctype>.
}

int main(){
char g;

for(int i = 0; i < 256; ++i){
g = i;
cout << i <<" "<< g <<" "<< num_seeker(g) << endl;
}

return 0;
}

Jul 22 '05 #4
muser wrote:

is string converted to its integer equivalent by minusing it by 48?
You mean: a single character, not a string.

Yes, you can do that. But do yourself a favour and replace 48
with '0'.

int AsInt;
char AsChar = '8';

AsInt = AsChar - '0';

AsInt now contains the value 8.
the function is suppose to check the fifth digit of struct member
using the formula contained within the function.
The function isn't doing that at the moment, hence this post and the
querie above. string must be of type char.

bool checkdigitforcustomercode( char* string )
{

int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

for(int i =0; i < 5; ++i)
check1 += (string[i] - 48) * weightingfactor;/* problem area */
weightingfactor --;
That looks strange. What is it supposed to do?

If you want to convert "32078" into an integer, then
you simply can do:

sscanf( string, "%d", &check );

But note: a 5 digit string, eg. "78542", may overflow
an int on some systems, noteable those where the maximum
signed int is 32768. So you'd better check that.

If you want to do it the hard way (by hand, insted of
sscanf) it is done this way:

check1 = 0;
for( int i = 0; i < 5; ++i )
{
check1 = 10 * check1;
check1 += string[i] - '0';
}

Consider the string "29671"
Check1 starts with a value of 0. Now the for
loop begins:

i = 0
Check1 * 10 -> 0
'2' - '0' -> 2 + Check1 -> 2

i = 1
Check1 * 10 -> 20
'9' - '0' -> 9 + Check1 -> 29

i = 3
Check1 * 10 -> 290
'6' - '0' -> 6 + Check1 -> 296

i = 4
Check1 * 10 -> 2960
'7' - '0' -> 7 + Check1 -> 2967

i = 5
Check1 * 10 -> 29670
'1' - '0' -> 1 + Check1 -> 29671

Finshed: The string "29671" has been converted to its
numerical equivalent 29671
The other thing your code: for(int i =0; i < 5; ++i)
check1 += (string[i] - 48) * weightingfactor;/* problem area */
weightingfactor --;


could mean, is some sort of checksum over the digits, where
each digit has a different weight.
But then I guess it has to read:

for(int i =0; i < 5; ++i)
{
check1 += (string[i] - 48) * weightingfactor;/* problem area */
weightingfactor --;
}

Note that the decrement of the weightingfactor is now enclosed
in the loop and each digit gets a different weightingfactor.
(Without that, the whole concept of weightingfactor wouldn't make
any sense, since the weightingfactor isn't used anywhere else in
your function).

Another tip: If you think that some computation doesn't work the
way you think it should, it is always a good idea to either
* use a debugger to check the variables
* or insert some outputstatements which shed some light on what
is going on:

for( int i = 0; i < 5; ++i )
{
printf( "Check1: %d, String[%d] %c, Weight %d -> ",
check1, i, string[i], weightingfactor );

check1 += (string[i] - 48) * weightingfactor;/* problem area */

printf( "%d\n", check1 );
}

Then you can see on your monitor what is going on and will be able to
check the computation. Once you understand what is going on and what
is wrong with that: fix the computation, use the output to verify that
it now works as you expect it to work and remove those additional output
statements.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message >
NEVER use straight values when working with characters, it's completely
unportable. You _ought_ to just use '0' and '9' (to form the expression

('0' <= charin && charin <= '9')

Better yet, use 'std::isdigit' function from <cctype>.


Victor, Thanks for the kick in the tail...again ;-)

I actually misread the original post...I thought he was stripping numbers
from a serial number/barcode type thingie...where portability would be
non-important.

on my original code...if you had a large file that contained a boatload of
ascii data to sort through...you could speed up my function a bit by using:

int num_seeker(char charin){
return (charin > 57) ? -1 : ((charin < 48) ? -1 : charin & 15);
}
That way you'd get your return value after just 1 comparison for all
characters (likely to be the most frequent) performing the second comparison
only if needed.

Victor...I respect your thoughts a lot. Would you ever do something like
this, or is it simply offensive to your programming sensibilities?

Jul 22 '05 #6
Joe C wrote:

"Victor Bazarov" <v.********@comAcast.net> wrote in message >
NEVER use straight values when working with characters, it's completely
unportable. You _ought_ to just use '0' and '9' (to form the expression

('0' <= charin && charin <= '9')

Better yet, use 'std::isdigit' function from <cctype>.


Victor, Thanks for the kick in the tail...again ;-)

I actually misread the original post...I thought he was stripping numbers
from a serial number/barcode type thingie...where portability would be
non-important.

on my original code...if you had a large file that contained a boatload of
ascii data to sort through...you could speed up my function a bit by using:

int num_seeker(char charin){
return (charin > 57) ? -1 : ((charin < 48) ? -1 : charin & 15);
}
That way you'd get your return value after just 1 comparison for all
characters (likely to be the most frequent) performing the second comparison
only if needed.

Victor...I respect your thoughts a lot. Would you ever do something like
this, or is it simply offensive to your programming sensibilities?

Can't talk for Victor.
But even then I would do:

return (charin > '9') ? -1 : ((charin < '0') ? -1 : charin - '0');

Much simpler to not have to remember the code values for the digits.
(On most CPU's I think its reasonable to assume that a subtraction
runs in the same time as an and masking).

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #7
"Karl Heinz Buchegger" <kb******@gascad.at> wrote...
Joe C wrote:

"Victor Bazarov" <v.********@comAcast.net> wrote in message >
NEVER use straight values when working with characters, it's completely unportable. You _ought_ to just use '0' and '9' (to form the expression
('0' <= charin && charin <= '9')

Better yet, use 'std::isdigit' function from <cctype>.


Victor, Thanks for the kick in the tail...again ;-)

I actually misread the original post...I thought he was stripping numbers from a serial number/barcode type thingie...where portability would be
non-important.

on my original code...if you had a large file that contained a boatload of ascii data to sort through...you could speed up my function a bit by using:
int num_seeker(char charin){
return (charin > 57) ? -1 : ((charin < 48) ? -1 : charin & 15);
}
That way you'd get your return value after just 1 comparison for all
characters (likely to be the most frequent) performing the second comparison only if needed.

Victor...I respect your thoughts a lot. Would you ever do something like this, or is it simply offensive to your programming sensibilities?

Can't talk for Victor.
But even then I would do:

return (charin > '9') ? -1 : ((charin < '0') ? -1 : charin - '0');

Much simpler to not have to remember the code values for the digits.
(On most CPU's I think its reasonable to assume that a subtraction
runs in the same time as an and masking).


Karl, you read my mind. Couple more of these and I'll let you talk
for me. :-)))

Victor
Jul 22 '05 #8
On Wed, 17 Dec 2003 20:26:06 +0100, Karl Heinz Buchegger
<kb******@gascad.at> wrote:

[snip]
Can't talk for Victor.
But even then I would do:

return (charin > '9') ? -1 : ((charin < '0') ? -1 : charin - '0');

Much simpler to not have to remember the code values for the digits.
(On most CPU's I think its reasonable to assume that a subtraction
runs in the same time as an and masking).


It also has the advantage that it does not depend on the
character set being ASCII.

Sincerely,

Gene Wirchenko

Jul 22 '05 #9
In article <f9**************************@posting.google.com >,
ch**********@hotmail.com says...
is string converted to its integer equivalent by minusing it by 48?


No. A single character might be, depending on the character set in use,
but a character is not a string.

If I'm reading your code correctly, I'd use something like this:

for (i=0; i<5;i++)
if ( isdigit(string[i]))
check1 += (string[i]-'0') * weightingfactor--;

The part where you have 'if (checkdigit = 10)' is almost certainly an
error -- you're doing an assignment instead of a comparison. In any
case, I'd probably cheat, and make this table-driven:

char digits[] = "01234567890X";

// loop from above.

checkdigit = digits[11 - (check1 % 11)];

I'm not quite sure I follow your 'if (!string[i] == checkdigit)'.
Perhaps you wanted: 'if (string[i] != checkdigit)'? comparison ('==')
has lower precedence than logical negation ('!'), so as-is, you're doing
logical negation on 'string[i]', and then comparing the result of that
to checkdigit. Logical negation is _normally_ done on a logical value,
and it looks like string[i] will be a character rather than boolean.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #10
Jerry Coffin <jc*****@taeus.com> wrote in message news:<MP************************@news.clspco.adelp hia.net>...
In article <f9**************************@posting.google.com >,
ch**********@hotmail.com says...
is string converted to its integer equivalent by minusing it by 48?


No. A single character might be, depending on the character set in use,
but a character is not a string.

If I'm reading your code correctly, I'd use something like this:

for (i=0; i<5;i++)
if ( isdigit(string[i]))
check1 += (string[i]-'0') * weightingfactor--;

The part where you have 'if (checkdigit = 10)' is almost certainly an
error -- you're doing an assignment instead of a comparison. In any
case, I'd probably cheat, and make this table-driven:

char digits[] = "01234567890X";

// loop from above.

checkdigit = digits[11 - (check1 % 11)];

I'm not quite sure I follow your 'if (!string[i] == checkdigit)'.
Perhaps you wanted: 'if (string[i] != checkdigit)'? comparison ('==')
has lower precedence than logical negation ('!'), so as-is, you're doing
logical negation on 'string[i]', and then comparing the result of that
to checkdigit. Logical negation is _normally_ done on a logical value,
and it looks like string[i] will be a character rather than boolean.


Thank you Victor and Karl for responding to my previous post. Jeff
you've actually spotted the problem with comparing if(!string[i] ==
checkdigit), as i actually just came online to ask why the
checkdigitforcustomercode wasn't working (citing that exactly line of
code as a contentious area).
Karl and Victor I did use your advice and have remedied most of the
problem using quick watch and other debugging tools.
To get one of the two 'big boys' of these newsgroups to respond to a
post is a quite a feat, to get both is unprecendented, and I thank you
one and both for your advice.
Jul 22 '05 #11
> >But even then I would do:

return (charin > '9') ? -1 : ((charin < '0') ? -1 : charin - '0');

Much simpler to not have to remember the code values for the digits.
(On most CPU's I think its reasonable to assume that a subtraction
runs in the same time as an and masking).


It also has the advantage that it does not depend on the
character set being ASCII.


Actually it does; there's no reason that 0-9 have to be contiguous in
the character set. (Hence the reason for using the <cctype> functions,
if you are a portability freak).
Jul 22 '05 #12
In article <84**************************@posting.google.com >,
ol*****@inspire.net.nz says...

[ ... ]
Actually it does; there's no reason that 0-9 have to be contiguous in
the character set.


The C++ standard requires that 0-9 be contiguous in the character set.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #13

"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:MP************************@news.clspco.adelph ia.net...
In article <84**************************@posting.google.com >,
ol*****@inspire.net.nz says...

[ ... ]
Actually it does; there's no reason that 0-9 have to be contiguous in
the character set.


The C++ standard requires that 0-9 be contiguous in the character set.

Yes, but it doesn't hold true for the letters. Nobody here probably knows
what "Junior is 11" means.
Jul 22 '05 #14
I've put this code through the debugger a couple of times and still
I'm unable to find the problem.
The checkdigit is correct, the remainder variable is correct. But when
I check the char string variable, I get a memory address. The function
works all except when I get to the definitive line if(string1[4] !=
checkdigit) return false;
bool checkdigitforcustomercode( char* string )
{

int checkdigit;
int remainder;
int product;
int string1[5];
string1[0] = string[0] - 0;
string1[1] = string[1] - 0;
string1[2] = string[2] - 0;
string1[3] = string[3] - 0;
string1[4] = string[4] - 0;
product = (string[0] * 5) + (string[1] * 4) + (string[2] * 3) +
(string[3] * 2);

remainder = (product % 11);

checkdigit = (11 - remainder);
if(string1[4] != checkdigit){}
return false;

return true;
}

I have tried putting the zeroes in single quotation marks e.g. '0'.
And putting brackets around the equation string1[0] = (string[0] - 0);
the string contains numbers 24686, the last digit is a six, and the
checkdigit is a six, so the function is suppose to return true, but
doesn't. It returns false.

if(!checkdigitforcustomercode( rec.Newcrecord.customercode )){
prnfile<< "Invalid: Incorrect check digit for c record customer
code\n";
prnfile<< record << endl;
return false;
}

rec is the object of a union, and Newcrecord is the object of a
struct.

Jerry Coffin <jc*****@taeus.com> wrote in message news:<MP************************@news.clspco.adelp hia.net>...
In article <f9**************************@posting.google.com >,
ch**********@hotmail.com says...
is string converted to its integer equivalent by minusing it by 48?


No. A single character might be, depending on the character set in use,
but a character is not a string.

If I'm reading your code correctly, I'd use something like this:

for (i=0; i<5;i++)
if ( isdigit(string[i]))
check1 += (string[i]-'0') * weightingfactor--;

The part where you have 'if (checkdigit = 10)' is almost certainly an
error -- you're doing an assignment instead of a comparison. In any
case, I'd probably cheat, and make this table-driven:

char digits[] = "01234567890X";

// loop from above.

checkdigit = digits[11 - (check1 % 11)];

I'm not quite sure I follow your 'if (!string[i] == checkdigit)'.
Perhaps you wanted: 'if (string[i] != checkdigit)'? comparison ('==')
has lower precedence than logical negation ('!'), so as-is, you're doing
logical negation on 'string[i]', and then comparing the result of that
to checkdigit. Logical negation is _normally_ done on a logical value,
and it looks like string[i] will be a character rather than boolean.

Jul 22 '05 #15
"muser" <ch**********@hotmail.com> wrote...
[...]
string1[0] = string[0] - 0;
string1[1] = string[1] - 0;
string1[2] = string[2] - 0;
string1[3] = string[3] - 0;
string1[4] = string[4] - 0;

Subtracting 0 from anything usually doesn't do anything. Do you
really think it does? What do you think you're accomplishing here?


product = (string[0] * 5) + (string[1] * 4) + (string[2] * 3) +
(string[3] * 2);
Why are you multiplying by 5, 4, 3, and 2 here? What happened to
string[4]? How come it's not participating?

remainder = (product % 11);
What is that for?

checkdigit = (11 - remainder);
What is that supposed to do?


if(string1[4] != checkdigit){}
What are the curly braces after the 'if' expression?
return false;

return true;
}

I have tried putting the zeroes in single quotation marks e.g. '0'.
And putting brackets around the equation string1[0] = (string[0] - 0);
the string contains numbers 24686, the last digit is a six, and the
checkdigit is a six, so the function is suppose to return true, but
doesn't. It returns false.


Wow... You _have_ tried everything. I don't even know what to suggest.

To be honest with you, I see NO HOPE for you. You should stop wasting
your time. Go paint, or play guitar, or ... In any case, programming is
simply not your thing. Once you realise that, you'll be free.
Jul 22 '05 #16
muser wrote:
I've put this code through the debugger a couple of times and still
I'm unable to find the problem.
The checkdigit is correct, the remainder variable is correct. But when
I check the char string variable, I get a memory address. The function
works all except when I get to the definitive line if(string1[4] !=
checkdigit) return false;
bool checkdigitforcustomercode( char* string )
{

int checkdigit;
int remainder;
int product;
int string1[5];
string1[0] = string[0] - 0;
string1[1] = string[1] - 0;
string1[2] = string[2] - 0;
string1[3] = string[3] - 0;
string1[4] = string[4] - 0; You need to put the zeros in single quotation marks.
product = (string[0] * 5) + (string[1] * 4) + (string[2] * 3) +
(string[3] * 2);

remainder = (product % 11);

checkdigit = (11 - remainder);
if(string1[4] != checkdigit){}
return false; With the above if-statement you check if string1[4] != checkdigit. Then
follow {}. That means that if the expression in your if-statemant
returns true *nothing happens*. The next line is *not* part of the
if-statement. It is *always* executed.
return true;
} This return-statement is unreachable becauseof the non-conditional
"return false;" above. The solution is trivial. Set the curly braces at
the right position:

if(string1[4] != checkdigit){
return false;
}
return true;

You should learn to understand what you actually write. You should be
able to find such an error on yourself by going through your program
line by line and thinking about what it will actually do.
I have tried putting the zeroes in single quotation marks e.g. '0'.
And putting brackets around the equation string1[0] = (string[0] - 0);
the string contains numbers 24686, the last digit is a six, and the
checkdigit is a six, so the function is suppose to return true, but
doesn't. It returns false.

if(!checkdigitforcustomercode( rec.Newcrecord.customercode )){
prnfile<< "Invalid: Incorrect check digit for c record customer
code\n";
prnfile<< record << endl;
return false;
}

rec is the object of a union, and Newcrecord is the object of a
struct.


Jul 22 '05 #17
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<ghnFb.172568$_M.779785@attbi_s54>...
"muser" <ch**********@hotmail.com> wrote...
[...]
string1[0] = string[0] - 0;
string1[1] = string[1] - 0;
string1[2] = string[2] - 0;
string1[3] = string[3] - 0;
string1[4] = string[4] - 0;

Subtracting 0 from anything usually doesn't do anything. Do you
really think it does? What do you think you're accomplishing here?


product = (string[0] * 5) + (string[1] * 4) + (string[2] * 3) +
(string[3] * 2);


Why are you multiplying by 5, 4, 3, and 2 here? What happened to
string[4]? How come it's not participating?

remainder = (product % 11);


What is that for?

checkdigit = (11 - remainder);


What is that supposed to do?


if(string1[4] != checkdigit){}


What are the curly braces after the 'if' expression?
return false;

return true;
}

I have tried putting the zeroes in single quotation marks e.g. '0'.
And putting brackets around the equation string1[0] = (string[0] - 0);
the string contains numbers 24686, the last digit is a six, and the
checkdigit is a six, so the function is suppose to return true, but
doesn't. It returns false.


Wow... You _have_ tried everything. I don't even know what to suggest.

To be honest with you, I see NO HOPE for you. You should stop wasting
your time. Go paint, or play guitar, or ... In any case, programming is
simply not your thing. Once you realise that, you'll be free.


I had been thinking the same thing victor, (that is to give up and
quit) but now that I've put in so many hours and come this far, I
can't let go like the proverbial dog who has got hold of a bone and
won't let go.
You see if I get this part of the program to work, I will have
finished this part of the course and could in all fairness give up.
What frustrates me is that the code should work and doesn't.

My assignments is this, if you had a load of customers, but wanted to
identify them by code alone you might use a customer code.
(rec.Newcrecord.customercode)These codes are held on a file along with
other customer details from different departments. Some of the
customer codes held on file are wrong, so there is a formula for
checking whether a customer code is right or not.
You * the customer code by a weighting factor (in this instance 5432)
the last customer code digit is a 6, but for the purposes of the
formula it isn't used when validating the customer code.

say customer code is 2468
****
and the weight 5432

the product of this would be 10 + 16 + 18 + 16 = 60;
so the product actually equals 60.
This is then divided by 11 (because all the valid customercodes will
have a modulus 11 checkdigit)
remainder = product % 11;
This produces 5;
subtracting 11 (our mod number) with the remainder gives us 6.
6 being our check digit.
if(string[4] != checkdigit){
return false;
}
or if(string[4] != checkdigit)
return false;
I should have clarified what it was I was trying to achieve.
For all intent purposes when I debug the function
CheckDigitForCustomerCode( char* string )

the product variable contains 60;
the remainder variable contains 5;
the checkdigit variable contains 6;

But the thing is, is that when I go to execute the program my valid
customer code of 24686, comes up as invalid.
I would like to know what could possibly cause this to happen.
string1 is of type integer. string is of type char.
If this or my post is frustrating any members of the newsgroup please
feel reassured that it isn't frustrating you half as much as it is me.
Jul 22 '05 #18
ch**********@hotmail.com (muser) wrote in message news:<f9**************************@posting.google. com>...
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<ghnFb.172568$_M.779785@attbi_s54>...
"muser" <ch**********@hotmail.com> wrote...
[...]
string1[0] = string[0] - 0;
string1[1] = string[1] - 0;
string1[2] = string[2] - 0;
string1[3] = string[3] - 0;
string1[4] = string[4] - 0;

Subtracting 0 from anything usually doesn't do anything. Do you
really think it does? What do you think you're accomplishing here?


product = (string[0] * 5) + (string[1] * 4) + (string[2] * 3) +
(string[3] * 2);


Why are you multiplying by 5, 4, 3, and 2 here? What happened to
string[4]? How come it's not participating?

remainder = (product % 11);


What is that for?

checkdigit = (11 - remainder);


What is that supposed to do?


if(string1[4] != checkdigit){}


What are the curly braces after the 'if' expression?
return false;

return true;
}

I have tried putting the zeroes in single quotation marks e.g. '0'.
And putting brackets around the equation string1[0] = (string[0] - 0);
the string contains numbers 24686, the last digit is a six, and the
checkdigit is a six, so the function is suppose to return true, but
doesn't. It returns false.


Wow... You _have_ tried everything. I don't even know what to suggest.

To be honest with you, I see NO HOPE for you. You should stop wasting
your time. Go paint, or play guitar, or ... In any case, programming is
simply not your thing. Once you realise that, you'll be free.


I had been thinking the same thing victor, (that is to give up and
quit) but now that I've put in so many hours and come this far, I
can't let go like the proverbial dog who has got hold of a bone and
won't let go.
You see if I get this part of the program to work, I will have
finished this part of the course and could in all fairness give up.
What frustrates me is that the code should work and doesn't.

My assignments is this, if you had a load of customers, but wanted to
identify them by code alone you might use a customer code.
(rec.Newcrecord.customercode)These codes are held on a file along with
other customer details from different departments. Some of the
customer codes held on file are wrong, so there is a formula for
checking whether a customer code is right or not.
You * the customer code by a weighting factor (in this instance 5432)
the last customer code digit is a 6, but for the purposes of the
formula it isn't used when validating the customer code.

say customer code is 2468
****
and the weight 5432

the product of this would be 10 + 16 + 18 + 16 = 60;
so the product actually equals 60.
This is then divided by 11 (because all the valid customercodes will
have a modulus 11 checkdigit)
remainder = product % 11;
This produces 5;
subtracting 11 (our mod number) with the remainder gives us 6.
6 being our check digit.
if(string[4] != checkdigit){
return false;
}
or if(string[4] != checkdigit)
return false;
I should have clarified what it was I was trying to achieve.
For all intent purposes when I debug the function
CheckDigitForCustomerCode( char* string )

the product variable contains 60;
the remainder variable contains 5;
the checkdigit variable contains 6;

But the thing is, is that when I go to execute the program my valid
customer code of 24686, comes up as invalid.
I would like to know what could possibly cause this to happen.
string1 is of type integer. string is of type char.
If this or my post is frustrating any members of the newsgroup please
feel reassured that it isn't frustrating you half as much as it is me.


p.s. why I use an empty control statement because my compiler is
logging this as an error or warning when I compile the program without
it, hence the need for an empty control statement.
Jul 22 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
5
by: Anton Pervukhin | last post by:
Hello! Imagine the situation you have a class that interprets the command line of the program you are executing. This class is written by third party and has a main function parse with arguments...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
10
by: kevin.hall | last post by:
GCC 3.3 and MSVS 6.0 have no problem converting char* to const char* (not even a warning), but MS's WinCE compiler generated an error complained that this was not possible. MS's WinCE compiler did...
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
10
by: yaniv.dg | last post by:
hi all, i'm bumping into smething very starnge its very simple code but from some reason the program is acting strange this is my code: #include<stdio.h> #include<conio.h> void main(void) {...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
10
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is...
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
9
by: Eric | last post by:
I am working on a large, old code base and attempting to move it to GCC 4.2. Throughout the code, there is stuff like: char *aVar = "aString"; or void aFunc( char *aVar) { ... } aFunc(...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.