3

I'm a grade 11 student and I am coding in Basic language. I asked the user a question, and the user gets to choose yes or no. In the coding, a "type mismatch" error displayed. Also in the coding, after the question, I coded in some IFs and END IFs, and before I had this problem, it printed 0 when I ran the program.

I'm confused as to why I'm getting a type mismatch, even though my variables in the area of the IFs and END IFs. I have a string with $ at the end as the variable. This is what I have.

A part of the program is supposed to calculate the total insurance of someone renting a car, but the computer needs to know if the user even wants insurance. Even if I say I do want insurance, the program still displays 0 when it outputs the insurance at the end of the program.

So my question is, why am I getting a type mismatch?

Print "Do you want insurance?"
Print "1.Yes"
Print "2.No"
Input strInsurance$
IF strInsurance$=Yes THEN
    strInsurance$=sngDailyInsurance*sngDays
END IF
IF strInsurance$=No THEN
    strInsurance$=0
END IF
Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
  • You may drop the useless `str` prefix from `strInsurance$` as the type is already encoded in the `$` sigil. Also make that second `IF` as `ELSE IF` construct or `ELIF` if your BASIC dialect has this, and handle the case when the user enters neither "Yes" or "No". – BlackJack Jun 28 '22 at 10:43

2 Answers2

2

strInsurance$ is a string variable, so you'd have to surround Yes with quotes, like:

IF strInsurance$="Yes" THEN

And:

IF strInsurance$="No" THEN

Also, you cannot assign a number to a string variable like you do here:

strInsurance$=0

And here:

strInsurance$=sngDailyInsurance*sngDays

It's strange that you reuse the input variable for the result in any case.

Miguel Calderón
  • 3,001
  • 1
  • 16
  • 18
0

The correct code would be:

Print "Do you want insurance?"
Print "1.Yes"
Print "2.No"
Input strInsurance$
IF strInsurance$="Yes" THEN
   strInsurance$=str$(sngDailyInsurance*sngDays)
END IF
IF strInsurance$="No" THEN
   strInsurance$="0"
END IF
Azathoth
  • 105
  • 1
  • 9