M
M
marina_m162020-05-05 12:47:34
Pascal
marina_m16, 2020-05-05 12:47:34

Wrote a program (line-number). Where is the mistake?

"Write a program that calculates the sum of three numbers entered as a character string. All numbers are integers. Example: Enter the expression: 12+3+45, Answer: 60"

program A2;
var s,s1,s2,s3: string;
begin
  writeln ('Введите три целых числа');
  readln (s1);
  readln(s2);
  readln (s3);
  s:=s1+s2+s3;
  writeln (s1, '+', s2, '+', s3, '=', s);
  readln;
end.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
HemulGM, 2020-05-05
@marina_m16

You are doing the wrong job. You have one string "12+3+45" as input, not 3 different variables.
Therefore, you must split the string on the "+" characters. And each individual character set, in this case
12, 3, and 45, is converted to a numeric type using StrToInt or Val.

J
Jamaludin Osmanov, 2020-05-05
@jamalosm

For variables, you need to specify the number type, because. when adding lines, they continue each other:

'1'+'2'+'3'='123'

1+2+3=6

program A2;
var s,s1,s2,s3: integer;
begin
  writeln('Введите три целых числа');
  readln(s1);
  readln(s2);
  readln(s3);
  s:=s1+s2+s3;
  writeln (s1, '+', s2, '+', s3, '=', s);
  readln;
end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question