You are here:

Qbasic, Quickbasic/Unable to get program to run

Advertisement


Question
QUESTION: An auto rental company rents three types of cars at the following rates:

Fixed Cost Cost
Car Type per Day per Mile
Compact $10 $0.15
Intermediate $20 $0.18
Large $30 $0.22

Thus, for example, a person renting a compact car for 3 days and driving 100 miles would be charged 10 * 3 + .15 * 100 = $45.

It should output the charge for each customer. Use the sample data in the following table to test your program.

Customer Name Car Type Days Held Miles Traveled
Jones Large 6 500
Smith Compact 17 3,000
Baker Intermediate 8 250
Williams Intermediate 4 1,000
Winston Large 3 500
I have put together a program but it is not pulling the data and math to generate the "total cost for rental" field. what am I doing wrong. This is what I have come up with:

'*************************************
'* THIS PROGRAM IS TO CALCULATE CAR RENTAL REVENUE GENERATED *
'*************************************
'VARIABLE USED:
'T1$, H1$, H2$, D1$ Customer Name
'NAM $ Employee Name
'VEHICALTYPE$ Vehicle Type
'MILAGE Miles Traveled
'RATE Vehicle Rate
'DAYS Days Held
'TOTAL Total Revenue Generated Per Account

'*************************************
'* PROGRAM MAINLINE *
'*************************************
CLS
GOSUB InitializeVariables
GOSUB PrintHeadings
GOSUB ProcessDetail
GOSUB CalculateAnswer
END

'*************************************
'* INITILIZE VARIABLES *
'*************************************

InitializeVariables:
LET T1$ = " CAR RENTAL AUGUST 2011 "
LET H1$ = " Customer Vehical Days Miles Total Cost"
LET H2$ = " Name Type Held Traveled For Rental"
LET D1$ = "/ / / / ## #### ######"

'*************************************
'* PRINT HEADINGS *
'*************************************
PrintHeadings:
PRINT
PRINT T1$ 'Print Title Line
PRINT
PRINT H1$ 'Print Column Heading, Line 1
PRINT H2$ 'Print Column Heading, Line 2
PRINT
RETURN

'*************************************
'* PROCESS DETAIL *
'*************************************
ProcessDetail:
GOSUB ReadData 'Prime the data read
DO UNTIL UCASE$(NAM$) = "END"
GOSUB CalculateVehicalCost
GOSUB CalculateAnswer
GOSUB ReadData
LOOP
RETURN

'*************************************
'* READ DATA *
'*************************************

ReadData:
READ NAM$, VehicalType$, DaysHeld, MilesTraveled
DATA "Jones","Large",6,500
DATA "Smith","Compact",17,3000
DATA "Baker","Intermediate",8,250
DATA "Williams","Intermediate",4,1000
DATA "Winston","Large",3,500
DATA "END",0,0,0
RETURN

'*************************************
'* CALCULATE VEHICAL COST *
'*************************************
CalculateVehicalCost:
LET Compact = (MilesTraveled * .15)
LET Intermediate = (MilesTraveled * .18)
LET Large = (MilesTraveled * .22)

'*************************************
'* CALCULATE ANSWER *
'*************************************
CalculateAnswer:
LET TotalCostForRental = (VehicalType * DaysHeld)

'*************************************
'* PRINT DETAIL *
'*************************************
PrintDetail:
PRINT USING D1$; NAM$, VehicalType, DaysHeld, MilesTraveled
RETURN
END

I was told to put the read data into an array but im not exactly how to do that plus I keep getting a Type MisMatch error on the PRINT USING D1$; NAM$, VehicalType, DaysHeld, MilesTraveled line


Thank you for your help!

ANSWER: Hi Jay,

It appears as if you're using `VehicalType` (also, if your teacher is marking on spelling in code, it's Vehicle :D) as a string in most places, but try and use it as an integer in:

CalculateAnswer:
LET TotalCostForRental = (VehicalType * DaysHeld)


You'll need some sort of mechanism in your code to convert your VehicalType$ to on of the vehicle costs (Compact, Intermediate, Large) to get the correct TotalCostForRental value

Your PRINT USING needs VehicalType to have a $ at the end to specify that it's the same string from the read statement.

-Alex

---------- FOLLOW-UP ----------

QUESTION: After fixing my code slightly I still get an error on the

LET TotalCostForRental = (VehicleType$ * DaysHeld)line


can you look at my code and help me diagnosis the problem?

