C++ reading,writing,displaying a file?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

C++ reading,writing,displaying a file?

 
0
  #1
Sep 6th, 2005
I am trying to write a program that reads in a file of numbers, writes to a file and displays the numbers in correct order. I don't fully understand how to read the file and pass that information into my class declarations in order to output before and after the swap. Below is my code so far: Help me understand this please?

  1.  
  2.  
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <cmath>
  7. #include <iomanip>
  8. #include <fstream>
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  
  14. class Sort
  15. {
  16. private:
  17. int size;
  18. int *prt;
  19.  
  20. public:
  21. Sort();
  22. void Print();
  23. void Selection_Sort( int arr[], int size);
  24. void Swap ( int &x, int &y);
  25.  
  26. }
  27.  
  28. Sort::Sort
  29. {
  30. int size = 0;
  31. int *ptr = 0;
  32.  
  33. } // end of constructor
  34.  
  35. void Sort::Selection_Sort( int arr[],int size)
  36. {
  37. int min =0;
  38. int check = 0;
  39. int i = 0;
  40.  
  41. for ( check = 0; check < size - 1; check++)
  42. {
  43. min = check;
  44. for ( i = 0; i < size; i++)
  45. if ( arr[i] < arr[min])
  46. min = i;
  47. Swap(arr[check], arr[min]); // call Swap function
  48. } // end of for loop
  49.  
  50. } // end of Selection Sort
  51.  
  52. void Sort::Swap(int &x, int &y)
  53. {
  54. int temp;
  55. temp = x;
  56. x = y;
  57. y = temp;
  58.  
  59. } // end of Swap
  60.  
  61. void Sort::Print(ostream &o, char * list)
  62. {
  63. o << endl;
  64. o << list.Sort;
  65. }
  66.  
  67.  
  68. int main()
  69. {
  70.  
  71. ifstream infile("abc.txt", ios::in) // input file
  72.  
  73. if (!infile)
  74. {
  75. cout << " File could not be opened. " << endl;
  76. exit(1);
  77. }
  78.  
  79. ostream outfile("xyz.txt", ios::out) // output file
  80.  
  81. if (!outfile)
  82. {
  83. cout << " File could not be opened. " << endl;
  84. exit(1);
  85. }
  86.  
  87.  
  88. return 0;
  89.  
  90. }
Attached Files
File Type: cpp sort.cpp (1.2 KB, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: C++ reading,writing,displaying a file?

 
0
  #2
Sep 6th, 2005
either decide whether you are going to use a class and let selection sort sort the held array pointed at by the int* member, in which case you will need a different constructor than a default one and you will also need to think about copying operations as the int* will point to dynamically allocated memory. Or alternatively keep everything as it is but instead use free functions wrapped inside a namespace. There is no point in using a class if theres no data held. Classes consisting of static functions and no data are pretty pointless most of the time.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Re: C++ reading,writing,displaying a file?

 
0
  #3
Sep 6th, 2005
I definitely have to use classes. I am trying to re-write it as we speak. When you say copy, are you referring to strcopy the array.

Thanks for the input.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: C++ reading,writing,displaying a file?

 
0
  #4
Sep 6th, 2005
its not a string. I mean you will need a constructor that can create enough memory to hold the array passed to your class and somehow copy the array to it. a constructor taking an int* and an int seems to fit the bill perfectly. an int member size and an int* member ptr kinda suggests that your teach wants you to use dynamic allocation and make your member funcs act on the object itself and not on passed in params. Remember when dealing with dynamic allocations it is important to consider copying operations carefully and also destruction.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC