Putting a Text file in an array and then sorting

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1932 Views - Last Post: 12 December 2010 - 08:23 PM Rate Topic: -----

#1 Guest_pkjjack*


Reputation:

Putting a Text file in an array and then sorting

Posted 12 December 2010 - 11:10 AM

I am getting an error on line 58 of the code below. I am using Dev-C++ to compile and debug the program. Its states the following:
expected unqualified id before "for"\
expected constructor, destructor, or type conversion before '<=' token
expected ',' or ';' before '<=' token
I'm not sure what the problem is. Any help, hints, and or slaps to the back of my head will be greatly appreciated.
Code below.
#include <iostream> // Required for cout.
#include <iomanip> // required for setw.
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
using namespace std;

// Declare constants and function prototype

const int N=100;
double maxVal (const double x[], int n);

int main()
{
    // Declare objects
    int npts=0;
    double y[N], temp;
    string Grades_Fall2010;
    ifstream grades;	 //declare input stream
   // ofstream grades;  //declare output stream
    int count =0, numPts, z; //int z = numPts;
    grades.open("Grades_Fall2010.txt");
    if (grades.fail())
    {
       cout << "Error: Could not find or open file!";
    }
    
//Read a data value from file.
grades >> temp;

// While the array is not full and not end of file assign values to the array.

while ( npts < N && !grades.eof() )
{
    y[npts] = temp;  // Assigns data values to the array
    ++npts;          // Increments inputs
    grades >> temp;  // Inputs next value
}

// Close File
grades.close();
//return 0;
}

// This function sorts an array of N elements in ascending order
void sort (double x[], int n);
 
 // Declare Objects
 int array[100];
 int m;
 int k;
 int j;
 double hold;
 
 // Implements selection sort algorithm
for (int k=0; k<=n-2; ++k)
 {
     // Finds position of smallest value in array beginning at k.
     m = k;
     for ( int j=k+1; j<=n-1; ++j)
     {
         if (x[j] < x[m])
         m = j
         }
         // Exchanges the value with the smallest value at k.
         hold = x[m];
         x[m] = x[k];
         x[k] = hold;
         }
         
         // Void Return.
         return;
         }
         
    

//system ("pause");
return 0;                      
}

ifstream MyFile("Grades_Fall2010.txt");

//int array[100];
int x = 0;
int i = 0;
while (MyFile >> x); //puts data from MyFile into y
          {
              array[i] = x; // puts value of y into array
               i++;
          }  
for(int j=0;j<i;j++)
          {
              cout<<array[j]<<endl; 
          }



Is This A Good Question/Topic? 0

Replies To: Putting a Text file in an array and then sorting

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 11:21 AM

why have you declared k twice?
Was This Post Helpful? 0
  • +
  • -

#3 pkjjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 02-August 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 11:33 AM

View Postsk1v3r, on 12 December 2010 - 10:21 AM, said:

why have you declared k twice?

I added the int j; and int k; at lines 53 and 54 just to see if it would change any symptoms or call outs. I have since removed them. Same results from the Dev-C++ compiler.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 11:43 AM

On line 48 is this a function prototype or the actual function?

// This function sorts an array of N elements in ascending order
void sort (double x[], int n);

 // Declare Objects


Jim
Was This Post Helpful? 0
  • +
  • -

#5 pkjjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 02-August 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 11:50 AM

It is the function, but if I get rid of the semicolon at the end I get a compiler callout at int array[100]:
expected int declarator before "int"
expected ',' or ';' before "int"
expected unqualified-id before"for"
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 11:55 AM

Did you add some {} around your function?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 pkjjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 02-August 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 12:00 PM

Jim,

No I did not add curly braces. I don't believe I am supposed to. Am I wrong there?

Thanks,
Paul
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 12:05 PM

See this link program structure and this Functions I and you tell me.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 pkjjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 02-August 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 12:16 PM

Jim,

Understand,
colon removed after function prototype. Curly brace added before object declaration. Extraneous return 0; and extra curly brace removed. Work moves on. I'm sure I will need more knocks about the head and shoulders and Thanks!

