Recent

Author Topic: Ive tried several times To get this programme to work but it wont  (Read 9178 times)

MyLAWLAWL

  • New member
  • *
  • Posts: 7
Program seen below continuously crashes when i get to the statement "net_income = (Monthly_income - Total_Expenses + Total_Monthly_Loan_Repayments);" can someone please tell me what im doing wrong and please note i am new to programming i just started yesterday so please don't be too harsh



Program approved_applicants(input, output);
uses crt;

const
     Clarendon_Court = 12500;
     Sangre_Grande = 9500;
     Providence_Gardens = 7500;

var
   housing_community, name : string;
   net_income : real;
   Monthly_Income : real;
   Total_Monthly_Loan_Repayments : real;
   Total_Expenses : real;

  begin
       writeln('Hello! Please choose your housing community Clarendon Court, Sangre Grande Villas, Providence Gardens:  ') ;
       readln(housing_community);
       writeln('Please enter your Full Name: ');
       readln(name);
       writeln('Please enter your Total Monthly Loan Repayments');
       readln(Total_Monthly_Loan_Repayments);
       writeln('Please enter your Total Monthly Expenses');
       readln(Total_Expenses);
       writeln('Please enter the your Monthly Income');
       readln(Monthly_Income);
       net_income = (Monthly_income - Total_Expenses + Total_Monthly_Loan_Repayments);
                  If (housing_community='Providence Gardens')Then;
                        If Net_income>=Sangre_Grande then
                        If ((Total_Expenses + Total_Monthly_Loan_Repayments)>=net_income*0.5) then
                        writeln('You have Successfully been approved for housing in Providence Gardens Congratulations')
                     else
                        writeln('Im truly sorry but you have been declined approval for housing in Providence Gardens')
                  Else If (housing_community= 'Clarendon Court')Then
                        If Net_income>=Clarendon_Court then
                        If ((Total_Expenses + Total_Monthly_Loan_Repayments)>=net_income*0.5) then
                           writeln('You have Successfully been approved for housing in Clarendon Court Congratulations')
                        else
                            writeln('Im truly sorry but you have been declined approval for housing in Claredon Court')
                  Else If (housing_community='Sangre Grande Villas')Then
                        If Net_income>=Providence_Gardens then
                        If ((Total_Expenses + Total_Monthly_Loan_Repayments)>=net_income*0.5) then
                           writeln('You have Successfully been approved for housing in Sangre Grande Congratulations')
                       else
                            writeln('Im truly sorry but you have been declined approval for housing in Sangre Grande');
end.

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: Ive tried several times To get this programme to work but it wont
« Reply #1 on: March 02, 2017, 05:52:02 pm »
In Pascal an assignment is written with := and not with just a =.
So try this:

Code: Pascal  [Select][+][-]
  1. net_income := (Monthly_income - Total_Expenses + Total_Monthly_Loan_Repayments);
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Ive tried several times To get this programme to work but it wont
« Reply #2 on: March 02, 2017, 06:02:35 pm »
intended or not:
Code: [Select]
If (housing_community='Providence Gardens')Then;
Note the use of the semi-colon at the end.

btw:
Program seen below continuously crashes when i get to the statement "net_income = (Monthly_income - Total_Expenses + Total_Monthly_Loan_Repayments);"
That is not crashing, that is called "the compiler stops compiling becuase it encountered an erro and informs you about this error"

a crash is when you run your program and that program unexpectedly stops working with either a runtime error, sigv fault, or stops working completely without any notification
« Last Edit: March 02, 2017, 06:07:05 pm by molly »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Ive tried several times To get this programme to work but it wont
« Reply #3 on: March 02, 2017, 06:07:39 pm »
Program seen below continuously crashes when i get to the statement "net_income = (Monthly_income - Total_Expenses + Total_Monthly_Loan_Repayments);"

How can the program crash if that line will not even compile?

Bart

jacmoe

  • Full Member
  • ***
  • Posts: 249
    • Jacmoe's Cyber SoapBox
