help converting a javascript to c++ prog

Sage

13 short of a dozen
Regular
i need a c++ prog that will perfrorm the following operations (written in java) and return the variable "chk" to the user, but I'm having troubble with converting some of it...

Code:
function isSSN(ssn1, ssn2) {

var chk = 0 ;

for (var i = 0 ; i <= 5 ; i++) { chk = chk + ((i % 8 + 2) * parseInt(ssn1.substring(i, i+1))) }
for (var i = 6 ; i <= 11 ; i++) { chk = chk + ((i % 8 + 2) * parseInt(ssn2.substring(i-6, i-5))) }
chk = 11 - (chk % 11) ;
chk = chk % 10 ;
return true ;
}
 
Considering I know absolutely nothing about what you're doing this is the best I can do. :)

Code:
double isSSN(double ssn1, double ssn2);

void main(void)
{
	double ssn1=0, ssn2=0;
	int a;

	cout << "SSN1:";
	cin >> ssn1;
	cout << "SSN2:";
	cin >> ssn2;

	a = isSSN(ssn1, ssn2);

	cout << a << endl;
}

double isSSN(double ssn1, double ssn2)
{
	int chk=0;
	int count=0;

	for (count = 0; count <= 5; count++)
	{
		chk += (count % 8 + 2) * ssn1*(count, count+1);
	}

	for (count = 6 ; count <= 11 ; count++) 
	{ 
		chk += (count % 8 + 2) * ssn1*ssn2*(count-6, count-5);
	}

	chk = 11 - (chk % 11);

	chk = chk % 10;

	return true;
}
 
original flow and semantics preserved. the second loop iterator's declaration may need to be skipped, depending on complier's ANSI compliance. type TCHAR presence assumed (i.e ms windows assumed).

Code:
#include <atlmisc.h> // WTL's CString resides here

bool isSSN(const CString &ssn1, const CString &ssn2, int &chk)
{
    chk = 0;

    for (int i = 0 ; i <= 5 ; i++)
    {
        int ssn_digit;

        if (1 == ::_stscanf((LPCTSTR) ssn1.Mid(i, 1), _T("%i"), &ssn_digit))
            chk = chk + ((i % 8 + 2) * ssn_digit);
        else
            throw _T("NaN encountered");
    }

    for (int i = 6 ; i <= 11 ; i++)
    {
        int ssn_digit;

        if (1 == ::_stscanf((LPCTSTR) ssn2.Mid(i-6, 1), _T("%i"), &ssn_digit))
            chk = chk + ((i % 8 + 2) * ssn_digit);
        else
            throw _T("NaN encountered");
    }

    chk = 11 - (chk % 11) ;
    chk = chk % 10 ;
    return true ;
}
 
Well Killer yours, with the values of 123456-123456, returns 1. what it should return is 3. I don't know what went wrong, I'm not very good at this stuff. As for the other one, I'll have to go find atlmisc.h so that I can compile it.
 
Sage said:
Well Killer yours, with the values of 123456-123456, returns 1. what it should return is 3. I don't know what went wrong, I'm not very good at this stuff. As for the other one, I'll have to go find atlmisc.h so that I can compile it.

If you have a question sheet or something it would be much easier if you posted it. :)
 
it's to check if a number, consisting of one 6 didgit part, and one seven digit part is valid. validity is determined by if the 7th digit of the second is == chk

here's the original:
Code:
function isSSN(ssn1, ssn2) {
	var chk = 0 ;
	
	if (ssn1.length == 6) {
		if (ssn2.length == 7) {
			for (var i = 0 ; i <= 5 ; i++) { chk = chk + ((i % 8 + 2) * parseInt(ssn1.substring(i, i+1))) }
			for (var i = 6 ; i <= 11 ; i++) { chk = chk + ((i % 8 + 2) * parseInt(ssn2.substring(i-6, i-5))) }
			chk = 11 - (chk % 11) ;
			chk = chk % 10 ;
			if (chk != ssn2.substring(6, 7)) {
				return false ;
			} else return true ;
		} else return false ;
	} else return false ;
}

now, I just need to be able to come up with two or three numbers that will return true...

oh and if this helps, it's also being discussed here... bring your translator
http://clubpro.spb.ru/ubb/Forum16/HTML/000270.html
 
sage, drop the CString version. use the following (with exceptions removed):

Code:
bool isSSN(const TCHAR *ssn1, const TCHAR *ssn2, int &chk) 
{ 
    bool result = true;
    chk = 0; 

    if (::_tcslen(ssn1) < 6 || ::_tcslen(ssn2) < 6)
        result = false;

    for (int i = 0 ; i <= 5 && result; i++) 
    { 
        int ssn_digit; 
        const TCHAR substr[2] = { ssn1[i], (_TCHAR) '\0' };

        if (1 == ::_stscanf(substr, _T("%i"), &ssn_digit))
            chk = chk + ((i % 8 + 2) * ssn_digit); 
        else
        {
            result = false; // NaN encountered
            break;
        }
    } 

    for (int i = 6 ; i <= 11 && result; i++) 
    { 
        int ssn_digit;
        const TCHAR substr[2] = { ssn2[i - 6], (_TCHAR) '\0' };

        if (1 == ::_stscanf(substr, _T("%i"), &ssn_digit))
            chk = chk + ((i % 8 + 2) * ssn_digit); 
        else 
        {
            result = false; // NaN encountered
            break;
        }
    } 

    chk = 11 - (chk % 11) ; 
    chk = chk % 10 ; 
    return result; 
}
 
Back
Top