944,138 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2530
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 9th, 2005
0

Error reading file

Expand Post »
i tried this
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
desidude is offline Offline
15 posts
since Jul 2005
Jul 9th, 2005
0

Re: Error reading file

C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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".
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 9th, 2005
0

Re: Error reading file

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
desidude is offline Offline
15 posts
since Jul 2005
Jul 9th, 2005
0

Re: Error reading file

>i tried the way u suggested me but it still gives same output
Odd.
C++ Syntax (Toggle Plain Text)
  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. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 9th, 2005
0

Re: Error reading file

i tried every possible way u suggested me and it still doesnt work. could it be some other problem?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
desidude is offline Offline
15 posts
since Jul 2005
Jul 9th, 2005
0

Re: Error reading file

Did you copy and paste what dave wrote and compiled it?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jul 9th, 2005
0

Re: Error reading file

>fin >> isFirst;
I think you mean:
C++ Syntax (Toggle Plain Text)
  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):
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 9th, 2005
0

Re: Error reading file

thx a bunch guys
i am sorry dave it was my fault in implementing codes . its working
thx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
desidude is offline Offline
15 posts
since Jul 2005
Jul 9th, 2005
0

Re: Error reading file

again need help guys
trying to calculate average of all numbers in previous program
here is what i did but it gives average = 77
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
desidude is offline Offline
15 posts
since Jul 2005
Jul 9th, 2005
0

Re: Error reading file

>here is what i did but it gives average = 77
Um...what's the problem with getting the correct answer?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: using stringstream for tokenizing a string
Next Thread in C++ Forum Timeline: Help! error LNK2001 & error LNK1120





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


Follow us on Twitter


© 2011 DaniWeb® LLC