Re: Ive tried several times To get this programme to work but it wont
« Reply #4 on: March 02, 2017, 06:09:00 pm »
Perhaps, what he calls 'crash', is the compiler failing to compile it?
more signal - less noise

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: Ive tried several times To get this programme to work but it wont
« Reply #5 on: March 03, 2017, 04:21:19 am »
The first thing you need to understand when learning a programming language is its syntax. Long before you start to apply logic into it. Syntax describes how to write a correct code in the particular language. So, I suggest you to read these first:

- Free Pascal Language Reference: http://freepascal.org/docs-html/current/ref/ref.html
- Pascal Language Tutorial: https://www.tutorialspoint.com/pascal/
- Object Pascal Tutorial: http://wiki.lazarus.freepascal.org/Object_Pascal_Tutorial

Good luck! :)
-Bee-

A long time pascal lover.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Ive tried several times To get this programme to work but it wont
« Reply #6 on: March 03, 2017, 06:38:45 am »
Apart from the two errors pointed to by JanRoza and molly the code compiles and works fine. Just the = and the then; must be := and then
Specialize a type, not a var.

MyLAWLAWL

  • New member
  • *
  • Posts: 7
Re: Ive tried several times To get this programme to work but it wont
« Reply #7 on: March 03, 2017, 11:09:49 pm »
The first thing you need to understand when learning a programming language is its syntax. Long before you start to apply logic into it. Syntax describes how to write a correct code in the particular language. So, I suggest you to read these first:

- Free Pascal Language Reference: http://freepascal.org/docs-html/current/ref/ref.html
- Pascal Language Tutorial: https://www.tutorialspoint.com/pascal/
- Object Pascal Tutorial: http://wiki.lazarus.freepascal.org/Object_Pascal_Tutorial

Good luck! :)
Thanks :)

MyLAWLAWL

  • New member
  • *
  • Posts: 7
Re: Ive tried several times To get this programme to work but it wont
« Reply #8 on: March 03, 2017, 11:10:28 pm »
In Pascal an assignment is written with := and not with just a =.
So try this:

Code: Pascal  [Select][+][-]
  1. net_income := (Monthly_income - Total_Expenses + Total_Monthly_Loan_Repayments);

 :D thanks lol i wouldnt have known sorry if i came across noobish

MyLAWLAWL

  • New member
  • *
  • Posts: 7
Re: Ive tried several times To get this programme to work but it wont
« Reply #9 on: March 03, 2017, 11:11:10 pm »
intended or not:
Code: [Select]
If (housing_community='Providence Gardens')Then;
Note the use of the semi-colon at the end.

btw:
Program seen below continuously crashes when i get to the statement "net_income = (Monthly_income - Total_Expenses + Total_Monthly_Loan_Repayments);"
That is not crashing, that is called "the compiler stops compiling becuase it encountered an erro and informs you about this error"

a crash is when you run your program and that program unexpectedly stops working with either a runtime error, sigv fault, or stops working completely without any notification
oh okay sorry for sounding noobish

MyLAWLAWL

  • New member
  • *
  • Posts: 7
Re: Ive tried several times To get this programme to work but it wont
« Reply #10 on: March 03, 2017, 11:26:01 pm »
I changed it but it crashes i full understand what crashing means now but it just cuts off at that point on my computer is there something wrong with it? i made the changes and it compiles successfully now

Program approved_applicants(input, output);
uses crt;

const
     Clarendon_Court = 12500;
     Sangre_Grande = 9500;
     Providence_Gardens = 7500;

