Error reading file

Thread Solved
Reply

Join Date: Jul 2005
Posts: 15
Reputation: desidude is an unknown quantity at this point 
Solved Threads: 0
desidude desidude is offline Offline
Newbie Poster

Error reading file

 
0
  #1
Jul 9th, 2005
i tried this
  1. #include <fstream>
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. // Declare input stream
  10. ifstream fin;
  11. int min, max, val;
  12. int isFirst = true;
  13. fin.open("inFile.txt");
  14.  
  15.  
  16.  
  17. fin >> isFirst;
  18. min = isFirst;
  19. max = isFirst;
  20.  
  21. while (fin >> val)
  22. {
  23.  
  24. fin >> val;
  25. if (val < min)
  26. val = min;
  27. else
  28. val = max;
  29.  
  30. }
  31.  
  32.  
  33.  
  34. fin.close();
  35.  
  36. cout << "Largest value is " << max << ", smallest value is " << min << "\n";
  37. return 0;
  38. }
<< moderator edit: added code tags: [code][/code] >>
and it prints this
Largest value is 1, smallest value is 1
Press any key to continue
can someone suggest me what's i am doing wrong here
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error reading file

 
0
  #2
Jul 9th, 2005
  1. while (fin >> val)
  2. {
  3.  
  4. fin >> val;
  5. if (val < min)
  6. val = min;
  7. else
  8. val = max;
  9.  
  10. }
Change min and/or max, not val.
  1. while ( fin >> val )
  2. {
  3. if ( val < min )
  4. {
  5. min = val;
  6. }
  7. if ( val > max )
  8. {
  9. max = val;
  10. }
  11. }
It might also be helpful to post the contents of "inFile.txt".
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 15
Reputation: desidude is an unknown quantity at this point 
Solved Threads: 0
desidude desidude is offline Offline
Newbie Poster

Re: Error reading file

 
0
  #3
Jul 9th, 2005
i tried the way u suggested me but it still gives same output
the content of "inFile.txt" is
100
45
74
45
86
94
75
96
56
85
82
88
88
56
77
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error reading file

 
0
  #4
Jul 9th, 2005
>i tried the way u suggested me but it still gives same output
Odd.
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8. ifstream fin("inFile.txt");
  9. int min, max, val;
  10. if ( fin >> val )
  11. {
  12. min = max = val;
  13. while ( fin >> val )
  14. {
  15. if ( val < min )
  16. {
  17. min = val;
  18. }
  19. if ( val > max )
  20. {
  21. max = val;
  22. }
  23. }
  24. }
  25. cout << "Largest value is " << max << ", smallest value is " << min << "\n";
  26. return 0;
  27. }
  28.  
  29. /* my output
  30. Largest value is 100, smallest value is 45
  31. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 15
Reputation: desidude is an unknown quantity at this point 
Solved Threads: 0
desidude desidude is offline Offline
Newbie Poster

Re: Error reading file

 
0
  #5
Jul 9th, 2005
i tried every possible way u suggested me and it still doesnt work. could it be some other problem?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Error reading file

 
0
  #6
Jul 9th, 2005
Did you copy and paste what dave wrote and compiled it?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Error reading file

 
0
  #7
Jul 9th, 2005
>fin >> isFirst;
I think you mean:
  1. fin>> val;
isFirst doesn't strike me as a data variable, it seems more like a status variable. You don't even need it. Oddly enough, I didn't bother looking at Dave's code, but my solution is only slightly different (but it avoids undefined behavior in the case of an empty or non-existent file):
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. ifstream fin("inFile.txt");
  8. int val;
  9.  
  10. if (fin >> val) {
  11. int min, max;
  12.  
  13. min = max = val;
  14.  
  15. while (fin >> val)
  16. {
  17. if (val < min)
  18. min = val;
  19.  
  20. if (val > max)
  21. max = val;
  22. }
  23.  
  24. cout << "Largest value is " << max << ", smallest value is " << min << "\n";
  25. }
  26.  
  27. return 0;
  28. }
Great minds think alike, I suppose.

>i tried every possible way u suggested me and it still doesnt work.
Then instead of looking at the suggestions and implementing them they way you think they should be done, actually try them as they are written. In my experience, Dave doesn't post untested code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 15
Reputation: desidude is an unknown quantity at this point 
Solved Threads: 0
desidude desidude is offline Offline
Newbie Poster

Re: Error reading file

 
0
  #8
Jul 9th, 2005
thx a bunch guys
i am sorry dave it was my fault in implementing codes . its working
thx
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 15
Reputation: desidude is an unknown quantity at this point 
Solved Threads: 0
desidude desidude is offline Offline
Newbie Poster

Re: Error reading file

 
0
  #9
Jul 9th, 2005
again need help guys
trying to calculate average of all numbers in previous program
here is what i did but it gives average = 77
  1. #include <fstream>
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int main()
  9.  
  10. {
  11.  
  12. ifstream fin("inFile.txt");
  13. ofstream fout("outFile.txt");
  14.  
  15. if (fin.fail())
  16. {
  17. cout<<"Input file opening failed.\n";
  18. exit(1);
  19. }
  20. if (fout.fail())
  21. {
  22. cout<<"output file opening failed.\n";
  23. exit(1);
  24. }
  25. int min, max, val;
  26. double sum = 0;
  27. int count = 0, next ;
  28.  
  29.  
  30.  
  31. if ( fin >> val )
  32.  
  33. {
  34.  
  35.  
  36. min = max = val;
  37. while ( fin >> val )
  38. {
  39.  
  40.  
  41. if ( val < min )
  42. {
  43. min = val;
  44. }
  45.  
  46. if ( val > max )
  47. {
  48. max = val;
  49. }
  50.  
  51. }
  52. sum = sum+val;
  53. count=count++;
  54.  
  55. }
  56.  
  57.  
  58.  
  59. sum=sum/count;
  60.  
  61.  
  62.  
  63. fout<<"average score is:"<< sum << "\n";
  64. cout<<"average score is:"<< sum << "\n";
  65.  
  66.  
  67. fout << "Largest value is " << max << ", smallest value is " << min << "\n";
  68. cout << "Largest value is " << max << ", smallest value is " << min << "\n";
  69. fin.close();
  70. fout.close();
  71.  
  72. return 0;
  73. }
Code tags added. -Narue

what am i doing wrong here?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Error reading file

 
0
  #10
Jul 9th, 2005
>here is what i did but it gives average = 77
Um...what's the problem with getting the correct answer?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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