:?: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]
String$ and Variables?
- Codemss
- Veteran
- Posts: 124
- Joined: Sun Jun 24, 2007 6:49 am
- Location: Utrecht, The Netherlands
- Contact:
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
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
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL
Re: String$ and Variables?
Please explain why you want that total? What is your application? There may be a better way to do it.Morpheus wrote:I would like to add up the values of each letter that was inputed by the user.
Mac
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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:
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
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
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
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
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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
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