Need help on Reading from a file

This is a discussion on Need help on Reading from a file within the C++ Programming forums, part of the General Programming Boards category; Ok I am suppose to do this:
For research purposes and to better help students, the admissions office of your ...

Need help on Reading from a file

Ok I am suppose to do this:
For research purposes and to better help students, the admissions office of your local university wants to know how well female and male student GPAs for certain courses. Due to confidentiality, the letter code f is used for fimale students and m for male students. Every file entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries in the file is unknown. Write a program that computes and uptputs the average GPA for both females and male students. Format your results to two decimal places.

is there any way to make the calculations in the input.txt file and then read it from the file. Its suppose to add all the m scores and give an average if they answer m and add all the f and give the average it they answer f.

I'm 100% stuck and have no idea how to do this >_< I've tried it different ways but it's still wrong pleasee helps

Your code doesn't seem to match the problem statement at all. The problem doesn't mention anything about getting input from the user or worrying about majors and stuff. Are you sure that's not just leftover code from a previous assignment?

The first step would be to start with an empty program. Add an empty main() function. Then add code to open an input file and verify that it opened (print an error message if the open fails). Once you compiled and tested that and it finds your input file correctly, you can move on to worrying about reading from the file.

I would start by reading the data from the file at writing it to cout. To read the data from the file you will need two variables, one for the gender and one for the GPA. That's your hint, see if you can get that far.

Your code doesn't seem to match the problem statement at all. The problem doesn't mention anything about getting input from the user or worrying about majors and stuff. Are you sure that's not just leftover code from a previous assignment?

The first step would be to start with an empty program. Add an empty main() function. Then add code to open an input file and verify that it opened (print an error message if the open fails). Once you compiled and tested that and it finds your input file correctly, you can move on to worrying about reading from the file.

I would start by reading the data from the file at writing it to cout. To read the data from the file you will need two variables, one for the gender and one for the GPA. That's your hint, see if you can get that far.

That is part of the work , I am suppose to have:
o output to the screen.
o output to file (write to a file)
o input from the keyboard.
o input from a file (read from a file)
o use and declare variables
o have at least one assignment statement
o have at least one calculation
o format your output by using set(w)
o format your number to 2 decimal places and show .00
o include a form of selection (i.e. if, if...else, switch)
o include a form of repetition (i.e. loops)

Ok, so even with the other stuff in there, you need to check to see if the input file was opened correctly. You can use inData.is_open() for that.

Then you need to read in the data. Your current code looks like this:

Code:

inData >> m >> f;

There are two problems with that. First, a line of data has a gender and a GPA. You are reading into m and f. You want the variables you use there to match what you are trying to read in. Since the gender is a char and the GPA is a double, you should probably have a char variable for the gender and a double variable for the GPA. Right?

So add that code and get it working. You can test that it works by outputting it immediatly to cout (only for testing, you'd remove that output code after you know it works).

The second problem is that you only read one line. You need to loop while the read is successful. The expression inData >> x evaluates to true if it is successful, so you can put that into the control of your loop. This way you read in each line.

Once you have those two things working, then you can worry about how you're going to use the data you read in to get the averages.

So something like this or is my code wrong? I know to do somewhat but I have no idea what the code is suppose to look like:

I am suppose to do a while loop which would open the inData file read each line if its a m and keep looping or if its a f it would open the inData file read 1 line at a time and keep looping for the f right? Then it would need to close the loop and add the average of whichever the person picks whethere m or f

That looks good. The next step I'd suggest is to put that in a loop so that you loop through each line in file. As it is, you are only reading one line.

Code:

inData.is_open();

That does nothing. The point of using is_open() was for you to use the return value to verify that the file is open. You should figure out how to do that.

Code:

while (gender == m)

If you want to test the gender to see if it is m or f, you need to put the character in quotes:

Code:

if (gender == 'm') // if gender is male

Code:

if (cout == m)

This and the else below it do nothing. Don't leave extra random stuff in the file. Only put code in there that you know what it is supposed to do and why you have it in there.

Once you get your loop working so that you are reading in all lines of the file, then you can figure out what to do when gender is 'm' and when it is 'f'. I think maybe the first step would actually be to find the average of all gpas, and ignore the gender. If you can output the average of all gpas, then you can go back and separate the male and female gpas into separate averages.

Well Daved still had a pretty good suggestion, let's break down the steps we need to take to get there.

Originally Posted by Daved

I would start by reading the data from the file at writing it to cout. To read the data from the file you will need two variables, one for the gender and one for the GPA. That's your hint, see if you can get that far.

I honestly think that if you start out like Daved suggested, then you will be better prepared to finish your homework on your own. Okay, well we know that in order to read a file we need:
* a stream object that works
* a place to store the information
* means to get that information
* to understand that cout prints to the console (by default)

Let's not build the whole program at once.

Getting a stream object to work is actually very easy. For example, with ifstream, all you need to do is open() something. But since opening a file changes the state of the stream, you cannot just assume that the stream is still working; occassionally, people will make typos and you will attempt to open something that isn't there. Gone undetected, this can break the program if you try to read any data. Since open() is an operation that can fail for most streams and break them, the is_open() function was written and standardized to protect you.

Declaring a variable, I hope, is also easy for you. You just need to make sure that you correctly store the data you received from a source by using the correct types of variables. You also know how cout and the stream insertion operators ( >> and << ) work.

Collecting all the data from a stream source implies that you also need to loop input operations. Given that you understand the tools you need to use, the task is at least that much simpler. Or parent site, www.cprogramming.com, has a lot of tutorials if you need to relearn a part of the language, like if-else statements or looping.

All that's left to do after you're correctly reading and writing to the files you need, is to process the data you collected. Try tallying all the scores if someone is a boy. Then tally the scores of boys and girls at the same time. Finally, calculate and print the output.

and make it into a loop. Don't worry about 'm' or 'f' for now. Just read the data in a loop so that you get all the lines from the file. Remember, you want to loop while the read succeeds (which is while the line of code above succeeds).

I really need help on this. I have to present it tomorrow and this is the only part I'm stuck on. can someone please give me the code / help me with it, so that I can make it work and then I can learn it. Please it is really important that I get this to work, I tried going to my prof. in office hours but he wasn't there and half the people I know in that class are lost too because she didn't really teach is this stuff well. Please Help