Qbasicnews.com
November 12, 2019, 05:51:08 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Check for Multiple Digits  (Read 7296 times)
techdude2007
New Member

Posts: 2


« on: May 12, 2008, 09:45:17 PM »

I am trying to write a program that the user enters 4465 and the program checks to make sure that the same digits is not used twice in the number.  So in this case, the program would reject the number because there is 2 fours.  Any idea how to do this?
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #1 on: May 13, 2008, 02:35:31 AM »

This program does not allow one to enter any previously entered digit:
Code:
REM  Program omision error detected by Moneo and corrected by Ralph on June 1, 2008, as remarked on below

CLS
PRINT
PRINT " This program allows entering a number with from 1 to 10 digits, but "
PRINT " it only accepts entry of digits that have not been used before"
PRINT
PRINT " Type in your number, digit by digit.  When done, press Enter ";
DIM n(20) AS INTEGER

DO
again:
  k$ = "": WHILE k$ = "": k$ = INKEY$: WEND
  IF k$ = CHR$(13) THEN EXIT DO 'new line, from Moneo's input
  IF k$ < "0" OR k$ > "9" THEN GOTO again
  i = i + 1
  n(i) = VAL(k$)
  FOR j = 1 TO i - 1
    IF n(j) = n(i) THEN
      i = i - 1
      GOTO again
    END IF
  NEXT j
  PRINT k$;
LOOP 'modified, due to Moneo's input
 
system
« Last Edit: May 31, 2008, 04:59:08 PM by Ralph » Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
LPG
Member
*
Posts: 61

semper ubi sub ubi (always wear under wear)


« Reply #2 on: May 13, 2008, 04:22:54 AM »

You could go like this:

Code:
CLS
PRINT "ENTER A NUMBER AND I WILL TELL YOU IF THERE ARE ANY REPEATS:";
LINE INPUT A$
DIM NUMBERS(9) AS INTEGER
FOR B = 1 TO LEN(A$)
 IF NUMBERS(VAL(MID$(A$,B,1))) = 1 THEN REPEAT = 1
 IF NUMBERS(VAL(MID$(A$,B,1))) = 0 THEN NUMBERS(VAL(MID$(A$,B,1))) = 1
NEXT B
IF REPEAT = 1 THEN PRINT "THE NUMBER HAS A REPEAT"
IF REPEAT = 0 THEN PRINT "THE NUMBER DOSN'T HAVE ANY REPEATS"

SYSTEM
Logged

WHILE RPG$ <> "complete" : make up silly excuses :WEND
Moneo
Na_th_an
*****
Posts: 1971


« Reply #3 on: May 28, 2008, 10:01:46 PM »

Here's a version with more error checking and complete reporting of duplicated digits.

Regards..... Moneo

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim dup(0 to 9)
input "Enter series of numbers "; n$
if n$="" then print "Error: null entry.":system

for x=1 to len(n$)
    d$=mid$(n$,x,1)
    if d$<"0" or d$>"9" then print "Invalid digit(s) found.":system
    d=val(d$)
    dup(d)=dup(d)+1
next x

for x=0 to 9
    if dup(x)>1 then gotdups=1:print "digit";x;"repeated";dup(x);"times."
next x
if gotdups=0 then print "No duplicate digits found."
system
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #4 on: May 30, 2008, 12:37:07 PM »

LPG and Moneo:
Seems as though techdude2007 has either not received any notification on any of our three posts, or that he has his answer(s) and believes that's the end of the story.  Nothing new, as I see this happen, now and then.  But, sometimes there is a valid reason for a long delay in answering...we'll see.  Smiley
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Dav
Administrator
Ancient Guru
*****
Posts: 709



WWW
« Reply #5 on: May 30, 2008, 02:19:32 PM »

Well, here's another one ...

Quote

Entry:                               

  INPUT "Enter a number: ", num       ' Get a number entry only
  num$ = STR$(num)                      ' Convert it to a string

  FOR s = 1 TO LEN(num$) - 1          ' Step through string.
    h$ = MID$(num$, s, 1)                ' Get a nibble here...
    t$ = MID$(num$, s + 1, 1)          ' ...and a nibble there.
    IF h$ = t$ THEN                         ' If both are the same,
       PRINT "Duplicate! Re-enter!"    ' then say so ...
       GOTO Entry                            ' ...and go back.
    END IF
  NEXT

  PRINT "No duplicates found."        ' If you make it here, all OK.

Logged

Moneo
Na_th_an
*****
Posts: 1971


« Reply #6 on: May 30, 2008, 10:39:09 PM »

Dav,

I think your solution assumes that the duplicate numbers are adjacent, like 4465. But, what if they are not adjacent, like 4654?

Regards..... Moneo
Logged
Dav
Administrator
Ancient Guru
*****
Posts: 709



WWW
« Reply #7 on: May 31, 2008, 12:37:44 AM »

Oops...I read the thread too quickly and failed to fully see what was asked for. My apologies to all.   

- Dav
Logged

Ralph
Ancient Guru
****
Posts: 544


« Reply #8 on: May 31, 2008, 12:48:01 AM »

Hi, Moneo!  What's wrong with my proposed solution?
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #9 on: May 31, 2008, 04:28:58 PM »

Ralph,

The way you have it just goes into an endless loop. What happens is that the chr$(13) is caught by the test if less than "0" or greater than "9", and thus the chr$(13) is never seen later by the LOOP UNTIL.

If you put the test for chr$(13) right after the INKEY line, then it works.
You can relace the LOOP UNTIL with a plain LOOP.

Regards..... Moneo

Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #10 on: May 31, 2008, 04:52:56 PM »

Moneo, you have a very discerning eye!  Thank you.  I went back and corrected my original code.  Oh, and speaking of "original", whatever happened to the OP?
« Last Edit: May 31, 2008, 05:00:25 PM by Ralph » Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #11 on: May 31, 2008, 09:11:17 PM »

Glad to be of help, Ralph.

About the OP, who knows. He might have grabbed one of the solutions and ran.

Regards..... Moneo
Logged
LPG
Member
*
Posts: 61

semper ubi sub ubi (always wear under wear)


« Reply #12 on: June 01, 2008, 10:27:20 PM »

techdude2007 did not reply to his other post asking for a program to search inside an integer. He will have read it as he posted this topic after Ralph and I replied.
Logged

WHILE RPG$ <> "complete" : make up silly excuses :WEND
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!