View Full Version : QBasic questions
mewhoexactlywhat
Oct11-05, 09:25 PM
There are a few things that I can't remember learning in my Computer Programming class, and I haven't been able to find the answers. Can anyone tell me how to:
-round a number to a certain number of decimal places?
-use dates (month day, year) as response to input as a single variable?
-how/why to use a "line input prompt"?
Thank you.
round a number to a certain number of decimal places?
I guess you could use the print using command.
PRINT USING
Suppose you want to add $12.47 to $1.03. The correct answer is $13.50. This almost works:
PRINT 12.47 + 1.03
It makes the computer print:
13.5
But instead of 13.5, we should try to make the computer print 13.50.
This command forces the computer to print 13.50:
PRINT USING "##.##"; 12.47 + 1.03
The “##.##” is called the picture or image or format: it says to print two characters, then a decimal point, then two digits. The computer will print:
13.50
This command puts that answer into a sentence:
PRINT USING "You spent ##.## at our store"; 12.47 + 1.03
The computer will print:
You spent 13.50 at our store
Rounding This program makes the computer divide 300 by 7 but round the answer to two decimal places:
CLS
PRINT USING "##.##"; 300 / 7
When the computer divides 300 by 7, it gets 42.85714, but the format rounds the answer to 42.86. The computer will print:
42.86
Multiple numbers Every format (such as “###.##”) is a string. You can replace the format by a string variable:
CLS
a$ = "###.##"
PRINT USING a$; 247.91
PRINT USING a$; 823
PRINT USING a$; 7
PRINT USING a$; -5
PRINT USING a$; -80.3
The computer will print:
247.91
823.00
7.00
-5.00
-80.30
When the computer prints that column of numbers, notice that the computer prints the decimal points underneath each other so that they line up. So to make decimal points line up, say PRINT USING instead of just PRINT.
To print those numbers across instead of down, say this:
PRINT USING "###.##"; 247.91; 823; 7; -5; -80.3
It makes the computer print 247.91, then 823.00, etc., like this:
247.91823.00 7.00 -5.00-80.30
Since the computer prints those numbers so close together, they’re hard to read. To make the computer insert extra space between the numbers, widen the format by putting a fourth “#” before the decimal point:
PRINT USING "####.##"; 247.91; 823; 7; -5; -80.3
Then the computer will print:
247.91 823.00 7.00 -5.00 -80.30
If you say —
PRINT USING "My ## pals drank ###.# pints of gin"; 24; 983.5
the computer will print:
My 24 pals drank 983.5 pints of gin
Oversized numbers Suppose you say:
PRINT USING "###.##"; 16238.7
The computer tries to print 16238.7 by using the format “###.##”. But since that format allows just three digits before the decimal point, the format isn’t large enough to fit 16238.7. So the computer must disobey the format. But the computer also prints a percent sign, which means, “Warning! I am disobeying you!” Altogether, the computer prints:
%16238.70
Final semicolon At the end of the PRINT USING statement, you can put a semicolon:
CLS
PRINT USING "##.##"; 13.5;
PRINT "credit"
Line 2 makes the computer print 13.50. The semicolon at the end of line 2 makes the computer print “credit” on the same line, like this:
13.50credit
Advanced formats Suppose you’re running a high-risk business. On Monday, your business runs badly: you lose $27,931.60, so your “profit” is minus $27,931.60. On Tuesday, your business does slightly better than break-even: your net profit for the day is $8.95.
Let’s make the computer print the word “profit”, then the amount of your profit (such as -$27,931.60 or $8.95), then the word “ha” (because you’re cynical about how your business is going).
You can do that printing in several ways. Let’s explore them.…
If you say —
CLS
a$ = "profit######.##ha"
PRINT USING a$; -27931.6
PRINT USING a$; 8.95
the computer will print:
profit-27931.60ha
profit 8.95ha
If you change the format to “profit###,###.##ha”, the computer will insert a comma if the number is large:
profit-27,931.60ha
profit 8.95ha
If you change the format to “profit+#####.##ha”, the computer will print a plus sign in front of any positive number:
profit-27931.60ha
profit +8.95ha
To print a negative number, the computer normally prints a minus sign before the number. That’s called a leading minus. You can make the computer put the minus sign after the number instead; that’s called a trailing minus. For example, if you change the format to “profit######.##-ha”, the computer will print a minus sign AFTER a negative number (and no minus after a positive number), like this:
profit27931.60-ha
profit 8.95 ha
Normally, a format begins with ##. If you begin with $$ instead (like this: “profit$$#####.##ha”), the computer will print a dollar sign before the digits:
profit-$27931.60ha
profit $8.95ha
If you begin with ** (like this: “profit**#####.##ha”), the computer will print asterisks before the number:
profit*-27931.60ha
profit******8.95ha
If you begin with **$ (like this: “profit**$#####.##ha”), the computer will print asterisks and a dollar sign:
profit*-$27931.60ha
profit******$8.95ha
When you’re printing a paycheck, use the asterisks to prevent the employee from enlarging his salary. Since the asterisks protect the check from being altered, they’re called check protection.
You can combine several techniques into a single format. For example, you can combine the comma, the trailing minus, and the **$ (like this: “profit**$##,###.##-ha”), so that the computer will print:
profit**$27,931.60-ha
profit*******$8.95 ha
If you change the format to “profit##.#####^^^^ha”, the computer will print numbers by using E notation:
profit-2.79316E+04ha
profit 8.95000E+00ha
ComputerGeek
Oct12-05, 12:15 AM
There are a few things that I can't remember learning in my Computer Programming class, and I haven't been able to find the answers. Can anyone tell me how to:
-round a number to a certain number of decimal places?
-use dates (month day, year) as response to input as a single variable?
-how/why to use a "line input prompt"?
Thank you.
best to forget QBasic and use python. QBasic is a horrid language that promotes bad programing.
If you like Python then you'll love VPython - a free add-on that lets you do 3D graphics animation very easily!
Bio-Hazard
Oct12-05, 07:51 PM
My first language was qbasic..
ComputerGeek
Oct12-05, 08:03 PM
My first language was qbasic..
and it is a crappy language.
I wont say its a crappy language. Its just not as versatile as other programming languages. Its a easy language to learn; meant for beginners. Visual BASIC was the first programming language I learnt. I moved on to other languages such as C, and we I look back a BASIC I can see how limited it was, but it certainly was not crappy.
ComputerGeek
Oct12-05, 09:35 PM
I wont say its a crappy language. Its just not as versatile as other programming languages. Its a easy language to learn; meant for beginners. Visual BASIC was the first programming language I learnt. I moved on to other languages such as C, and we I look back a BASIC I can see how limited it was, but it certainly was not crappy.
it is a spaghetti code language. it is crappy. and VB is worse because it attempts to add Object layers to Basic but makes it come out as a kludge.
All computer languages have their merits (and faults) and none of them are perfect. One chooses a language for a particular task based on the goodness of fit between the language and the task at hand as well as one's proficiency with the tools available. I think the computer language wars are rather pointless and silly.
Someone asks for help with a particular language and the response is to bash the language and its implementations?
All computer languages have their merits (and faults) and none of them are perfect. One chooses a language for a particular task based on the goodness of fit between the language and the task at hand as well as one's proficiency with the tools available. I think the computer language wars are rather pointless and silly.
Someone asks for help with a particular language and the response is to bash the language and its implementations?
Here Here!
I have the languages I don't like, but thats only because I find reasonable alternatives for my uses. Nobody writes full scale apps in assembly because its time consuming .. but that would be the most efficient method of code... you have to strike a balance of necessity. If people want to start bashing languages, then I'll go ahead and started opening up some holes .. especially on python lovers.. Object Oriented is good... on paper.. like capitalism, and communism .. in reality it doesn't work well. BASIC was a building block for the IBM .. despite Apple really being a lot more advanced... but.. anyways, BASIC has its uses.. Shell scripting is extremely useful when you use it to do what its meant to do .. same with BASIC.... python is meant to make coding easier for nonfunctional programmers ... at least BASIC doesn't consume nasty amounts of resources just to do something simple .
ComputerGeek
Oct13-05, 08:49 AM
All computer languages have their merits (and faults) and none of them are perfect. One chooses a language for a particular task based on the goodness of fit between the language and the task at hand as well as one's proficiency with the tools available. I think the computer language wars are rather pointless and silly.
Someone asks for help with a particular language and the response is to bash the language and its implementations?
The usefulness of a language doe snot mean that it is not crappy.
fact is that Python fits all the situations that Basic and VB can fit and does a better job.
The usefulness of a language doe snot mean that it is not crappy.
fact is that Python fits all the situations that Basic and VB can fit and does a better job.
Well, good for you! :)
But can you answer the original poster's questions?
Wow what a topic this has turned out to be :yuck:
mewhoexactlywhat
Oct13-05, 03:54 PM
Thank you very much, my questiosn were all answered between this post and my class, except for the question about the line input prompt. Is that the same as input? If not, what is the difference? Thank you for the assistance. P.S. Whether or not QBasic is the best it is generally seen as the best for beginners, in the second semester I'll be learning Visual Basic, and hopefully next year C++. I might try to take additional classes to learn others at some point also. Even if none are perfect, at leats there is plenty of variety.:smile:
Whether or not QBasic is the best it is generally seen as the best for beginners, in the second semester I'll be learning Visual Basic, and hopefully next year C++. I might try to take additional classes to learn others at some point also. Even if none are perfect, at leats there is plenty of variety.
You should watch what you say here because certain people may characterize what you are trying to learn as being inferior and will try discourage you.
mewhoexactlywhat
Oct13-05, 10:04 PM
Well, at my school they only teach QBasic, Visual Baisc, and C++. At the local college I might take some classes, but I think they only offer C++ and Java. Any others I would need to teach myself (if I took up a seperate computer language it would just give me another excuse not to do school work at this point).
ComputerGeek
Oct13-05, 10:08 PM
Well, at my school they only teach QBasic, Visual Baisc, and C++. At the local college I might take some classes, but I think they only offer C++ and Java. Any others I would need to teach myself (if I took up a seperate computer language it would just give me another excuse not to do school work at this point).
no offense, but that program is pretty weak. My program had C++, java, Ada, LISP, Assembly (no particular dialect, it was a theoretical Architecture), PHP, Perl, Javascript, Pascal, and a few others.
your program has to expose you to more languages than that. what are the other courses that you have to take?
ComputerGeek
Oct13-05, 10:15 PM
You should watch what you say here because certain people may characterize what you are trying to learn as being inferior and will try discourage you.
if he is doing it for a class, then it is not that big of an issue, but come on. stop being politically correct about languages. Would you use SnoBol today? How about B? Basic is not a good language give today's choices and methodology, just as SnoBol is not a good language given todays choices and methodology.
mewhoexactlywhat
Oct14-05, 03:18 PM
I'm in high school (also correction: she). It's not a huge school, so there is not much selection with computer langauges. Also, I rechecked and the local college also offers some classes in Perl, Linux, and others. However I'm surprised that there was such a selection at ComputerGeek's school. Are you talking about a high school program or a college program?
ComputerGeek
Oct14-05, 03:59 PM
I'm in high school (also correction: she). It's not a huge school, so there is not much selection with computer langauges. Also, I rechecked and the local college also offers some classes in Perl, Linux, and others. However I'm surprised that there was such a selection at ComputerGeek's school. Are you talking about a high school program or a college program?
did you mention you were in HS? If so, I glanced over that. in any case, I am talking about a college curriculum which was the frame of reference I was coming from when you started this thread.
I am glad to hear that you are a girl because there are so few girls in Computer science as compared to boys. I am also not as critical now that I know it is a High School program because that is actually a very robust program for a high school. They should however dump QBasic and Visual Basic if their intent is to teach programming. I am not trying to sound like a python evangelist, but as far as kids learning to program, Python enforces best practices, is very easy to learn, and is very powerful. so it covers everything that Qbasic and VB do, plus it teaches good programming techniques. I suggest that you pick up a book about python and learn the language if you are interested.
mewhoexactlywhat
Oct14-05, 04:08 PM
I think I'll do that during winter break then. Thank you.:smile:
vBulletin® v3.7.6, Copyright ©2000-2009, Jelsoft Enterprises Ltd.