Results 1 to 15 of 15

Thread: A Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277

    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

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277
    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

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277
    doesn't work

    can you help please

    i'll loose 10 points

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277
    right

    thanks alot

    'am sorry

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277
    any other help please

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277
    when I compile this code
    PHP Code:
    #include <iostream>
    #include <fstream>

    using namespace std;

    int main(int argcchar **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 012; ++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

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277
    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

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width