LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-19-2022, 11:05 AM   #16
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019

Seems most of them make the same mistake that Sundial was warning about: a long is not guaranteed to be large enough on all platforms. unsigned long long should be good for 19 digits however, but as Sundial says, why limit yourself: process the number as a string.
 
1 members found this post helpful.
Old 02-06-2024, 02:30 PM   #17
lxs602
Member
 
Registered: Oct 2018
Distribution: Ubuntu 22.04
Posts: 36

Original Poster
Rep: Reputation: 1
Long overdue, but thanks.
 
Old 02-06-2024, 10:34 PM   #18
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Also: when this is not “a learning exercise,” remember that there are many contributed libraries of software … for every programming language that exists … where other(!) people have confronted these “problems that we all have,” and solved them very well. Including all(!) variations of “credit-card number validation.”

It’s very important to realize, when you are first “learning to program in [X],” that today you are not “alone.” Extremely the opposite. Therefore, when you confront any problem, your first assumption should be that someone else has already done it. (And that you can freely “sneak a peek” at their source code. In due time, you might make your own contribution …)

Last edited by sundialsvcs; 02-06-2024 at 10:37 PM.
 
Old 04-23-2024, 03:23 PM   #19
lxs602
Member
 
Registered: Oct 2018
Distribution: Ubuntu 22.04
Posts: 36

Original Poster
Rep: Reputation: 1
Thanks. I was trying not to, but probably better than becoming frustrated.
 
Old 04-23-2024, 08:55 PM   #20
murugesandins
Member
 
Registered: Apr 2024
Location: Bangalore Karnataka India
Distribution: CYGWIN_NT
Posts: 61

