String$ and Variables?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Morpheus
Newbie
Posts: 8
Joined: Sun Dec 30, 2007 3:14 pm
Location: USA

String$ and Variables?

Post by Morpheus »

:?:I have written a program asking the user to input a word. I have given the letters of the alphabet values from 6 to 156. I would like to add up the values of each letter that was inputed by the user. Then i would like to print the total. If anybody can help me i would appreciate it. This is a very nice web site and alot of info.
Thank you[/i]
roy
Veteran
Posts: 55
Joined: Sun Jul 29, 2007 2:39 pm
Location: London

Post by roy »

Could you post your program and what you have tried.
User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss »

First, ask the user to input a word.
I would then make a loop that runs from 1 to the length of the word (LEN(word$)).
In that loop, get the ASCII number of the character at that position(with MID$ and ASC) and add it to an integer variable which is set to 0 before the loop. To get the ascii number of a string: ASC(string$). To get a part of a string$: part$ = MID$(string$, begin, length). If you want one letter, length must be set to 1. Take care: strings don't start at position 0, but at 1.
After the loop, display the variable.
Greets,

Codemss
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Re: String$ and Variables?

Post by Mac »

Morpheus wrote:I would like to add up the values of each letter that was inputed by the user.
Please explain why you want that total? What is your application? There may be a better way to do it.

Mac
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Sounds like Scrabble to me. You can assign each letter your own value in a SELECT CASE routine that reads the word one letter at a time using:

Code: Select all


FOR start = 1 TO LEN(word$)
      letter$ = UCASE$(MID$(word$, start, 1))
      SELECT CASE letter$
         CASE "A": score = 6
         'rest of letters
      END SELECT
      Total = Total + score
NEXT
If you are using consecutive number values for each letter, then you could just use the ASCII codes like Codems said. UCASE the word so that the values are the same for either case key entry. ASC("A") = 65

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

RESOLVED
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply