Author Topic: Help with program  (Read 4063 times)

0 Members and 1 Guest are viewing this topic.

Offline Aeveus

  • Newbie
  • Posts: 5
    • View Profile
Help with program
« on: April 05, 2020, 03:09:08 am »
Hello, I'm having trouble with this program. When I run it it seems like it's looping the menu over and over or something and it won't let me do inputs, and I'm not sure why (or even if that's the case). Any help is appreciated! :)

Code: QB64: [Select]
  1. '******************************* Program Mainline **************************
  2.     GOSUB DisplayMainMenu
  3.     SELECT CASE Choice$
  4.         CASE "1"
  5.             GOSUB UStoPounds
  6.             CASE "2"
  7.             GOSUB PoundstoUS
  8.             CASE "3"
  9.             GOSUB UStoCanadian
  10.             CASE "4"
  11.             GOSUB CanadiantoUS
  12.     END SELECT
  13. LOOP UNTIL Choice$ = "5"
  14. '******************************* Display Main Menu ***********************
  15. DisplayMainMenu:
  16. GOSUB PrintMainTitle
  17. LOCATE 5, 26
  18. PRINT "CURRENCY CONVERTER"
  19. LOCATE 8, 18
  20. PRINT "1. CONVERT US DOLLARS TO POUNDS"
  21. LOCATE 10, 18
  22. PRINT "2. CONVERT POUNDS TO US DOLLARS"
  23. LOCATE 12, 18
  24. PRINT "3. CONVERT US DOLLARS TO CANADIAN DOLLARS"
  25. LOCATE 14, 18
  26. PRINT "4. CONVERT CANADIAN DOLLARS TO US DOLLARS"
  27. LOCATE 16, 18
  28. PRINT "5. END"
  29. LOCATE 19, 16
  30. PRINT "ENTER SELECTION:";
  31. '*************************Print Main Title *******************************
  32. PrintMainTitle:
  33. LOCATE 3, 12
  34. PRINT " C O N V E R T  C U R R E N C Y "
  35. '**************************UstoPounds************************************
  36. UStoPounds:
  37. PRINT "ENTER AMOUNT:"; Amount
  38. ConvertedAmount = AMOUNT * .81
  39. PRINT ConvertedAmount
  40. '****************************PoundstoUS***************************
  41. PoundstoUS:
  42. PRINT "ENTER AMOUNT:"; Amount
  43. ConvertedAmount = AMOUNT * 1.23
  44. PRINT ConvertedAmount
  45. '**************************UStoCanadian****************************
  46. UStoCanadian:
  47. PRINT "ENTER AMOUNT:"; Amount
  48. ConvertedAmount = AMOUNT * 1.42
  49. PRINT ConvertedAmount
  50. '*******************************CanadiantoUS************************
  51. CanadiantoUS:
  52. PRINT "ENTER AMOUNT:"; Amount
  53. ConvertedAmount = AMOUNT * .7
  54. PRINT ConvertedAmount
  55.  

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Help with program
« Reply #1 on: April 05, 2020, 03:26:03 am »
This may work out better for you.

Code: QB64: [Select]
  1. '******************************* Program Mainline **************************
  2.  
  3.     GOSUB DisplayMainMenu
  4.     DO
  5.         _LIMIT 30
  6.         choice$ = INKEY$
  7.     LOOP UNTIL VAL(choice$) > 0 AND VAL(choice$) < 6
  8.     ON VAL(choice$) GOSUB UStoPounds, PoundstoUS, UStoCanadian, CanadiantoUS
  9.     IF VAL(choice$) = 5 THEN END
  10.     PRINT "press a key to return to menu"
  11.     SLEEP
  12. '******************************* Display Main Menu ***********************
  13. DisplayMainMenu:
  14. GOSUB PrintMainTitle
  15. LOCATE 5, 26
  16. PRINT "CURRENCY CONVERTER"
  17. LOCATE 8, 18
  18. PRINT "1. CONVERT US DOLLARS TO POUNDS"
  19. LOCATE 10, 18
  20. PRINT "2. CONVERT POUNDS TO US DOLLARS"
  21. LOCATE 12, 18
  22. PRINT "3. CONVERT US DOLLARS TO CANADIAN DOLLARS"
  23. LOCATE 14, 18
  24. PRINT "4. CONVERT CANADIAN DOLLARS TO US DOLLARS"
  25. LOCATE 16, 18
  26. PRINT "5. END"
  27. LOCATE 19, 16
  28. PRINT "ENTER SELECTION:"
  29. '*************************Print Main Title *******************************
  30. PrintMainTitle:
  31. LOCATE 3, 12
  32. PRINT " C O N V E R T  C U R R E N C Y "
  33. '**************************UstoPounds************************************
  34. UStoPounds:
  35. INPUT "ENTER AMOUNT:", Amount
  36. ConvertedAmount = Amount * .81
  37. PRINT ConvertedAmount
  38. '****************************PoundstoUS***************************
  39. PoundstoUS:
  40. INPUT "ENTER AMOUNT:", Amount
  41. ConvertedAmount = Amount * 1.23
  42. PRINT ConvertedAmount
  43. '**************************UStoCanadian****************************
  44. UStoCanadian:
  45. INPUT "ENTER AMOUNT:", Amount
  46. ConvertedAmount = Amount * 1.42
  47. PRINT ConvertedAmount
  48. '*******************************CanadiantoUS************************
  49. CanadiantoUS:
  50. INPUT "ENTER AMOUNT:", Amount
  51. ConvertedAmount = Amount * .7
  52. PRINT ConvertedAmount
  53.  
  54.  
In order to understand recursion, one must first understand recursion.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with program
« Reply #2 on: April 05, 2020, 10:43:27 am »
HI @Aeveus, you are missing INPUT statement, you have PRINT at the critical points where you need INPUT

eg
Code: QB64: [Select]
  1. PRINT "Enter selection"

should be

Code: QB64: [Select]
  1. INPUT "Enter selection "; choice$

You also needed a SLEEP or _DELAY to stop code from looping around menu after the converted value has been displayed.

Also as programming style goes all those GOSUBs makes for too much unneeded jumping around code.

The menu can all be combined under the mission to display whats available and get a selection.

I would do the whole thing like this:
Code: QB64: [Select]
  1. OPTION _EXPLICIT '<<< checks for typos requires dim ofall your variables
  2. DIM choice AS INTEGER, amt AS SINGLE, cvrt$, from$, to$
  3. DO ' main loop present menu, get choice and convert amount
  4.     CLS
  5.     LOCATE 3, 26
  6.     PRINT "CURRENCY CONVERTER"
  7.     LOCATE 6, 18
  8.     PRINT "1. CONVERT US DOLLARS TO POUNDS"
  9.     LOCATE 8, 18
  10.     PRINT "2. CONVERT POUNDS TO US DOLLARS"
  11.     LOCATE 10, 18
  12.     PRINT "3. CONVERT US DOLLARS TO CANADIAN DOLLARS"
  13.     LOCATE 12, 18
  14.     PRINT "4. CONVERT CANADIAN DOLLARS TO US DOLLARS"
  15.     LOCATE 14, 18
  16.     PRINT "5. END"
  17.     LOCATE 17, 22
  18.     INPUT "ENTER SELECTION: "; choice
  19.     LOCATE 19, 22
  20.     IF choice < 5 THEN
  21.         INPUT "Enter amount to convert "; amt
  22.         SELECT CASE choice
  23.             CASE 1: cvrt$ = _TRIM$(STR$(UStoPounds(amt))): from$ = "US$": to$ = " Pounds"
  24.             CASE 2: cvrt$ = _TRIM$(STR$(PoundstoUS(amt))): from$ = "Pounds": to$ = " US$"
  25.             CASE 3: cvrt$ = _TRIM$(STR$(UStoCanadian(amt))): from$ = "US$": to$ = " Canadian$"
  26.             CASE 4: cvrt$ = _TRIM$(STR$(CanadiantoUS(amt))): from$ = "Canadian$": to$ = " US$"
  27.         END SELECT
  28.         LOCATE 21, 19
  29.         PRINT amt; from$; " converts to "; cvrt$; to$
  30.         _DELAY 3 ' <<< pause screen before cls and next menu display
  31.     END IF
  32. LOOP UNTIL choice = 5
  33.  
  34. FUNCTION UStoPounds (amount)
  35.     UStoPounds = amount * .81
  36.  
  37. FUNCTION PoundstoUS (amount)
  38.     PoundstoUS = amount * 1.23
  39.  
  40. FUNCTION UStoCanadian (amount)
  41.     UStoCanadian = amount * 1.42
  42.  
  43. FUNCTION CanadiantoUS (amount)
  44.     CanadiantoUS = amount * .7
  45.  
  46.  
« Last Edit: April 05, 2020, 11:46:24 am by bplus »

Offline gaslouk

  • Newbie
  • Posts: 29
    • View Profile
Re: Help with program
« Reply #3 on: April 05, 2020, 12:08:16 pm »
Code: QB64: [Select]
  1. '******************************* Program Mainline **************************
  2. OPTION _EXPLICIT '<<< checks for typos requires dim for all your variables
  3. DIM SHARED choice AS LONG
  4. DIM SHARED amount AS LONG
  5. DIM SHARED convertedamount AS LONG
  6.  
  7. 1 GOSUB DisplayMainMenu
  8.  
  9. SELECT CASE choice
  10.   CASE 1
  11.     GOSUB UStoPounds
  12.   CASE 2
  13.     GOSUB PoundstoUS
  14.   CASE 3
  15.     GOSUB UStoCanadian
  16.   CASE 4
  17.     GOSUB CanadiantoUS
  18.   CASE 5
  19.     SYSTEM
  20. LOCATE 19, 34
  21. INPUT "Your Choise:"; choice
  22. '******************************* Display Main Menu ***********************
  23. DisplayMainMenu:
  24. GOSUB PrintMainTitle
  25. LOCATE 5, 26
  26. PRINT "CURRENCY CONVERTER"
  27. LOCATE 8, 18
  28. PRINT "1. CONVERT US DOLLARS TO POUNDS"
  29. LOCATE 10, 18
  30. PRINT "2. CONVERT POUNDS TO US DOLLARS"
  31. LOCATE 12, 18
  32. PRINT "3. CONVERT US DOLLARS TO CANADIAN DOLLARS"
  33. LOCATE 14, 18
  34. PRINT "4. CONVERT CANADIAN DOLLARS TO US DOLLARS"
  35. LOCATE 16, 18
  36. PRINT "5. END"
  37. LOCATE 19, 16
  38. PRINT "ENTER SELECTION:";
  39. '*************************Print Main Title *******************************
  40. PrintMainTitle:
  41. LOCATE 3, 12
  42. PRINT " C O N V E R T  C U R R E N C Y "
  43. '**************************UstoPounds************************************
  44. UStoPounds:
  45. LOCATE 20, 16
  46. INPUT "ENTER AMOUNT:"; amount
  47. convertedamount = amount * .81
  48. LOCATE 21, 16
  49. PRINT "Your Somthing is: "; convertedamount
  50. '****************************PoundstoUS***************************
  51. PoundstoUS:
  52. LOCATE 20, 16
  53. INPUT "ENTER AMOUNT:"; amount
  54. convertedamount = amount * 1.23
  55. LOCATE 21, 16
  56. PRINT "Your Somthing is: "; convertedamount
  57. '**************************UStoCanadian****************************
  58. UStoCanadian:
  59. LOCATE 20, 16
  60. INPUT "ENTER AMOUNT:"; amount
  61. convertedamount = amount * 1.42
  62. LOCATE 21, 16
  63. PRINT "Your Somthing is: "; convertedamount
  64. '*******************************CanadiantoUS************************
  65. CanadiantoUS:
  66. LOCATE 20, 16
  67. INPUT "ENTER AMOUNT:"; amount
  68. convertedamount = amount * .7
  69. LOCATE 21, 16
  70. PRINT "Your Somthing is: "; convertedamount

Aeveus this is your program.

« Last Edit: April 05, 2020, 12:10:50 pm by odin »

Offline odin

  • Administrator
  • Newbie
  • Posts: 92
  • I am.
    • View Profile
Re: Help with program
« Reply #4 on: April 05, 2020, 12:10:31 pm »
Please use code boxes (click the QB64 button when writing a post and paste your code inside the [code] and [/code] tags.

It has been done for you in the post above.