943,898 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1546
  • C++ RSS
Dec 9th, 2006
0

Reading in from file

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Dec 9th, 2006
0

Re: Reading in from file

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 9th, 2006
0

Re: Reading in from file

wow im retarded.. thanks a lot. I knew it was something simple but I didn't see it. Thanks again!
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Dec 9th, 2006
0

Re: Reading in from file

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Dec 9th, 2006
0

Re: Reading in from file

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Stripper program!
Next Thread in C++ Forum Timeline: help me ffix this





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC