Hello,
I want this program to take some numbers from the input and print their sum. This happens until the Enter key is pressed:
function numbers()
flag = 0
sum = 0
while flag != ""
print("Enter the number: ")
flag = parse(Int,readline())
sum += flag
end
println("The sum is $sum")
end
numbers()
Because the flag becomes the code of the Enter key, the program does not work properly. Should I use the ASCII code of the Enter key?
I just asked Perplexity to write that code for me. Here it is:
function sum_of_integers()
total_sum = 0
while true
print("Enter a number: ")
input = readline(stdin)
if isempty(input)
println("Terminating the loop.")
break
elseif isdigit(input[1])
num = parse(Int, input)
total_sum += num
println("Current sum: $total_sum")
else
println("Please enter only a number.")
end
end
println("The total sum of entered numbers is: $total_sum")
end
One answer: just store the readline() result in a variable, and then check it before trying to parse it. i.e. change your loop body after the println to:
line = readline()
line == "" && break # halt loop on an empty input
sum += parse(Int, line)
Of course, in this case you can replace the while flag != "" condition with while true, since that check is always true anyway — the exit condition is instead in the body of the loop. And you no longer need the flag variable.
This doesn’t have any error checking (e.g. it doesn’t check for an input that is not a number — it will throw an exception in that case), but you could make the parsing more robust in any number of ways, e.g. by using tryparse instead of parse.
The ASCII code of \n is 10 (0x0a). Do you really want to stop whenever the user enters “10”?
Or if you are asking how to access the ASCII codes that were entered, these are all accessible, but are in the string returned by readline(), but you have no way to access this if you pass it directly to parse rather than storing it in a variable.
(This is a classic example of an XY problem — you have a basic logic bug in your program in which you were trying to parse a string before checking its contents, but you confused yourself by instead focusing on ASCII codes.)
In general, AI-generated content is discouraged. People are on this forum to talk to humans.
The AI-generated answer doesn’t contain any explanations, contains lots of extraneous changes that distract from understanding what was wrong with the original code, and is not particularly good code anyway (e.g. checking whether the first character of the line is a digit is not a robust way to check for numeric input — it has both false negatives and false positives).
function numbers()
flag = 0
sum = 0
print("Enter the number: ")
while ((flag = readline()) != "")
print("Enter the number: ")
sum += parse(Int,flag)
end
println("The summer is $sum")
end
numbers()
The only problem is that I have used two print commands to print the message.
You can just break out of a while true loop, as already suggested by @stevengj above.
function numbers()
sum = 0
while true
print("Enter the number: ")
line = readline()
line == "" && break
sum += parse(Int, line)
end
println("The sum is $sum")
end
Or if you don’t like break statements:
function numbers()
sum = 0
stop = false
while !stop
print("Enter the number: ")
line = readline()
stop = isempty(line)
stop || (sum += parse(Int, line)) # (i.e. if !stop ... end)
end
println("The sum is $sum")
end