Results 1 to 10 of 25

Thread: Help / Advice please...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Instead of posting the answer, can you post your code for us to see, so we can tell you where you went wrong?

  2. #2
    sorry, I'm a bit ashamed of the code so far. am a bit lost on it.

    Code:
    var     count,i,totnum,number,numcheck: integer;
                    pos,zero,neg:boolean;
    
    begin {main program}
    
             begin
             
        write('Please type in how many numbers will be input ');
        readln(totnum);
    
             writeln('what numbers will be checked?');
             read(numcheck);
             pos:=numcheck>0;             //boolean variables
             neg:=numcheck<0;
             zero:=numcheck=0;
    
            // for count:= 0 to totnum do
    
            if pos then write('This is a positive number');
            if neg then write('This is a negative number');
            if zero then write('This number is Zero');
    
            cont;
             end;
    
    end.
    
     {        program countspace(input,output);
     var i,lenght,count:integer;
         sentence:string;
    
    
     begin
             write('sentence: ');
             readln(sentence);
    
             for i:=0 to lenght (sentence) do     //Do error??!?!?!?!
             if (sentence[i] = ' ') or (sentence[i] ='.') then
             count:=count+1;
    
             writeln('spaces: ',count);
             readln;         end.   }
    The prog at the bottom is a simple one to read number of spaces but even that wouldn't compile. Simply don't knwo what I'#m doing wrong
    Last edited by stevengreen22; 21-03-2011 at 04:00 PM.

  3. #3
    No need to be ashamed. No matter how long you've been programming, there will always be people who write better, cleaner code. Just make sure you learn from it so you can use it the next time.

    Anyway about your code. It's really not that bad at all. In fact, you're almost there already.

    Code:
    var   count,i,totnum,numcheck: integer;
                    pos,zero,neg:boolean;
    
    procedure askNumber();
    begin
       writeln('what numbers will be checked?');
       read(numcheck);
    
       pos:=numcheck>0;             //boolean variables
       neg:=numcheck<0;
       zero:=numcheck=0;
    
       if pos then writeln('This is a positive number');
       if neg then writeln('This is a negative number');
       if zero then writeln('This number is Zero');
    end;
    
    procedure askTotal();
    begin
        write('Please type in how many numbers will be input ');
        readln(totnum);
    
        for count:= 0 to totnum-1 do askNumber()
    end;
    
    begin //main
      askTotal()
    end.
    In the code above, which is mostly yours, I have separated the first question and placed it in a new procedure, together with a for-loop, which repeats the second procedure with the 2nd question.

    I have not completed your assignment which asks for the totals of positive/negative values, but I'm pretty sure you can do the yourself now. If not, we'll still be here...

  4. #4
    YOu absolute legend, Thank you so much! I've not tested it yet but it looks how i wanted it to. I really liek using procedures from previous tasks, they make everything cleaner but i didn't think to use them in this and the loops are fantastic. thanks!!!!

  5. #5

  6. #6
    I just had a play and it works fine, thankyou, this is also the first I've seen a procedure call upon another, something else that I've learnt, One thing though...Why have you added'()' at the end of them? Is this necessary? I've not used it on any of mine, is it better practice to have them?
    also...i just had an error message but what I'd now like it to do is store the pos / neg / zero boolean variables as a number. is this possible using these variables or should i have additional variables?
    thanks for taking the time to read /reply

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25
    The () is personal preference. They don't affect the compiled code what-so-ever only how your code is read. So honestly it would be up to you as how you prefer your own code-style to be. It's like that last semi-colon before the end in the main code block. You'll notice that he didn't add that either. I always add the semi-colon myself unless it's right before an else in an if .. than .. else statement.
    Jason McMillen
    Pascal Game Development
    Co-Founder





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •