Read input from a file and loop it
-- :)
Thank you
# 1 Re: Read input from a file and loop it
What is the point of this empty while loop?
while (gender == m)
{
}
# 2 Re: Read input from a file and loop it
What about this?
if (gender == 'm' ) // if gender is male
inData >> gender >> gpa;
else if (gender == 'f') // if gender is female
inData >> gender >> gpa;
if needed at all, this is better:
if (gender == 'm' || gender == 'f')
inData >> gender >> gpa;
Laitinen
# 3 Re: Read input from a file and loop it
Thank you,
so for the while loop would it look like this?
while (m <= 1)
{
inData >> gender >> gpa;
if (gender == 'm' ) // if gender is male
inData >> gender >> gpa;
else if (gender == 'f') // if gender is female
inData >> gender >> gpa;
}
# 4 Re: Read input from a file and loop it
How does the file input.txt look like? And what do you want to do with it?
# 5 Re: Read input from a file and loop it
The input file is this http://geocities.com/lifeis2evil/input.txt and I need the program to read all the m and find the average if a user picks m or if the user picks f then do the same with the f numbers
# 6 Re: Read input from a file and loop it
Let me understand your requirement.
If the user enters f as gender, then you want to calculate the average of all numbers in input.txt written next to f. Likewise, if the user enters m as gender, then you want to calculate the average of all numbers in input.txt written next to m.
Let me know if this is what you want.
Also, let me know what is the use of output.txt.
# 7 Re: Read input from a file and loop it
This reads the file and calculate the average depending on what the gender is:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream inData;
int count = 0;
double value = 0.0;
double average = 0.0;
double sum = 0.0;
char currentGender;
char gender = 'm';
inData.open("input.txt");
while(!inData.eof())
{
inData >> currentGender >> value;
if(currentGender == gender)
{
sum +=value;
count++;
}
}
average = sum/count;
return 0;
}
# 8 Re: Read input from a file and loop it
If you want to calculate average gpa for a given gender, then replace your while loop with the following:-
NoOfSamples = 0;
TotalGpa = 0;
while(! inData.eof())
{
inData >> genderInFile >> gpa;
if(gender == genderInFile)
{
NoOfSamples++;
TotalGpa += gpa;
}
}
average = TotalGpa/NoOfSamples;//this gives average gpa for the given gender
Note:- You have to declare genderInFile as char, TotalGpa as double and NoOfSamples as int.
# 9 Re: Read input from a file and loop it
Hi laitinen,
It seems we both were typing together.
In your code, inside while loop you have written if and else if blocks. The code inside both of these are the same. Even the condition in if is the same as the condition in else.
So, do you need both of these? I think only if is needed.
# 10 Re: Read input from a file and loop it
In your code, inside while loop you have written if and else if blocks. The code inside both of these are the same. Even the condition in if is the same as the condition in else.
So, do you need both of these? I think only if is needed.
Of course, I have already edited the code, I was a little too fast.
Regarding your code, will this compile:
while(! eof(inData))
{
//...
}
Shouldnt this be
while(! inData.eof())
{
//...
}
Best regards,
Laitinen
# 11 Re: Read input from a file and loop it
Yes, it should be
inData.eof()
Got confused between eof of FILE pointer and that of ifstream.
# 12 Re: Read input from a file and loop it
Shouldnt this be
while(! inData.eof())
{
//...
}
Best regards,
Laitinen
I have found whenever I use the above method, it runs the last line of the input file TWICE. This is because inData.eof() doesn't return false until AFTER it makes a call that is false.
Here for a better worded example I found:
end-of-file is only indicated *after* an input routine has
tried to read, and failed. (In other words, C's I/O is not like
Pascal's.) Usually, you should just check the return value of
the input routine -- fgets(), for example, returns NULL on end-
of-file. In virtually all cases, there's no need to use feof()
at all.
So it may be better to do something like this:
while (!ins.get(inData, 256) == NULL)
{
//..
}
# 13 Re: Read input from a file and loop it
I have found whenever I use the above method, it runs the last line of the input file TWICE. This is because inData.eof() doesn't return false until AFTER it makes a call that is false.
Then I think you have done something wrong. My code works as expected, it does not read the last line twice.
Laitinen
# 14 Re: Read input from a file and loop it
Let me understand your requirement.
If the user enters f as gender, then you want to calculate the average of all numbers in input.txt written next to f. Likewise, if the user enters m as gender, then you want to calculate the average of all numbers in input.txt written next to m.
Let me know if this is what you want.
Also, let me know what is the use of output.txt.
Yes that is correct :) . the output file works fine, its just making a file on the drive with the info the person writes :)
# 15 Re: Read input from a file and loop it
Then I think you have done something wrong. My code works as expected, it does not read the last line twice.
Laitinen
From what I have read it is kind of "hit-or-miss" with using that.
So to be safe it is a good idea to use the way I posted...even if it is now working.
# 16 Re: Read input from a file and loop it
while (!ins.get(inData, 256) == NULL)
{
//..
}
Do you mind posting code that can be compiled? It dont understand what you want to do here.
Laitinen
# 17 Re: Read input from a file and loop it
-- :)
Thank you
# 18 Re: Read input from a file and loop it
-- :)
Thank you
# 19 Re: Read input from a file and loop it
oh and I'm using Dev-C++ compiler :)
# 20 Re: Read input from a file and loop it
-- :)
Thank you
# 21 Re: Read input from a file and loop it
If it looks right now all I need to do is get it to calculate the average of m if the user picks m and display it to the screen. or if the user picks f calculate the average and display it to the screen :) which I have no idea how to do lol and its the hardest part since I suck at math.. helps :p
# 22 Re: Read input from a file and loop it
Do you mind posting code that can be compiled? It dont understand what you want to do here.
Laitinen
This code can be compiled.
I have it compiled just fine.
inData must be of type Char []
void GetInput(ifstream& ins, ofstream& outs)
{
char input [256];
while (!ins.get(input, 256) == NULL)
{
//..
}
And what it does is the same thing as his while loop to read to end of file...only more reliable.
Whenever it tries to read the file and gets NULL (End of file) it will jump out of the loop. It tests for NULL before it runs whats inside. If you use !eof then it can sometimes run the last line of the input file twice.
I have had this happen.
# 23 Re: Read input from a file and loop it
-- :)
Thank you
# 24 Re: Read input from a file and loop it
-- :)
Thank you
# 25 Re: Read input from a file and loop it
With the current input file which you showed us, your program will ALWAYS display the average for female. Think about it, when you ask the user what gender he/she is you stores this in a variable called gender. Then when you read in the contents from file, you overwrite this variable, hence the last line in your file is a female. Now when you want to write out the average, your program has 'f' in gender.
Your solution would be to create a new variable which you use for the gender read in from file. You can call it genderFromFile for instance. You will use that variable in your last while loop. Now when you want to display the average, you will not have overwritten the gender the user typed in!
Also shouldn't "average" be of type double?
Laitinen
# 26 Re: Read input from a file and loop it
-- :)
Thank you