Input from file issues

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

Join Date: Oct 2009
Posts: 8
Reputation: kingcrim05 is an unknown quantity at this point 
Solved Threads: 0
kingcrim05 kingcrim05 is offline Offline
Newbie Poster

Input from file issues

 
0
  #1
27 Days Ago
I don't get what is happening here. I used this exact code for another algorithm and it works but here i only get large negative numbers for an input, what gives?

My output is:
floatArray[0] is -107374176.000000
floatArray[1] is -107374176.000000
floatArray[2] is -107374176.000000
floatArray[3] is -107374176.000000
floatArray[4] is -107374176.000000
floatArray[5] is -107374176.000000
floatArray[0] is -107374176.000000
floatArray[1] is -107374176.000000
floatArray[2] is -107374176.000000
floatArray[3] is -107374176.000000
floatArray[4] is -107374176.000000
floatArray[5] is -107374176.000000


  1. int main(int argc, char *argv[])
  2. {
  3. /* Extract value for # of elements in array */
  4. int d;
  5. sscanf (argv[2], "%d", &d);
  6.  
  7.  
  8. /* Create Input and Output Streams */
  9. ifstream fin;
  10. ofstream fout;
  11.  
  12. /* Open Input Stream */
  13. fin.open("numlist.dat");
  14.  
  15.  
  16. /* Tests to ensure files opened */
  17. if(fin.fail())
  18. {
  19. cerr << "Input did not open\n";
  20. exit(2);
  21. }
  22.  
  23. /* Construct New Array */
  24. float *floatArray= new float[d];
  25.  
  26. /* Put numbers from list into array */
  27. float item;
  28. // printf("Put Numbers from list into array\n");
  29. for (j=0; j<d;j++)
  30. {
  31. fin >> item;
  32. // printf("item is %f\n", item);
  33. floatArray[j]=item;
  34. printf("floatArray[%d] is %f\n", j, floatArray[j]);
  35. }
  36.  
  37. /* Call to HEapSort */
  38. HeapSort(floatArray, len);
  39.  
  40. /* Display Array After Sort */
  41. for (j=0; j<d;j++)
  42. {
  43. printf("floatArray[%d] is %f\n", j, floatArray[j]);
  44. }
  45. /* Close file for Reading */
  46. fin.close();
  47.  
  48. /* Open Output Stream */
  49. fout.open("numlist.dat.srt");
  50.  
  51. /* Tests to ensure files opened */
  52. if(fout.fail())
  53. {
  54. cerr << "Input did not open\n";
  55. exit(2);
  56. }
  57.  
  58. /* Write Output */
  59. for (i=0;i<d;i++)
  60. {
  61. fout << floatArray[i] << endl;
  62. }
  63. /* Close File */
  64. fout.close();
  65.  
  66. getchar();
  67. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso
 
0
  #2
27 Days Ago
Hmm. Can you show us your file?
Don't forget to delete []floatArray;
Last edited by twomers; 27 Days Ago at 4:10 am.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC