Pascal Programming Help

Discussion in 'Computer Programming, Emulation, and Game Modding' started by sekhu, Mar 27, 2009.

Mar 27, 2009

Pascal Programming Help by sekhu at 7:27 PM (712 Views / 0 Likes) 4 replies

  1. sekhu
    OP

    Member sekhu moo?

    Joined:
    Feb 14, 2007
    Messages:
    1,156
    Country:
    United Kingdom
    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.
     
  2. kmihalj

    Newcomer kmihalj Advanced Member

    Joined:
    Apr 27, 2006
    Messages:
    98
    Location:
    Croatia
    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.
     
  3. sekhu
    OP

    Member sekhu moo?

    Joined:
    Feb 14, 2007
    Messages:
    1,156
    Country:
    United Kingdom
    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
     
  4. Elrogos

    Newcomer Elrogos Member

    Joined:
    Jun 25, 2007
    Messages:
    14
    Country:
    Spain
    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)
     
  5. sekhu
    OP

    Member sekhu moo?

    Joined:
    Feb 14, 2007
    Messages:
    1,156
    Country:
    United Kingdom
    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
     

Share This Page