Rep: Reputation: 0
Quote:
Originally Posted by lxs602 View Post
I also tried using gdb, but it gave an error to do with get_long being not declared.
I faced the same issue after copy and compiling your code.c at localhost using gcc.exe (CYGWIN_NT)
Initially I was not having libcs50 library
Downloaded https://github.com/cs50/libcs50/arch...gs/v11.0.2.zip
Code:
$ unzip.exe libcs50-11.0.2.zip >/dev/null 2>&1
$ cd libcs50-11.0.2
$ make
make: Nothing to be done for 'all'.
$ /usr/bin/cp -i "Makefile" "Makefile.Original"
$# I modified Makefile related to CYGWIN
$ diff Makefile Makefile.Original
17c17
< OS := $(shell /usr/bin/uname | /usr/bin/sed "s/-[0-9]*\.[0-9]-[0-9]*//;")
---
> OS := $(shell uname)
19,24d18
< # CYGWIN_NT
< ifeq ($(OS),CYGWIN_NT)
<       LIB_BASE := $(BASENAME).so
<       LIB_MAJOR := $(BASENAME).so.$(MAJOR_VERSION)
<       LIB_VERSION := $(BASENAME).so.$(VERSION)
<       LINKER_FLAGS := -Wl,-soname,$(LIB_MAJOR)
26c20
< else ifeq ($(OS),Linux)
---
> ifeq ($(OS),Linux)
$ make
#...
install -m 644 src/cs50.h build/include
mv libcs50.so.11.0.2 libcs50.so libcs50.a build/lib
$ /usr/bin/find ./ -type f -name cs50.h
$ mv ./build/include/cs50.h /usr/include/
$ find ./ -type f -name "*.a" -exec grep -l get_long {} \;
./build/lib/libcs50.a
$ find ./ -type f -name "*.so*" -exec grep -l get_long {} \;
./build/lib/libcs50.so.11.0.2
$ mv ./build/lib/libcs50.so.11.0.2 ./build/lib/libcs50.a /usr/lib
$ /usr/bin/gcc.exe -I. -g -Wall 4175719828.c -o ./a.out -lcs50
Inside checkfunction
if( 0 == x || 0 > i )
{
break;
}
to prevent core dump issue
I have taken base version from https://github.com/jhustles/c_verify...editCardType.c
Code:
#include <cs50.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
        // Always initialize the variable
        long DebitCreditCardNum = 0;
        int sumOfProductDigits = 0;
        int sumOfNonProductDigits = 0; // sum of digits that are not products
        int checkSum = 0;
        long NumModulusTen = 0;
        long SecondModulusTen = 0;
        // Prompt user for credit card input.
        do
        {
                DebitCreditCardNum = get_long( "Number: " );
                // printf( "%li\n", DebitCreditCardNum);
        }
        while( 0 > DebitCreditCardNum );
        sumOfProductDigits = 0;
        sumOfNonProductDigits = 0;
        long ccNumbers1 = DebitCreditCardNum; // Used for checkSum
        long ccNumbers2 = DebitCreditCardNum; // Used for card validation
        while( 0 < ccNumbers1 )
        {
                NumModulusTen = ccNumbers1 % 10; // out: 4
                sumOfNonProductDigits += NumModulusTen; // add 4
                // printf( "Adding %li\n to sumOfNonProductDigits\n", NumModulusTen);
                ccNumbers1 = ccNumbers1 - NumModulusTen; // minus the last digit
                ccNumbers1 = ccNumbers1 / 10; // reduce by one last digit by dividing by 10
                // printf( "Updated ccNumbers1 after is: %li\n", ccNumbers1);
                // printf( "###########################\n" );
                // Second Number to iterate thru
                SecondModulusTen = ccNumbers1 % 10; // out: 1
                // printf( "Here's SecondModulusTen: %li\n \n", SecondModulusTen);
                // printf( "Sum Of Product Digits: %i\n \n", sumOfProductDigits);
                // printf( "Sum Of Non-Product Digits: %i\n \n", sumOfNonProductDigits);
                ccNumbers1 = ccNumbers1 - SecondModulusTen;
                ccNumbers1 /= 10;
                if( 9 < SecondModulusTen * 2 ) // if there's two digits you'll have to add each digit
                {
                        // Product result is double digits. Repeat the same thing to split numbers
                        // and reduce the ccNumbers1
                        SecondModulusTen = SecondModulusTen * 2;
                        int numSlice = SecondModulusTen % 10; // split by getting the last digit
                        sumOfProductDigits += numSlice;
                        sumOfProductDigits += ((SecondModulusTen - numSlice) / 10); // add to products sum
                }
                else
                {
                        // multiply the digit by 2 and add to sumOfProductDigits
                        SecondModulusTen = SecondModulusTen * 2;
                        sumOfProductDigits += SecondModulusTen;
                }
        }
        checkSum = sumOfProductDigits + sumOfNonProductDigits;
        // printf( "CHECK SUM: %i\n", checkSum);
        // Algorithm to check if card is valid:
        if( 0 == checkSum % 10 )
        {
                bool amex = false;
                bool mastercard = false;
                bool visa = false;
                amex =
                        (ccNumbers2 >= 340000000000000 && ccNumbers2 < 350000000000000) ||
                        (ccNumbers2 >= 370000000000000 && ccNumbers2 < 380000000000000);
                mastercard =
                        (ccNumbers2 >= 5100000000000000 && ccNumbers2 < 5600000000000000);
                visa =
                        (ccNumbers2 >= 4000000000000 && ccNumbers2 < 5000000000000) ||
                        (ccNumbers2 >= 4000000000000000 && ccNumbers2 < 5000000000000000);
                if(amex)
                {
                        printf( "AMEX\n" );
                }
                else if(mastercard)
                {
                        printf( "MASTERCARD\n" );
                }
                else if(visa)
                {
                        printf( "VISA\n" );
                }
                else
                {
                        printf( "%lu INVALID_NUMBER\n", DebitCreditCardNum );
                }
        }
        else
        {
                printf( "%lu INVALID NUMBER\n", DebitCreditCardNum );
        }
        return 0;
}
/*
01. Always initialize variables
02. Alwayse beter to write return/return0 based on the functions.
03. Replace:
        while (DebitCreditCardNum < 0);
        ...
        if
        ...
        else if
        ...
        for
With:
        while( 0 > DebitCreditCardNum );
        ...
        if
        ...
        else if
        ...
        for
Reason: new programmers may assign using:
        while( DebitCreditCardNum = 0 );
I have taken following ATM card numbers from internet images:
$ ./a.out
Number: 5333619503715702
MASTERCARD
$ a.out
Number: 5555555555554444
MASTERCARD
$ a.out
Number: 5454545454545454
MASTERCARD
$ a.out
Number: 4444333322221111
VISA
*/
$ mv verifyCreditCardType.c ValidateCardType.c
$ /usr/bin/gcc.exe -g -Wall ValidateCardType.c -o ./a.out -lcs50

Last edited by murugesandins; 04-23-2024 at 09:28 PM. Reason: Updated sample output
 
  


Reply

Tags
newbie coding



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
basic linux programming/compiling question (permissions error) Godsmacker777 Programming 11 03-17-2005 11:35 AM
Basic Linux Programming Question (QT terminal control) Boffy Programming 5 09-09-2004 12:44 PM
I'm a BASIC chap, looking for some info on BASIC programming CragStar Programming 2 01-21-2001 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration