Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,836 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,638 people online right now. Registration is fast and FREE... Join Now!




reading in a file

 
Reply to this topicStart new topic

reading in a file

camckee316
2 Oct, 2008 - 07:36 PM
Post #1

New D.I.C Head
*

Joined: 29 Aug, 2008
Posts: 44


My Contributions
I am having a little trouble reading in a file. I thought i had the code right but i guess not. My output is not the same as my text file.
CODE

//MODE vs MEDIAN  pg.632
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

//int findMode(int *,int);
//double findMedian(int *,int);
//void arraySort(int *,int);

int main()
{  
    
    ifstream file1; //used to read from data file numbers_1
//    ifstream file2; //used to read from data file numbers_2
//    ifstream file3; //used to read from data file numbers_3
    const int num_commuters=30; //number of comuters
    int mile[num_commuters];
    int count;

//        
//    cout <<"                        Medians    :      Modes" << endl;
//    cout <<"                        _________________________" << endl;
//    cout <<"File 1 numbers_1.txt" <<"               :           " << endl;
//    cout <<"File 2 numbers_2.txt" <<"               :           " << endl;
//    cout <<"File 3 numbers_3.txt" <<"               :           " << endl;  
//    cout << endl;
//    cout <<"Average Median: " << endl;
//    cout <<"Average Mode: " << endl <<endl;
//    cout <<"     Orignal List           :            Sorted List" << endl;
//    cout <<"__________________________________________________________" << endl;
//    cout <<"File 1    File 2    File 3  :  File 1    File 2    File 3" << endl;
//    cout <<"______    ______    ______  :  ______    ______    ______" << endl;

    //open file
    file1.open("numbers_1.txt");
    if (!file1)
      cout <<"Error opening file\n";
    else
    {
      for (count=0; count < num_commuters; count++)
         file1 >> mile[num_commuters];
      file1.close();
      
      //display output
      cout <<"this is your file" << endl;
      for(count =0; count < num_commuters; count++)
      {
         cout << count +1 << ": ";
         cout << mile[num_commuters] << endl;          
      }
    }    
              
    system ("pause");
    return 0;
}
////******************
////Sorts the numbers*
////******************
//void arraySort()
//{
//    
//}
////************************
////Finds the Mode of array*
////************************
//int findMode()
//{
//    
//}
////**************************
////Finds the Median of array*
////**************************
//int findMedian()
//{
//    
//}                

i know there are other things in here as i am currently working on them but my objective now is to read the file in correctly. Could anyone be so kind to point out my error? i have attached the file that is to be read in. My output consists of 39 and that is it. Attached File  numbers_1.txt ( 250bytes ) Number of downloads: 4

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Reading In A File
2 Oct, 2008 - 09:02 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 218 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Instead of mile[num_commuters] you want mile[count] in both of your loops. Remember that count is the variable that changes in the loop so you want that variable to refer to the subscripts of the array. Using num_commuters was only accessing subscript 30 of the array and nothing else because it didn't change value.

So once you change over to using the "count" variable in your array you will see it working just fine. Remember to change it in both of your for loops. Both the reading of the file into the array and in the loop of reading the array and printing to screen.

Good luck.

"At DIC we be file array loading code ninjas... we are also loaded on jack daniels. It makes us better coders!" decap.gif
User is online!Profile CardPM
+Quote Post

camckee316
RE: Reading In A File
3 Oct, 2008 - 01:17 PM
Post #3

New D.I.C Head
*

Joined: 29 Aug, 2008
Posts: 44


My Contributions
It was suppose to be to a pointer not an array. That is my mistake. However I did get it read in the file and display the output correct. Now I am sorting the numbers, and I run across this error "99 non-lvalue in assignment " when compiling. I am using the most recent up to date version of Dev-C++, I have tried google to find some meaning and come up empty handed. Any here is the code:
CODE

//MODE vs MEDIAN  pg.632
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void sortArray1(int *, int );
void showArray1(int *, int );

int main()
{  
    ifstream file1; //used to read from data file numbers_1
//    ifstream file2; //used to read from data file number_2
//    ifstream file3; //used to read from data file number_3
    const int num_commuters=50; //number of comuters
    int mile1[num_commuters];
//    int mile2[num_commuters];
//    int mile3[num_commuters];
    int count;

    //open file 1
    file1.open("numbers_1.txt");
    if (!file1)
      cout <<"Error opening file\n";
    else
    {
      for (count=0; count < num_commuters; count++)
         file1 >> *(mile1+count);
        
    //open file 2
//        file2.open("numbers_2.txt");
//    if (!file2)
//      cout <<"Error opening file\n";
//    else
//    {
//      for (count=0; count < num_commuters; count++)
//         file1 >> *(mile2+count);
//
        
//    //open 3    
//        file3.open("numbers_3.txt");
//    if (!file3)
//      cout <<"Error opening file\n";
//    else
//    {
//      for (count=0; count < num_commuters; count++)
//         file1 >> *(mile3+count);
      
      //close all files        
      file1.close();
//     file2.close();
//      file3.close();
      
      //display output
    
    }
//table
     cout <<"                        Medians    :      Modes" << endl;
    cout <<"                        _________________________" << endl;
    cout <<"File 1 numbers_1.txt" <<"               :           " << endl;
    cout <<"File 2 numbers_2.txt" <<"               :           " << endl;
    cout <<"File 3 numbers_3.txt" <<"               :           " << endl;  
    cout << endl;
    cout <<"Average Median: " << endl;
    cout <<"Average Mode: " << endl <<endl;
    cout <<"     Orignal List           :            Sorted List" << endl;
    cout <<"__________________________________________________________" << endl;
    cout <<"File 1    File 2    File 3  :  File 1    File 2    File 3" << endl;
    cout <<"______    ______    ______  :  ______    ______    ______" << endl;  
   //see out file 1
    for(count =0; count < num_commuters; count++)
      {
        cout << "  " << *(mile1+count) << endl;
        
      }
    

    system ("pause");
    return 0;
}
//}
//}
//*****************
//sort the pointer*
//*****************
void sortArray1(int *mile1, int size)
{
     int temp;
     bool swap;
    
     do
     {
          swap = false;
          for (int count = 0; count < (size -1); count++)
          {
            if(*mile1+count > *mile1+(count+1))
            {
                temp = *mile1+count;
                *mile1+count = *mile1+(count + 1);
                *mile1+(count + 1) = temp;
                 swap = true;                  
            }          
          }
     }
     while (swap);
}
//*****************
//show the pointer*
//*****************
void showArray(int *mile1, int size)
{
  for (int count = 0; count < (size-1); count++)
      cout << *mile1+count << endl;    
}



