|
-
Mar 10th, 2002, 02:48 PM
#1
Thread Starter
Hyperactive Member
A Question
I have a C++ Question:
write a program that reads an array of 4000 integer uumbers from a file and saves the total of every 12 elemnts into a new array and a file.
can any 1 help me please
-
Mar 10th, 2002, 03:27 PM
#2
Monday Morning Lunatic
I smell......a homework question 
How are the numbers laid out in the file? Are they actually an array of numbers, or are they one line each, or what?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 10th, 2002, 03:38 PM
#3
Thread Starter
Hyperactive Member
Yes, it's a homework 
suppose that there is a space between every two numbers or if you want every number is on a specfic line
-
Mar 10th, 2002, 03:46 PM
#4
Monday Morning Lunatic
I'll start you off:
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv) {
if(argc < 3) {
cerr << "No filenames given" << endl;
return -1;
}
ifstream input(argv[1]);
if(!input) {
cerr << "Could not open input file: " << argv[1] << endl;
return -1;
}
ofstream output(argv[2]);
if(!output) {
cerr << "Could not open output file: " << argv[2] << endl;
return -1;
}
double vals[12];
int count = 0;
while(!argv.eof()) {
if(count == 12) {
double sum = 0.0;
for(int i = 0; i < 12; ++i)
sum += vals[i];
output << sum << endl;
count = 0;
}
vals[count] << input;
}
return 0;
}
Note that this might not work, I haven't tested it...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 10th, 2002, 03:54 PM
#5
Thread Starter
Hyperactive Member
doesn't work
can you help please
i'll loose 10 points
-
Mar 10th, 2002, 03:56 PM
#6
Monday Morning Lunatic
Oh no, what a pity 
I don't have a compiler here at the moment, so you'll have to bear with me.
In what way doesn't it work? Making an effort at getting it to work will help you learn, which is the whole idea of classes, right?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 10th, 2002, 04:03 PM
#7
Thread Starter
Hyperactive Member
right 
thanks alot
'am sorry
-
Mar 10th, 2002, 04:05 PM
#8
Monday Morning Lunatic
I'll let you off on this one 
Good luck with it...and if you have any problems, check back here!
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 11th, 2002, 12:09 AM
#9
Thread Starter
Hyperactive Member
-
Mar 11th, 2002, 10:23 AM
#10
You said integer numbers, so use int instead of double.
vals[count] << input;
??
Does that actually work parksie?
Do
input >> vals[count];
that is better.
And you must increment count after each read:
input >> vals[count++];
No more help unless you post exactly what compile errors you get.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 11th, 2002, 05:37 PM
#11
Monday Morning Lunatic
1. No idea, nobody ever complained before so I assumed it worked 
2. It's a fair cop (waiting for ADSL then I can get VS6, until then I don't want to mess my sys up with VS5).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 11th, 2002, 05:49 PM
#12
Thread Starter
Hyperactive Member
when I compile this code
PHP Code:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv) {
if(argc < 3) {
cerr << "test.txt" << endl;
return -1;
}
ifstream input(argv[1]);
if(!input) {
cerr << "Could not open input file: " << argv[1] << endl;
return -1;
}
ofstream output(argv[2]);
if(!output) {
cerr << "Could not open output file: " << argv[2] << endl;
return -1;
}
double vals[12];
int count = 0;
26 while(!argv.eof()) {
if(count == 12) {
double sum = 0.0;
for(int i = 0; i < 12; ++i)
sum += vals[i];
output << sum << endl;
count = 0;
}
vals[count] << input;
}
return 0;
}
using visual c++ 6 I got 2 error:
Code:
Compiling...
test.cpp
c:\test.cpp(26) : error C2228: left of '.eof' must have class/struct/union type
c:\test.cpp(26) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
test.obj - 2 error(s), 0 warning(s)
can you change this code to read each line individually
-
Mar 11th, 2002, 05:51 PM
#13
Monday Morning Lunatic
OMG I must have been more hungover than I thought! I can't see how I wrote argv.eof()...that should have been input.eof(), which is the most sensible considering the code that went before it - simple logic normally solves most problems (for example, you can't possibly find the end-of-file for an array ).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 11th, 2002, 05:56 PM
#14
Thread Starter
Hyperactive Member
these is an error on this line vals[count] << input;
Code:
C:\test.cpp(37) : error C2677: binary '<<' : no global operator defined which takes type 'class std::basic_ifstream<char,struct std::char_traits<char> >' (or there is no acceptable conversion)
Error executing cl.exe.
test.obj - 1 error(s), 0 warning(s)
also can you change that code becuase each number is on a specific line
thanks
-
Mar 11th, 2002, 05:59 PM
#15
Monday Morning Lunatic
Swap it round to input >> vals[count++];
The way the streams work, it doesn't make any difference. As far as it considers it, they're separated by an arbitrary amount of whitespace (spaces, tabs, newlines, etc.) - so that same code will work whether they're all on one line separated by spaces, or on separate lines, or any combination.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
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
|