|
-
February 19th, 2009, 06:26 PM
#1
using data in a file
So I am taking an intro to programming course, and I have one problem that I haven't been able to solve. This is using the <fstream> library:
Given a text file, I need my program to use the data in it to perform certain operations. I've figured out how to read and echo the file, but I don't know how to use it to perform calculations. Example, I would like to find the average of the numbers in the lines containing 'M' (for male):
Code:
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
How would I make my program get just the numeric input from the lines with an 'M', so that I can find their average?
-
February 19th, 2009, 10:19 PM
#2
Re: using data in a file
How would I make my program get just the numeric input from the lines with an 'M', so that I can find their average?
1. Read one row from file and store in a CString variable.
2. Use CString::Tokenize to extract 2nd and 4th value to local string variables
3. Compare if 2nd value is 'M', if yes then further convert 4th value to integer
4. Do further calculations here
5. Repeat step 1
Last edited by ckweius; February 19th, 2009 at 10:26 PM.
-
February 19th, 2009, 10:54 PM
#3
Re: using data in a file
Or if you don't want to use CString, have a look at this thread.
-
February 20th, 2009, 03:50 AM
#4
Re: using data in a file
Hi!
What is wrong with strtok ? it always works fine for me.
Regards,
jibeiuvia!
-
February 20th, 2009, 04:13 AM
#5
Re: using data in a file
 Originally Posted by AtillaTheHun
So I am taking an intro to programming course, and I have one problem that I haven't been able to solve. This is using the <fstream> library:
Given a text file, I need my program to use the data in it to perform certain operations. I've figured out how to read and echo the file, but I don't know how to use it to perform calculations. Example, I would like to find the average of the numbers in the lines containing 'M' (for male):
Code:
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
How would I make my program get just the numeric input from the lines with an 'M', so that I can find their average?
Ok, take it easy man! 
if you like fstream, then you have to learn about input stream and output stream,
I'd prefer stringstream of sstream if I were you.
-Antonio
-
February 20th, 2009, 08:08 AM
#6
Re: using data in a file
 Originally Posted by ckweius
1. Read one row from file and store in a CString variable.
2. Use CString::Tokenize to extract 2nd and 4th value to local string variables
3. Compare if 2nd value is 'M', if yes then further convert 4th value to integer
4. Do further calculations here
5. Repeat step 1
This is the non-visual C++ forum.
-
February 20th, 2009, 08:15 AM
#7
Re: using data in a file
For a simple input file like that, you can just read
directly into the appropriate typed variables:
Code:
// assuming the ifstream object is : in
string name , sex , code;
int num;
while (in >> name >> sex >> code >> num)
{
// process the line
}
For more complex cases, reading line by line, placing into a stringstream,
and using it to gte the input is a better way to go (as mentioned in
previous posts).
Also, it is usually easier to do error checking in the line by line method.
Last edited by Philip Nicoletti; February 20th, 2009 at 08:58 AM.
-
February 20th, 2009, 10:48 PM
#8
Re: using data in a file
 Originally Posted by AtillaTheHun
So I am taking an intro to programming course, and I have one problem that I haven't been able to solve. This is using the <fstream> library:
Given a text file, I need my program to use the data in it to perform certain operations. I've figured out how to read and echo the file, but I don't know how to use it to perform calculations. Example, I would like to find the average of the numbers in the lines containing 'M' (for male):
Code:
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
How would I make my program get just the numeric input from the lines with an 'M', so that I can find their average?
You can
use getlline to read each string into a vector of strings and process each line/string in the vector after that.
- I am sorry if my help doesnot help you, I feel sorry for that only. It's my happiness to post help you, mustab man on atilla.
-
February 22nd, 2009, 05:12 AM
#9
Re: using data in a file
 Originally Posted by AtillaTheHun
So I am taking an intro to programming course, and I have one problem that I haven't been able to solve. This is using the <fstream> library:
Given a text file, I need my program to use the data in it to perform certain operations. I've figured out how to read and echo the file, but I don't know how to use it to perform calculations. Example, I would like to find the average of the numbers in the lines containing 'M' (for male):
Code:
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
How would I make my program get just the numeric input from the lines with an 'M', so that I can find their average?
Hello, I write code in C# only, you can
read the file into a string easily
str=File.ReadAllText(filename);
str.Tokenize(provide delimiters[])
then you can check match condition eaiser
You can do it yourself!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|