I am to read 3 file but am only working 1 file at time. My apologies to all of the coded out code. Where did I mess up at? Please leave thoughts and comments. Thanks


User is offlineProfile CardPM
+Quote Post

David W
RE: Reading In A File
4 Oct, 2008 - 07:46 AM
Post #4

D.I.C Regular
Group Icon

Joined: 20 Sep, 2008
Posts: 315



Thanked: 16 times
Dream Kudos: 275
My Contributions
// a little 'head start' .... biggrin.gif

CODE
//MODE vs MEDIAN  pg.632

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

const int num_commuters=50; //50=MAX number of commuters

void sortArray( int [], int );
void showArrays( int [], int [], int );

int main()
{  
    ifstream file1;    //used to read from data file numbers_1
//    ifstream file2;  //used to read from data file number_2
//    ifstream file3;  //used to read from data file number_3

    int mile1[num_commuters];    //to hold an a array with a maximum of 50 elements
    int mile1cpy[num_commuters]; //to hold an unsorted copy ...
//    int mile2[num_commuters];
//    int mile3[num_commuters];

    int count =0;
    //open file 1
    // I made up a file called 'toSort.txt'
    // with the following content:
    // 23 33 32 -12 34 66 66 -9 10
    file1.open("toSort.txt");
    if (!file1)
      cout <<"Error opening file\n";
    else
      while( file1 >> mile1[count] ) { mile1cpy[count]=mile1[count]; ++count; }
        
    //open file 2
//        file2.open("numbers_2.txt");
//    if (!file2)
//      cout <<"Error opening file\n";
//    else
//    {
//      for (count=0; count < num_commuters; count++)
//         file1 >> *(mile2+count);
//
        
//    //open 3    
//        file3.open("numbers_3.txt");
//    if (!file3)
//      cout <<"Error opening file\n";
//    else
//    {
//      for (count=0; count < num_commuters; count++)
//         file1 >> *(mile3+count);
      
      //close all files        
      file1.close();
//     file2.close();
//      file3.close();
      
      //display output
    
//table
    cout <<"                        Medians    :      Modes" << endl;
    cout <<"                        _________________________" << endl;
    cout <<"File 1 numbers_1.txt" <<"               :           " << endl;
    cout <<"File 2 numbers_2.txt" <<"               :           " << endl;
    cout <<"File 3 numbers_3.txt" <<"               :           " << endl;  
    cout << endl;
    cout <<"Average Median: " << endl;
    cout <<"Average Mode: " << endl <<endl;
    cout <<"     Orignal List           :            Sorted List" << endl;
    cout <<"__________________________________________________________" << endl;
    cout <<"File 1    File 2    File 3  :  File 1    File 2    File 3" << endl;
    cout <<"______    ______    ______  :  ______    ______    ______" << endl;  
    
    
    sortArray( mile1, count );
    showArrays( mile1cpy, mile1, count );
    
    cout << endl << endl;
    system ("pause");
    return 0;
}
//}
//}
//***************
//sort the Array* *** RECALL: Arrays are ALWAYS passed by reference! ***
//***************
void sortArray( int mile[], int size ) // a bubble sort
{
     int temp, i, j;
     bool swap;
     i = 0;
     do
     {
          swap = false;
          for( j = 1; j < size-i; ++j )
          {
            if( mile[j-1] > mile[j] ) // then swap
            {
                temp = mile[j];
                mile[j] = mile[j-1];
                mile[j-1] = temp;
                swap = true;                  
            }          
          }
          ++i;
     }
     while(swap);
}
//****************
//show the arrays*
//****************
void showArrays( int milecpy[], int mile[], int size )
{
  for( int i = 0; i < size; ++i )
      cout << setw(6)<< milecpy[i] << setw(31) << mile[i] << endl;    
}


This post has been edited by David W: 4 Oct, 2008 - 07:52 AM
User is offlineProfile CardPM
+Quote Post

camckee316
RE: Reading In A File
4 Oct, 2008 - 04:55 PM
Post #5

New D.I.C Head
*

Joined: 29 Aug, 2008
Posts: 44


My Contributions
Thanks smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 04:13PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month