Qbasicnews.com

Full Version: Need help in making table..
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hi all, i'm new here, i need help in making a table of payment for a store... The problem's like this :
A store sells 8 items, A,B,C,D,E,F,G,H and each has its own price.
Now whenever you run the program, it'd show the list of the items and the price (no problem here), and then you'll be asked :
Code:
Enter amount of items to buy : 3
Enter 1st item : C
Enter 2nd item : B
Enter 3rd item : D
The number of the appearance of these statements depends on the first input, which is 3, so if i enter 6, it'd show :
Code:
Enter 1st item : C
Enter 2nd item : B
Enter 3rd item : D
Enter 4th item : H
Enter 5th item : A
Enter 6th item : F
How do i do this? One more thing, there can't be 2 of the same item, so if i've selected A on the first selection, i can't select it again, BUT i don't want it to redo from the start (error message) maybe it'd better if it looks like this :
Code:
Enter 1st item : A
Enter 2nd item : A
Item already chosen, please choose the other
Enter 2nd item :

And also, when the item doesn't exist, it'd show :
Code:
Enter 1st item : A
Enter 2nd item : Y
Item does not exist, please choose the available items
Enter 2nd item :

Any idea how to do this?
And lastly after the selection is done, i plan to make the table which shows the list of the item + their price and the total price, i don't have any problems in drawing tables, but i don't know how to make the row extended based on the number of items... so if i entered 3 on the first input, there'd be 4 rows (1 for name/price,etc) and if i entered 6, there'd be 7.

Thanks in advance and sorry if i ask too much...
A good way to do this is to look into arrays for the item lists,. FOR..NEXT loops for input controll, and using arrays to store which item has been bought along with IF statements to make sure it's not bought agian...

As for the table,. use the FOR...NEXT loops and arrays to controll the lay out:

Non Working Code
[syntax="qbasic"]TEXT_TO_SCREEN "Item", "Price"
LOOPFOR variable = 1 TO MAX_ITEMS
TEXT_TO_SCREEN Item(variable), Price(variable)
NEXTLOOP[/syntax]

Hope that helps....

Anonymous

hi, i'm guesing this is for school so i'll give you an answer that uses fb and pointers, so if its a QB class, you'll get failed if you copy/paste it, and if its an fb class, noone will believe you knew how to make
something like that ;p


Code:
Const num_of_items = 8
Dim As zString Ptr store_items( num_of_items - 1 ) => { _
  @"A", _
  @"B", _
  @"C", _
  @"D", _
  @"E", _
  @"F", _
  @"G", _
  @"H" }

Dim nm As String
  
  Input "Enter amount of items to buy: ", nm
  
  If Val( nm ) = 0 Then
    ? "fine, you don't want any items? then get the hell out!"
    Sleep 5000
    
  End If


Dim As Integer i, j, v, c, k
Dim As String suf, itm, meh
Dim As Integer Ptr ch
ch = CAllocate( Len( Integer ) * num_of_items )

  
  For i = 1 To Val( nm )
  
    Select Case i Mod 100
      Case 11
        suf = "th"
      Case 12
        suf = "th"
      Case 13
        suf = "th"
      Case 12
        suf = "th"
      
      Case Else
        Select Case i Mod 10
          Case 0
            suf = "th"
            
          Case 1
            suf = "st"
            
          Case 2
            suf = "nd"
            
          Case 3
            suf = "rd"
            
          Case Else
            suf = "th"
            
        End Select
        
    End Select
    
    Do
      meh = "Enter " & i & suf & " item: "
      ? meh;
      Input itm

      For j = 0 To UBound( store_items )
        If LCase( itm ) = LCase( *store_items( j ) ) Then
          For k = 0 To c - 1
            If ch[k] = j Then
              v = 0
              ? "Item already chosen."
              Goto hahah
              
            End If
          Next
          
          v = -1
          ch[c] = j
          c += 1
          
        End If
          
        
        
      Next
      
      If v = 0 Then
        ? "Item doesn't exist."
        
      End If

      hahah:
      
      
    Loop While v = 0
    v = 0
    
  Next
You could use user defined types and have each item have a flag that gets set when you buy it. That way you can check if the item was bought already...
Thanks everyone, but i still can't get it to work..
Rattrapmax6 : what do Text_for_screen and loopfor do?
chaos : const, ubound and Lcase, what do they do?
could you give me a simpler examples of what those functions do?@_@
whitetiger0990 : how do i flag an item? could you show a simple example?

