Hey everyone. I have a project to work on but for some reason I cant get my basics to work. Im trying to read in from a file and just printing what I read in from file to an outfile but its not working. The output looks like this:

3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039

This is my code. Its very basic but for some reason not working. Any suggestions? Thanks.

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int i=0,j=0;
    int const MAXSIZE = 100;
    float data[MAXSIZE];
    
    //Open files for read/write
    ifstream infile("lab7_input.txt");
    ofstream outfile("lab7_output.txt");
    
    for( i = 0; i < MAXSIZE; ++i )
        infile >> data[i];

    for( j = 0; j < MAXSIZE; ++j )
        outfile << data[i] << " " ;
        
    return 0;
}

Your second loop should use j as a subscript for your array, not i

If you wrote for( int i = 0; i < MAXSIZE; ++i ) rather than declaring the variable at the start (in C style), then a newer compiler would have made i go out of scope, and you would have gotten a nice warning about using the i subscript where you shouldn't have.

wow im retarded.. thanks a lot. I knew it was something simple but I didn't see it. Thanks again!

Ok. So I wrote some more code and I got crazy errors. I don't know what to do from here. Any suggestions? Code is below and is well documented to show how it should function.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

    int const MAXSIZE = 100, input = 0;
    float data[MAXSIZE], tmp = 0;
    
    //Open files for read/write
    ifstream infile("lab7_input.txt");
    ofstream mergeoutfile("lab7_mergeout.txt");
    ofstream bubbleoutfile("lab7_bubbleout.txt");    

    //Main loop    
    while(input != 4)
    {
        //Prompt user with operations
        cout << endl << "What would you like to do?" << endl;
        cout << "1: Read data in from file" << endl;
        cout << "2: Sort the data using Bubblesort" << endl;
        cout << "3: Sort the data using Mergesort" << endl;
        cout << "4: Exit" << endl;
        cin >> input;
        cout << endl;
        
        //Perform selected operation
        switch(input)
        {
            case 1://Read data from file into array "data"
                
                for( int i = 0; i <= MAXSIZE-1; ++i )
                    infile >> data[i];
                cout << "The data has been successfully read into the array." << endl;
                break;
            case 2://Sort array using Bubblesort
                for( int j = 0; j <= MAXSIZE-1; ++j )
                {
                    if( data[j] > data[j+1] )
                    {
                        tmp = data[j];
                        data[j] = data[j+1];
                        data[j+1] = tmp;
                    }
                }            
                //Print to outfile "lab07_bubblesort.txt"
                for( j = 0; j <= MAXSIZE-1; ++j )
                    bubbleoutfile << data[j] << " " ;
                
                //Inform user of successful bubblesort
                cout << "The data has been sorted using Bubblesort and" << endl;
                cout << "printed to file  lab7_bubbleout.txt." << endl;
                break;
            /*
            case 3://Sort array using Mergesort
                
                
                //Print to outfile "lab07_mergesort.txt"
                for( int k = 0; k < MAXSIZE; ++k )
                    bubbleoutfile << data[k] << " " ;
                
                //Inform user of successful mergesort
                cout << "The data has been sorted using Mergesort and" << endl;
                cout << "printed to file  lab7_mergeout.txt." << endl;
                break;
            */
            case 4:
                return 0; 
                break;
            default:
                cout << "Please pick a number between 1 and 4" << endl; 
                break;
        }
    }
    return 0;
}

> int const MAXSIZE = 100, input = 0;
Well you need to declare input separately, it thinks it's supposed to be constant.
Which isn't so good when you try and input.

Also, you're missing some 'int' declarations in your for loops.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.