'************************************
'* THIS PROGRAM IS TO CALCULATE CAR RENTAL REVENUE GENERATED *
'*************************************
'VARIABLE USED:
'T1$, H1$, H2$, D1$
'NAM $         Customer Name
'VehicleType$  Vehicle Type
'MILAGE        Miles Traveled
'RATE          Vehicle Rate
'DaysHeld      Days Held
'TOTAL         Total Revenue Generated Per Account

'*************************************
'* PROGRAM MAINLINE *
'*************************************
CLS
GOSUB InitializeVariables
GOSUB PrintHeadings
GOSUB ProcessDetail
GOSUB CalculateAnswer
END

'*************************************
'* INITILIZE VARIABLES *
'*************************************

InitializeVariables:
LET T1$ = " CAR RENTAL "
LET H1$ = " Customer Vehicle Days Miles Total Cost"
LET H2$ = " Name Type Held Traveled For Rental"
LET D1$ = "/ / / / ## #### ######"

'*************************************
'* PRINT HEADINGS *
'*************************************
PrintHeadings:
PRINT
PRINT T1$ 'Print Title Line
PRINT
PRINT H1$ 'Print Column Heading, Line 1
PRINT H2$ 'Print Column Heading, Line 2
PRINT
RETURN

'*************************************
'* PROCESS DETAIL *
'*************************************
ProcessDetail:
GOSUB ReadData 'Prime the data read
DO UNTIL UCASE$(NAM$) = "END"
GOSUB CalculateVehicleCost
GOSUB CalculateAnswer
GOSUB ReadData
LOOP
RETURN

'*************************************
'* READ DATA *
'*************************************

ReadData:
READ NAM$, VehicleType$, DaysHeld, MilesTraveled
DATA "Jones","Large",6,500
DATA "Smith","Compact",17,3000
DATA "Baker","Intermediate",8,250
DATA "Williams","Intermediate",4,1000
DATA "Winston","Large",3,500
DATA "END",0,0,0
RETURN

'*************************************
'* CALCULATE VEHICAL COST *
'*************************************
CalculateVehicleCost:
LET Compact = (MilesTraveled * .15)
LET Intermediate = (MilesTraveled * .18)
LET Large = (MilesTraveled * .22)

'*************************************
'* CALCULATE ANSWER *
'*************************************
CalculateAnswer:
LET TotalCostForRental = (VehicleType$ * DaysHeld)

'*************************************
'* PRINT DETAIL *
'*************************************
PrintDetail:
PRINT USING D1$; NAM$; VehicleType$; DaysHeld; MilesTraveled
RETURN
END

ANSWER: As I said before:

You'll need some sort of mechanism in your code to convert your VehicalType$ to on of the vehicle costs (Compact, Intermediate, Large) to get the correct TotalCostForRental value


Try doing some sort of IF/ELSE IF/END IF statements to make sure you're using the correct vehicle cost for the vehicle type.

-Alex

---------- FOLLOW-UP ----------

QUESTION: so something like this?

'Calculate vehicle cost
   IF VehicleType$ >= Compact THEN
       Rate = 10

this is really confusing

Answer
You'll need to plan what you want and break it down into programming steps.

For instance, you know that you need to pick the Rate based on the type of vehicle based on the named type.  You know the variables with the rate data are Compact, Intermediate, and Large, found here:

CalculateVehicleCost:
LET Compact = (MilesTraveled * .15)
LET Intermediate = (MilesTraveled * .18)
LET Large = (MilesTraveled * .22)


You know the string values are (from your data statement): "Compact", "Intermediate", and "Large", from your data statements here (second column):

DATA "Jones","Large",6,500
DATA "Smith","Compact",17,3000
DATA "Baker","Intermediate",8,250
DATA "Williams","Intermediate",4,1000
DATA "Winston","Large",3,500
DATA "END","",0,0


Also, I just noticed, you should move your data statements to the bottom of your program so you're not making duplicating your data every time you call GOSUB ReadData - also, just corrected the last DATA statement so it uses a string for the second value.

The IF statement you tried won't work because you are literally telling QBasic "if the string value of 'VehicleType$ is greater than or equal to the integer value of Compact, then Rate = 10" - a few problems.  First, you can't directly compare a string value with an integer.  Second, a string can only be compared with = (equal) or <> (not equal), not a >= (greater than or equal).

You're going to have to change that to:

IF VehicleType$ = "Compact" THEN
 Rate = 10
END IF


I won't do your school homework/project for you, but I can give you pointers on what to do next.

Take care,
-Alex

Qbasic, Quickbasic

All Answers


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2016 About.com. All rights reserved.