Computer Science Canada

char array problem

Author:  ynotpeace [ Wed Dec 09, 2009 3:47 pm ]
Post subject:  char array problem

What is it you are trying to achieve?
this code uses char arrays to get letters then prints it back to you without any spaces


What is the problem you are having?
no matter what number of letters i type it only gives back 2 letters Sad





Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
heres the code

Turing:


var word1 : array 1 .. 100 of char
var word2 : array 1 .. 100 of char
var i : int := 0
var l : int :=0
var b : int :=0
var stop : boolean := false
var chars : array char of boolean
put "Enter first phrase> "..



       
         

loop
    Input.KeyDown (chars)
    i := i +1
    get word1(i)

    if word1(i) = " " then
        i := i - 1
    end if

    if chars (KEY_ENTER) then
        stop := true
       
    end if
    exit when stop = true
end loop
stop := false
l := i + 1



loop
    b:= b +1
    if b < l then
    put word1(b)..
    else stop := true
    end if
  exit when stop = true 
end loop
put ""
put "finished"





Please specify what version of Turing you are using
4.1.1

Author:  mirhagk [ Wed Dec 09, 2009 4:02 pm ]
Post subject:  RE:char array problem

Turing:

exit when word1(i)=(KEY_ENTER)


use that to quit the first loop instead, (it will fix it)

Author:  ynotpeace [ Wed Dec 09, 2009 4:13 pm ]
Post subject:  Re: char array problem

why do i have to click enter twice to exit loop???
can you somehow make it once?

Author:  TheGuardian001 [ Wed Dec 09, 2009 4:16 pm ]
Post subject:  Re: char array problem

You need to hit enter twice because you are using get to receive input. get requires that you press enter before anything is actually stored, which is ideal for strings, but horrible for character arrays representing a string. Since you're getting it as a character array, I'd advise you use getch, which will get a single character without waiting for enter to be pressed.

Author:  mirhagk [ Wed Dec 09, 2009 4:22 pm ]
Post subject:  RE:char array problem

its because your using get

you should use the getch command
first declare a string of 1 variable
Turing:

var word:string(1)

then getch the letter from the user and assign it to the array (i also put the value so that it shows on screen)
Turing:

getch(word)
put word..
word1(i):=word

Author:  ynotpeace [ Wed Dec 09, 2009 4:23 pm ]
Post subject:  Re: char array problem

does getch work with "array of char"???
cause:
getch (word1(i))
doesn't work...

Author:  mirhagk [ Wed Dec 09, 2009 4:24 pm ]
Post subject:  RE:char array problem

getch doesnt work with char. it only works with string(1) type (btw that means a string with a length of 1, which is basically a char and char can be assigned to it and vice versa)

Author:  ynotpeace [ Wed Dec 09, 2009 4:49 pm ]
Post subject:  Re: char array problem

sorry about being a pain but i'm really trying to get this

but heres the question i'm trying to solve:

Problem Description
An anagram is a word or a phrase formed by rearranging the letters of another phrase such as
?ITEM? and ?TIME?. Anagrams may be several words long such as ?CS AT WATERLOO? and
?COOL ASWET ART?. Note that two phrases may be anagrams of each other even if each phrase
has a different number of words (as in the previous example). Write a program to determine if two
phrases are anagrams of each other.

Input Specifications
The program should prompt the user for two phrases, each entered on a separate line. You may
assume that the input only contains upper case letters and spaces.

Output Specifications
The program will print out one of two statements: ?Is an anagram.? or ?Is not an anagram.?
Sample Prompting and User Input (user input in italics)

Inputs:
Enter first phrase> CS AT WATERLOO
Enter second phrase> COOL AS WET ART

Output:
Is an anagram.

Author:  mirhagk [ Wed Dec 09, 2009 5:01 pm ]
Post subject:  RE:char array problem

just get repeat the first loop for the second phrase then you can compare the two strings. there's several ways of doing this

first check to see if the lengths are the same, if not then theyre obviously not anagrams

then you could either count the amount of each letter in each array and see if they are the same

OR

you could convert the arrays to strings then go through each letter seeing if it exists in the other string (remove it from that string if it does exist)

Author:  ynotpeace [ Wed Dec 09, 2009 5:06 pm ]
Post subject:  Re: char array problem

thx guys


: