Tuesday 15 June 2010

loops - Trying to get Pascal to read from file then add all of the even numbers having issues. -


OK, I'm trying Pascal to read a set of integers from an input file, even then Take and add them together is not working for me in the category of 1-50. Here's what I have: List of input file numbers 1 2 3 4 5 6 7 8 9 11 12 64 13 14 15 16 71 33 34 35 36 41 44 46 82 512 49 50 and my Pascal program:

  Programs (input, output, indet); Uses Crt; War indenta: lesson; Number, even: integer; Start clrscr; Assign (indenta, 'j: \ num.txt'); Reset (indata) 0; Read (indet, number); While the points> 50 start reading (indet, number); If num 2 mod = 0 even then: = even + num; End; Writelan ('There is even Yoga', even); Readln (); End.   

Any help would be great! When I run it I get the text as "even sum is 0"

The code needs to be corrected, so that it can also be compiled. The reset line calling 0 is closed after ) is invalid syntax.

You first need to initialize your even variable so that it has a valid starting point.

Next, you have to fix the argument in your while loop. It's backwards :-) You num & lt; 50 If you want to stop at last - you have tested num & gt; 50 means that the loop will never be executed, because the first value ( 1 ) while the loop ends.

This is a plain console app in Delphi

  Program 2 project; Uses SysUtils; Var InData: Text; Number, even: integer; Start AssignFile (InData, 'D: \ TempFiles \ numbers.txt'); (Indata) reset; Read (indet, number); Even: = 0; While the number & lt; Start reading 50 (Indet, number); If num 2 mod = 0 even then: = even + num; End; Writelan ('There is even Yoga', even); Readln; End.   

This generates the output (which is correct according to your code, because it reads inside 64 , while num loop and so it should do 1 times more, and even 64 is even added to - I want to remove it for you I will leave for). : -)

  The amount is also 96.    

No comments:

Post a Comment