1 Replies - 143 Views - Last Post: 07 February 2012 - 01:33 AM Rate Topic: -----

Topic Sponsor:

#1 kn336a  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 27-July 09

problem: alphabetizing, calculate average

Posted 07 February 2012 - 12:40 AM

The program I have to write asks us to open a file, alphabetize and calculate the average test score of each student

So far, I have managed to open the file and am able to place information into a separate txt file. The problem I am having is finding out how to manipulate the data within the file once it's open. Not so much write my own data into a txt file.

Also, I was reading some tips on using bubble sort to alphabetize the names within a certain column. I'm not quite sure if thats applicable here.

For example, I am given two columns within the txt file such that:


Student A
marci 1
dan 2
mike3
sam 4

Student B
kelly 2
zack 3
tony 4
rachel 5

I'd like to only alphabetize column A.

My code so far



// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("Students.txt"); //opens txt file
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "File not found"; 

  ofstream outputFile;
  outputFile.open("StudentsNew.txt");
  
  cout << "   " << endl;
  cout << "Writing data to the file" << endl;
  
  //Writing to file
  
outputFile << "Testing!" << endl;
   

  
  //Close file
  outputFile.close();
  cout << "Done" << endl;
  
  cin.get();
  cin.ignore(); 
  return 0;
}



Can someone lead me into the right direction? Any tips would be great! Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: problem: alphabetizing, calculate average

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 62
  • View blog
  • Posts: 1,206
  • Joined: 28-March 09

Re: problem: alphabetizing, calculate average

Posted 07 February 2012 - 01:33 AM

That's good you got a nice little start, but have you tried any type of sort for yourself? In the code above, I see no sorting attempt of any kind...

I would suggest a bubble sort for an intro type of sorting algorithm, simple and gets the job done.

here's a little pseudo to get you started:
func bubSort(a[], size of a)
for(i = 1; i < length; i++)
 if a[i-1] less than a[i]
   swap is true
 end if
 end for
 until there is no more swapping
end



That should get you started...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1