open and input help

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
JTShadow
Newbie
Posts: 2
Joined: Tue Nov 22, 2005 1:57 pm

open and input help

Post by JTShadow »

I'm trying to make a program that opens up a file and reads a name, followed by a number, then followed by a dollar amount but I'm having trouble setting this up, any ideas? Basically i'm trying to run a check off the number and anything that is greater than the number is going to be printed out.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

1) Listen to your teacher
2) Read the textbook (if any)
3) Look in the QB help index
4) Attempt to write the program yourself
5) Go to a forum and ask for help when you're too lazy and wish to work at McDonalds sweeping floors for the rest of your life.
6) Show what you've written so far, and ask people to help you correct it

Now, the correct order to follow is:
1, 2, 3, 4, 6

The wrong order is any which include step 5..


So.. are you lazy, or were you just misstaken and you can infact handle this yourself?
I have left this dump.
Guest

Post by Guest »

1. Teacher doesn't really teach, he walks in the room, tells us what to do, then leaves.
2. No textbook, the course requires VB.NET but the teacher prefers QBASIC and its an intro to computers class.
3. Tried the help file, but the program didn't work.
4. Tried writting it and...
6. This is what I got....

CNT%=0, ACCUM%=0
OPEN "F:/TEST.TXT" FOR INPUT AS 1
DO UNTIL EOF(1)
READ NAM$, SSN$, WAGE!
IF SSN$ > 599999999 THEN
ACCUM%=ACCUM%+WAGE!
END IF
WEND
CLOSE #1
CLS: TOTAL WAGES: ACCUM%
END

Sadly this is due tomorrow, as I have to leave the school earlier in the semester to move across country due to my wifes military orders. I really do appreciate any help, and I'm not lazy about it, just lost.
moneo (login problems)

Re: open and input help

Post by moneo (login problems) »

JTShadow wrote:I'm trying to make a program that opens up a file and reads a name, followed by a number, then followed by a dollar amount but I'm having trouble setting this up, any ideas? Basically i'm trying to run a check off the number and anything that is greater than the number is going to be printed out.
The most important information you need is the format of the the file.
The record, or each record if more than one, consists of 3 fields: name, SSN number, and dollar amount. What is the format, or how do these 3 fields appear in the record? Are the 3 fields fixed length? Otherwise, what is the delimeter used to separate one field from the other?

You need to know exactly how the 3 data fields appear on the record in order to properly be able to read the 3 fields in.

Here's some comments/suggestions on your code:
1) Define your variables at the top of the program. This way you don't need to be putting the % or the ! in the variable name every time you use it. Example:
dim ACCUM as integer
dim WAGE as single

2) Your OPEN statement needs to end with "AS #1".

3) A "DO UNTIL" is terminiated by a "LOOP" not by a "WEND".

4) A "READ" statement is for reading values from a DATA statement. Your want to read form the file opened as #1, so replace the READ with INPUT #1 ....

5) If the SSN is a string, then you need to compare it to a string value of "599999999".

6) If WAGE is a single precision number, and can have decimals, then you cant mix variable types in the arithmetic when you add. Define ACCUM as single, just like WAGE, and then:
ACCUM=ACCUM+WAGE

7) At the end, I think you wanted:
CLS
PRINT "TOTAL WAGES "; ACCUM

8) If you're finished, you don't need to CLOSE the file. It will be closed when the program terminates.

If you don't agree with any of my comments, let's talk about it.
*****
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

What's going on?

I made the above post while I was signed on, and it now shows as GUEST.
*****
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Cookie problems moneo?

I've had some problems staying online too lately.. might have something to do with Pete upgrading the forum, try removing all cookies then log back on..


EDIT: I'm trying to fix up the topic though, removing a few posts..
I'll save them if I remove the wrong ones by misstake.
If anything's missing moneo, let me know
I have left this dump.
JTShadow
Newbie
Posts: 2
Joined: Tue Nov 22, 2005 1:57 pm

Post by JTShadow »

The records are fixed length, the name is 15 characters big. well it looks kinda like this....

"JOHN DOE ","123456789",4256.91

followed by 9 other entries going down the list for a total of 10.

So now the code is looking like....

ACCUM!=0
OPEN "G:/TEST.TXT" FOR INPUT AS #1
CLS
PRINT TAB(10); "Selected Employees (SSN > 599999999)"
Print "Name",,"SSN"," Wage"
Print "----",,"---"," ----"
DO UNTIL EOF(1)
INPUT #1, NAM$, SSN$, WAGE!
IF SSN$ > "599999999" THEN
ACCUM!=ACCUM!+WAGE!
PRINT NAM$, SSN$, WAGE!
END IF
LOOP
PRINT "TOTAL WAGES: ";,,, ACCUM!
CLOSE #1
END

I really do appreciate all the help. It would be nice too if i could change the ssn from the "123456789" format to the "123-45-6789" format, but i have no idea where to start with that... Also, I noticed that the wage fields don't line up right with the decimals places, is there a way to fix that?
Guest

Post by Guest »

By the way, the records are NOT fixed length since NAM$ and WAGE! can vary in length.

To format the SSN$, you could use a PRINT USING, but another simple way is is to break it up into 3 fields separated by a dash. See below.

To format the WAGE!, you need to use PRINT USING. I'll set it up for a max wage of 999,999. See below.

The NAM$ probably varies in length, so I'll set it for a maximum of 30 characters. See below.

Replace "PRINT NAM$, SSN$, WAGE!" with the following:
PRINT LEFT$(NAM$+SPACE$(30),30);
PRINT SPACE$(2);
PRINT LEFT$(SSN$,3)+"-"+MID$(SSN$,4,2)+"-"+MID$(SSN$,6);
PRINT SPACE$(2);
PRINT USING "###,###.##";WAGE!

Do the first test, and then align your heading to coincide with the new format of the printed data fields.
*****
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Log in problems again. Above Guest post is by me (Moneo).
*****
Zam(login problems too!)

Post by Zam(login problems too!) »

I love it, somebody comes and asks a question and everybody automaticaly knows that its for homework. (hehe)
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Z!re wrote:Cookie problems moneo?

I've had some problems staying online too lately.. might have something to do with Pete upgrading the forum, try removing all cookies then log back on..


EDIT: I'm trying to fix up the topic though, removing a few posts..
I'll save them if I remove the wrong ones by misstake.
If anything's missing moneo, let me know
Z!re, thanks for you continued help plus cleaning up the posts.

I log in every single time, so the problem should not be related to cookies.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Post Reply