Paul
Was This Post Helpful? 0
  • +
  • -

#10 pkjjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 02-August 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 12:37 PM

I did a search on the DreaminCode site for making a txt file from the sorted array but all I see is txt file into an array. I also did a Google search for it and found just txt file into array and not array into txt file. Any thoughts?

Thanks,
Paul
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 12:43 PM

Check out this link: Input/output with files.

You should also search for "file input/output in C++".

You may also want to bookmark this site: cplusplus.com tutorials.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 pkjjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 02-August 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 06:04 PM

This program is kind of trivial but it is big too me. I am breaking it into blocks as I go. Right now the code below gives me no output. The compiler has no errors but the Maximum Value is not displayed actually nothing is displayed. The file referenced is found and has data in it. I've tried many variants but no joy. It will in the end have standard deviation, grades sorted from highest to lowest, max score, min score, avg score etc. Code below:
#include <iostream> // Required for cout.
#include <iomanip> // required for setw.
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
using namespace std;

// Declare constants and function prototype pg 261.

const int N=100;
double maxval (const double x[], int n);

int main()
{
    // Declare objects
    int npts=0;
    double y[N], temp;
    string Grades_Fall2010;
    //string Grades_Sorted_Fall2010;
    ifstream grades;	 //declare input stream
    //ofstream sorted;  //declare output stream
    int count =0, numPts, z; //int z = numPts;
    grades.open("Grades_Fall2010.txt");
    if (grades.fail())
    {
       cout << "Error: Could not find or open file!";
    }
    exit (1);
    
//Read a data value from file.
grades >> temp;

// While the array is not full and not end of file assign values to the array.

while (npts < N && !grades.eof() )
{
    y[npts] = temp;  // Assigns data values to the array
    ++npts;          // Increments inputs
    grades >> temp;  // Inputs next value
}

// Find and Print Maximum Value.
cout << "Maximum Value: " << maxval (y,npts) << endl;

// Close File and Exit Program.
grades.close();
return 0;
}

// This function Returns the Maximum Value in the Array x With n Elements.
double maxval (const double x[], int n)
{
// Declare Local Objects.
       double maxVal;

// Determine the Maximum Value in the Array.
maxVal= x[0];
for (int k=1; k<n; ++k)
{
    if (x[k] > maxVal)
    maxVal = x[k];
}
//Return Maximum Value.
return maxVal;
}
       
       

// This function sorts an array of N elements in ascending order
void sort (double x[], int n)

// Declare Objects
{
 int m;
 double hold;

 // Implements selection sort algorithm
for (int k=0; k<=n-2; ++k)
 {
     // Finds position of smallest value in array beginning at k.
     m = k;
     for ( int j=k+1; j<=n-1; ++j)
     {
         if (x[j] < x[m])
         m = j;
         }
         // Exchanges the value with the smallest value at k.
         hold = x[m];
         x[m] = x[k];
         x[k] = hold;
                 }
         
        
         // Void Return.
         return;
         }


Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 06:13 PM

In the following code:

#include <iostream> // Required for cout.
#include <iomanip> // required for setw.



You should not put anything on the same line as an include statement except the file to be included.

    if (grades.fail())
    {
       cout << "Error: Could not find or open file!";
    }
    exit (1);




In the above code you have the exit outside the if statement therefore you will always be exiting. Also instead of "exit(1)" I would suggest just "return(1)". Exit does not always call your class destructors.

And lastly instead of using "grades.fail()" for your test condition you can just use:
if(!grades)


Jim
Was This Post Helpful? 0
  • +
  • -

#14 pkjjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 02-August 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 06:47 PM

Jim,
Made all suggested changes and results the same. "Press any key to continue..." I'm working it boss. Just no joy. Any other thoughts other than I'm a dumb ass...lol!

Thanks,
Paul
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Putting a Text file in an array and then sorting

Posted 12 December 2010 - 06:52 PM

Since you made some changes post the modified code.


Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2