Reading in from file

Reply

Join Date: Oct 2006
Posts: 165
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Reading in from file

 
0
  #1
Dec 9th, 2006
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.

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int i=0,j=0;
  10. int const MAXSIZE = 100;
  11. float data[MAXSIZE];
  12.  
  13. //Open files for read/write
  14. ifstream infile("lab7_input.txt");
  15. ofstream outfile("lab7_output.txt");
  16.  
  17. for( i = 0; i < MAXSIZE; ++i )
  18. infile >> data[i];
  19.  
  20. for( j = 0; j < MAXSIZE; ++j )
  21. outfile << data[i] << " " ;
  22.  
  23. return 0;
  24. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,603
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 854
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Reading in from file

 
0
  #2
Dec 9th, 2006
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 165
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: Reading in from file

 
0
  #3
Dec 9th, 2006
wow im retarded.. thanks a lot. I knew it was something simple but I didn't see it. Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 165
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: Reading in from file

 
0
  #4
Dec 9th, 2006
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.

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. int const MAXSIZE = 100, input = 0;
  10. float data[MAXSIZE], tmp = 0;
  11.  
  12. //Open files for read/write
  13. ifstream infile("lab7_input.txt");
  14. ofstream mergeoutfile("lab7_mergeout.txt");
  15. ofstream bubbleoutfile("lab7_bubbleout.txt");
  16.  
  17. //Main loop
  18. while(input != 4)
  19. {
  20. //Prompt user with operations
  21. cout << endl << "What would you like to do?" << endl;
  22. cout << "1: Read data in from file" << endl;
  23. cout << "2: Sort the data using Bubblesort" << endl;
  24. cout << "3: Sort the data using Mergesort" << endl;
  25. cout << "4: Exit" << endl;
  26. cin >> input;
  27. cout << endl;
  28.  
  29. //Perform selected operation
  30. switch(input)
  31. {
  32. case 1://Read data from file into array "data"
  33.  
  34. for( int i = 0; i <= MAXSIZE-1; ++i )
  35. infile >> data[i];
  36. cout << "The data has been successfully read into the array." << endl;
  37. break;
  38. case 2://Sort array using Bubblesort
  39. for( int j = 0; j <= MAXSIZE-1; ++j )
  40. {
  41. if( data[j] > data[j+1] )
  42. {
  43. tmp = data[j];
  44. data[j] = data[j+1];
  45. data[j+1] = tmp;
  46. }
  47. }
  48. //Print to outfile "lab07_bubblesort.txt"
  49. for( j = 0; j <= MAXSIZE-1; ++j )
  50. bubbleoutfile << data[j] << " " ;
  51.  
  52. //Inform user of successful bubblesort
  53. cout << "The data has been sorted using Bubblesort and" << endl;
  54. cout << "printed to file lab7_bubbleout.txt." << endl;
  55. break;
  56. /*
  57.   case 3://Sort array using Mergesort
  58.  
  59.  
  60.   //Print to outfile "lab07_mergesort.txt"
  61.   for( int k = 0; k < MAXSIZE; ++k )
  62.   bubbleoutfile << data[k] << " " ;
  63.  
  64.   //Inform user of successful mergesort
  65.   cout << "The data has been sorted using Mergesort and" << endl;
  66.   cout << "printed to file lab7_mergeout.txt." << endl;
  67.   break;
  68.   */
  69. case 4:
  70. return 0;
  71. break;
  72. default:
  73. cout << "Please pick a number between 1 and 4" << endl;
  74. break;
  75. }
  76. }
  77. return 0;
  78. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,603
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 854
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Reading in from file

 
0
  #5
Dec 9th, 2006
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Reply

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




Views: 1425 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC