C++ Compilation Failed

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
RichK88
Newbie
Posts: 2
Joined: Mon Sep 17, 2018 11:39 am

C++ Compilation Failed

Post by RichK88 » Mon Sep 17, 2018 11:46 am

Im doing a QBASIC Assignment where i "Write a program that will input a number and print the number, the square of the number, and the cube of the number. Continue the operation until 999 is entered". I've written the code below but i still end up with "C++ Compilation Failed" every time i try and run it. The book is really bad at explaining where i am going wrong and my professor just gave me hint on my GOSUB's so if anybody can explain to me in lighter terms where my problem is occurring it would be greatly appreciated!

Rem Richard Kopycinski
'************************** SQUARE AND CUBE PROGRAM *************************
'
'Variables Used: OriginalNumber --------------> Users Input Number
' SquareOfNumber --------------> Calculated Square
' CubeOfNumber ----------------> Calculated Cube
'
'**************************** Program Main Line *****************************
'
CLS
GOSUB InputNumber
DO While Num <> = 999
GOSUB InitializeScreen

GOSUB CalculateResults
GOSUB PrintResults
GOSUB InPutNumber
LOOP
END
'
'***************************** Input Number *********************************
InputNumber:
Input "Users Input Number:" , OriginalNumber$
RETURN
'
'**************************** Initialize Screen *****************************
InitializeScreen:
CLS
RETURN
'
'**************************** Calculate Results *****************************
CalculateResults:
SquareOfNumber = (OriginalNUmber ^ 2)
CubeOfNUmber = (OriginalNUmber ^ 3)
RETURN
'
'****************************** Print Results *******************************
PrintResults:
PRINT
PRINT , "Original Number: " ; OriginalNumber
PRINT , "Square Of Number: " ; SquareOfNumber
PRINT , "Cube Of Number: " ; CubeOfNumber
RETURN
'
'****************************** End Of Program ******************************

MikeHawk
Newbie
Posts: 5
Joined: Sun Jul 08, 2018 11:23 am

Re: C++ Compilation Failed

Post by MikeHawk » Sun Sep 23, 2018 6:56 am

Hi Rich. QBASIC Assignment? What year is this? "C++ Compilation Failed"?! I can't tell if this is some elaborate shitposting or if I'm missing something. I'm getting too old for the internet. I have no idea why you'd get a "C++ Compilation Failed", but there are more concerning things going on:

1. As a rule of thumb, you'd be better off using FUNCTIONs rather than GOSUBs. It's not an issue with small test programs, but the day your code becomes too confusing for you to understand, remember there's an alternative you can try. There's nothing wrong with your usage of GOSUB and RETURN, so maybe your compiler doesn't support those instructions by default, hence your teacher's hint?

2. You have to use only one operator in your "DO WHILE" statement, either "<>" (not equal) or "=" (equal). Still on that line, "Num" is always going to be zero because "Num" is only used in that statement and never initialized or modified anywhere else.

3. "OriginalNumber$" and "OriginalNUmber" are two different variables of different types (OriginalNumber$ holds text, OriginalNUmber holds numbers). OriginalNUmber is never initialized or modified anywhere else, and will thus always be zero. Two possibilities: you either stick with OriginalNumber$ and have to convert the variable before doing mathematical operations (with "VAL(OriginalNumber$)") - it'll work as intended for as long as the user doesn't input non-number things; Or you use OriginalNUmber and you have to tweak a couple of lines (INPUT under "InputNumber" and PRINT under "PrintResults") - in this case, INPUT will make sure the user inputs numeric values only.

4. Overall, be careful with variables, especially if you're going to hold very large numbers. By default, QBASIC uses 16-bit integers, and it won't be sufficient for your needs.

Spoiler below. Since it's been a week already, I don't feel like I'm doing your homework for you. And since I suspect GOSUBs were a part of the assignment (or maybe they were not, which is equally terrifying - why would you willingly subject yourself to that?), here's a version of the code that works without GOSUBs and uses LONG integers (32 bits per value) to ensure everything stays in range at least until 998:

Code: Select all

CLS ' Clear screen
DO ' Infinite loop -- CUE START
  INPUT "User Input Number: ", Num& ' Ask for user's number, stores in Num& (LONG integer)
  IF (Num& = 999) THEN EXIT DO ' If Num& is 999, exit loop now
  NumSQR& = Num& ^ 2 ' Get SQUARE of Num&, store in NumSQR&
  NumCUB& = Num& ^ 3 ' Get CUBE of Num&, store in NumCUB&
  PRINT "    Original Number: " ; Num& ' Print original number
  PRINT "   Square of Number: " ; NumSQR& ' Print square number
  PRINT "     Cube of Number: " ; NumCUB& ' Print cube number
  PRINT ' Print extra blank
LOOP ' Infinite loop -- CUE STOP
END ' End program

Post Reply