Pascal Programming Help

sekhu

moo?
OP
Member
Joined
Feb 14, 2007
Messages
1,151
Trophies
0
Website
Visit site
XP
369
Country
Code:
program nameloop;
uses crt;
var index,count: integer;
name,initial,gender:string;
salary:real;

begin
clrscr;

for index:=1 to 2 do
begin
writeln('enter your first name ');
readln(name);
writeln('enter your initial ');
readln(initial);
writeln('enter your gender ');
readln(gender);
writeln('enter your salary ');
readln(salary);

end;

begin

for count:=1 to 2 do
writeln (name,'ÂÂ', initial, 'ÂÂ', gender, 'ÂÂ' , salary:2:2, 'ÂÂ');
readln (name, initial, gender, salary);
end;



end.

I'm having trouble displaying the desired output with above code. The out put should be headings:

Name Initial Gender Salary

And below this output the results of the data entered. So far I can get the program to accept input data, for it to display, but it only displays the last set of data. For example, if I enter:

Bob S Male 100
Tim P Male 200

Only the last record, Tim P Male 200 appears, and repeats, rather than the two records appearing below one another. I'm also not sure how to get the headings to display correctly, do I use "goto x,y"? Where do I place the headings?

I've tried variations but I can't get it to display anything but the last record.

Thanks in advance for any guidance.
 

kmihalj

Well-Known Member
Newcomer
Joined
Apr 27, 2006
Messages
98
Trophies
0
Age
49
Location
Croatia
Website
Visit site
XP
208
Country
Croatia
Your program has an error, .... only one record, .... which is overwritten with last entered data, so no previous record exist.


program nameloop;
uses crt;

var index, count: integer;
name,initial,gender: ARRAY [1..2] of string;
salary: ARRAY [1..2] of real;

begin
clrscr;

for index:=1 to 2 do
begin
writeln('enter your first name ');
readln(name[index]);
writeln('enter your initial ');
readln(initial[index]);
writeln('enter your gender ');
readln(gender[index]);
writeln('enter your salary ');
readln(salary[index]);

end;

begin

for count:=1 to 2 do
writeln (name[count],' ', initial[count], ' ', gender[count], ' ' , salary[count]:2:2, ' ');
end;

end.
 

sekhu

moo?
OP
Member
Joined
Feb 14, 2007
Messages
1,151
Trophies
0
Website
Visit site
XP
369
Country
How would you do it without ARRAY, as we're not using arrays at the moment. Also, I'm not familiar with things like:

readln(salary[index]) - do I type [index] and what do the [] do exactly?

We're doing very basic stuff.

thanks for the help so far
 

Elrogos

Member
Newcomer
Joined
Jun 25, 2007
Messages
14
Trophies
0
Website
Visit site
XP
46
Country
If you are allowed to stop the program and get imput after showing each output, you can do the loop like this

CODE

for index:=1 to 2 do
begin
writeln('enter your first name ');
readln(name);
writeln('enter your initial ');
readln(initial);
writeln('enter your gender ');
readln(gender);
writeln('enter your salary ');
readln(salary);
writeln (name,' ', initial, ' ', gender, ' ' , salary:2:2, ' ');
end;


If you are not using arrays yet, I suppose that would be just fine; otherwise, you would need a fixed set of variables (in your problem you should be told how many inputs you can get at most, and that would lead to arrays anyways) or dynamic allocation.

Just for the record, the brackets ( [] ) are used for indexing into the array. Oh and please, DONT use 'goto' for now; that instruction is there for something, but it should be used carefully as it makes a bad programming practice (http://en.wikipedia.org/wiki/Goto)
 

sekhu

moo?
OP
Member
Joined
Feb 14, 2007
Messages
1,151
Trophies
0
Website
Visit site
XP
369
Country
our teacher stated that he wants the output to be displayed after all data has been entered. ALso how do I get the headings (name, initial, gender, salary) to be displayed above the output of the data entered?

thanks
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • Xdqwerty
    what are you looking at?
  • BakerMan
    I rather enjoy a life of taking it easy. I haven't reached that life yet though.
    Xdqwerty @ Xdqwerty: @BakerMan, it wasnt aimed at you but ok