var
   housing_community, name : string;
   Monthly_Income : real;
   Total_Monthly_Loan_Repayments : real;
   Total_Expenses : real;
   net_income : real;

  begin
       writeln('Hello! Please choose your housing community Clarendon Court, Sangre Grande Villas, Providence Gardens:  ') ;
       readln(housing_community);
       writeln('Please enter your Full Name: ');
       readln(name);
       writeln('Please enter your Total Monthly Loan Repayments');
       readln(Total_Monthly_Loan_Repayments);
       writeln('Please enter your Total Monthly Expenses');
       readln(Total_Expenses);
       writeln('Please enter the your Monthly Income');
       readln(Monthly_Income);
       net_income := (Monthly_income - Total_Expenses + Total_Monthly_Loan_Repayments);
                  If (housing_community='Providence Gardens')Then
                        If Net_income>=Sangre_Grande then
                        If ((Total_Expenses + Total_Monthly_Loan_Repayments)>=net_income*0.5) then
                        writeln('You have Successfully been approved for housing in Providence Gardens Congratulations')
                     else
                        writeln('Im truly sorry but you have been declined approval for housing in Providence Gardens')
                  Else If (housing_community= 'Clarendon Court')Then
                        If Net_income>=Clarendon_Court then
                        If ((Total_Expenses + Total_Monthly_Loan_Repayments)>=net_income*0.5) then
                           writeln('You have Successfully been approved for housing in Clarendon Court Congratulations')
                        else
                            writeln('Im truly sorry but you have been declined approval for housing in Claredon Court')
                  Else If (housing_community='Sangre Grande Villas')Then
                        If Net_income>=Providence_Gardens then
                        If ((Total_Expenses + Total_Monthly_Loan_Repayments)>=net_income*0.5) then
                           writeln('You have Successfully been approved for housing in Sangre Grande Congratulations')
                       else
                            writeln('Im truly sorry but you have been declined approval for housing in Sangre Grande');
end.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Ive tried several times To get this programme to work but it wont
« Reply #11 on: March 03, 2017, 11:49:48 pm »
Homework assignement I guess?

It does not crash (as long as I give in correct input), but most of the times it does not output anything after I entered my Monthly Income.
One time it did output something it declined me, even while my income was 50 thousand and my expenses ony 30...

To make the code at the end more redable, put your if..then..else blocks inside begin..end blocks.

Further: do not let the user enter the entire name of the project, especially when you later on compare the input in a case sensitive manner.
Present the choice as something like:
1: project A
2: project B
3: project C
And then let the user just enter 1,2, or 3.
Since you use crt unit, you can use ReadKey function for that instead of Read or Readln.

Validate input. In the example above do not accept anything but 1, 2 or 3.
Why do you let me enter my name, and then don't use this later on anywhere?

Bart

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Ive tried several times To get this programme to work but it wont
« Reply #12 on: March 04, 2017, 12:07:20 am »
This guy:

http://stackoverflow.com/questions/42545156/why-am-i-getting-22-3-itprog1-pas-fatal-syntax-error-expected-but-else-fo

seems to have a very similar problem.

Hint: if you are not a master in when a ; before an ELSE is allowed and when not, add begin .. end pairs to your nesting.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Ive tried several times To get this programme to work but it wont
« Reply #13 on: March 04, 2017, 12:13:45 am »
Try to follow your code when user selects;

Providence Gardens
Loan repayments: 10
Monthly expenses: 20
Monthly income: 200

You do want to end up in the 'Im truly sorry but you have been declined approval for housing in Providence Gardens', but the only if statement that evaluates to true is "If (housing_community='Providence Gardens')" all following if statements evaluate to False, and there will be no output at all.

Code like
Code: [Select]
if a then if c then x else y else if q then if z then foo else bar
is hard to read: which else belongs to which if?
It may very well not be what you intended.

OTOH
Code: [Select]
if a then
begin
  if b then
    foo
  else
    bar
end
else if c then
begin
  if p then
    foobar
  else
    barfoo
end;
is easier to follow and the compiler will do what you expect it to.

Also: why do you approve me (in all cases) when my net income is less than my expenses?

Bart

jacmoe

  • Full Member
  • ***
  • Posts: 249
    • Jacmoe's Cyber SoapBox
Re: Ive tried several times To get this programme to work but it wont
« Reply #14 on: March 04, 2017, 12:14:31 am »
@MyLAWLAWL :

Could you Please edit your posts and surround your code with code tags ?

As it is now, it is simply not pleasant to read at all. And that means that I don't bother taking the extra time required. And I suspect that not only applies to me, but a lot of other potential helpers.

So, make it easy to help by making it easy to read.  ;)

My eye sight is not what it used to be, you know - and the time that I have is not what it used to be either.
more signal - less noise

 

TinyPortal © 2005-2018