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
Printable View
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
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? :)
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
I'll start you off:Note that this might not work, I haven't tested it...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;
}
doesn't work
can you help please
i'll loose 10 points :( :(
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?
right :(
thanks alot
'am sorry
I'll let you off on this one :)
Good luck with it...and if you have any problems, check back here!
any other help please
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.
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).
when I compile this code
using visual c++ 6 I got 2 error: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;
}
can you change this code to read each line individuallyCode: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)
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 ;)).
these is an error on this line vals[count] << input;
also can you change that code becuase each number is on a specific lineCode: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)
thanks
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.