This is what i got so far...

Code:
dim S as integer
dim S as integer
dim X as integer

1 print "enter amount of item to buy" : input S
if S<1 then
goto 1
elseif S>8 then
print "Too many items"
goto 1
end if

for X = 0 to S
print "Enter 1st item" input Y
next X

running the program would give you this :

Code:
enter amount of item to buy : 6
Enter 1st item :
Enter 1st item :
Enter 1st item :
Enter 1st item :
Enter 1st item :
Enter 1st item :

But, i want it to be like this :

Code:
enter amount of item to buy : 6
Enter 1st item : B
Enter 2nd item : C
Enter 3rd item : A
Enter 4th item : F
Enter 5th item : E
Enter 6th item : D

That's the first problem...
The second one is that there can't be 2 same item and if the item doen't exist it'd go back to the previous statement

Code:
Enter 1st item : B
Enter 2nd item : C
Enter 3rd item : B
Item already chosen, please choose the other
Enter 3rd item :

and

Code:
Enter 1st item : B
Enter 2nd item : C
Enter 3rd item : L
Item doesn't exist, please choose the available item
Enter 3rd item :


The third one is that i want the input in each of these statement to be Y1,Y2,Y3, and so on until the amount of item (6 in above case) instead of Y ;
so i can call them later in the table, how do i do that? Or maybe there's another way to do it?

Code:
print "╔═══╗"
for X = 0 to S
print "║Y (z) ║    
next X
print "╚══╝"

z is number of item, so using above example, it's 6 (Y1-Y6), and when run should show:

Code:
╔══╗
║ B  â•‘  
║ C  â•‘
║ A  â•‘
║ E  â•‘
║ F  â•‘
║ D  â•‘
╚══╝

I plan to call it using that method, is there any other method?
I'm a complete newb at this, so sorry if i ask too much :p
Quote:Thanks everyone, but i still can't get it to work..
Rattrapmax6 : what do Text_for_screen and loopfor do?
chaos : const, ubound and Lcase, what do they do?
could you give me a simpler examples of what those functions do?@_@
whitetiger0990 : how do i flag an item? could you show a simple example?

Text_to_screen and LoopFor do nothing,.. It's fake code....

CONST valuename = value : Sets a value to given text that can be used averywhere in the code...

UBOUND() is a function that returns the max number of slots in an array...

LCASE() is another function that will return all lower case letters from a given string

He made the example hard so it wouldn't be copy/paste-able.... Sorry if we don't trust such questions,. but we can't fully answer homework assignments....

A flag, is a on/off variable... On may = 1, off may = 0.... These help control flow with certian items in a game, or whatever else.....

:roll: :wink:
I'm not sure about what variables are what in this
Code:
print "╔═══╗"
for X = 0 to S
print "║Y (z) ║  
next X
print "╚══╝"

So here's something from scratch

Code:
dim array(6)
for i = 1 to 6
array(i) = i
next
print "-----"
for counter = 1 to 6
print "| " + str(array(counter)) +" |"
next X
print "-----"
sleep

Now I'm not sure the chr$() codes for the box but this shows the basic shape. Now this only works for 1 character values so it may need more editing for more advanced items.

Anonymous

Code:
Function get_number_suffix( n As Integer ) As String

  Dim suf As String

    Select Case n Mod 100
    
      Case 11 to 13
        suf = "th"

      Case Else

        Select Case n Mod 10

          Case 0
            suf = "th"
            
          Case 1
            suf = "st"
            
          Case 2
            suf = "nd"
            
          Case 3
            suf = "rd"
            
          Case Else
            suf = "th"
            
        End Select
        
    End Select

  get_number_suffix = suf
  
End Function


Dim example As Integer

For example = 1 To 100
  
  Locate 1

  ? example;
  ? get_number_suffix( example );
  ? " number..."

  Sleep
  
Next

nice? i had a typo in it in the original one...
Quote:whitetiger0990 : how do i flag an item? could you show a simple example?
Code:
TYPE ooatypeahhhfearme
hrm as integer
cow as string
flagthing as integer
END TYPE
dim arrayofdoom(1 to 3) AS ooatypeahhhfearme
arrayofdoom(2).cow="Moo"
arrayofdoom(3).flagthing=1
for i = 1 to 3
print "Array " & i & " flag ";
if arrayofdoom(i).flagthing<>1 then print "not ";
print "set"
next


=D
I managed to solve my problem using combination of If and goto statements, but thanks anyway :p
Pages: 1 2 3