| | |
Error reading file
Thread Solved
![]() |
•
•
Join Date: Jul 2005
Posts: 15
Reputation:
Solved Threads: 0
i tried this
<< 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
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; int main() { // Declare input stream ifstream fin; int min, max, val; int isFirst = true; fin.open("inFile.txt"); fin >> isFirst; min = isFirst; max = isFirst; while (fin >> val) { fin >> val; if (val < min) val = min; else val = max; } fin.close(); cout << "Largest value is " << max << ", smallest value is " << min << "\n"; return 0; }
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
C++ Syntax (Toggle Plain Text)
while (fin >> val) { fin >> val; if (val < min) val = min; else val = max; }
C++ Syntax (Toggle Plain Text)
while ( fin >> val ) { if ( val < min ) { min = val; } if ( val > max ) { max = val; } }
"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
>i tried the way u suggested me but it still gives same output
Odd.
Odd.
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> using namespace std; int main() { ifstream fin("inFile.txt"); int min, max, val; if ( fin >> val ) { min = max = val; while ( fin >> val ) { if ( val < min ) { min = val; } if ( val > max ) { max = val; } } } cout << "Largest value is " << max << ", smallest value is " << min << "\n"; return 0; } /* my output Largest value is 100, smallest value is 45 */
"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
>fin >> isFirst;
I think you mean:
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):
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 think you mean:
C++ Syntax (Toggle Plain Text)
fin>> val;
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> using namespace std; int main() { ifstream fin("inFile.txt"); int val; if (fin >> val) { int min, max; min = max = val; while (fin >> val) { if (val < min) min = val; if (val > max) max = val; } cout << "Largest value is " << max << ", smallest value is " << min << "\n"; } return 0; }
>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.
•
•
Join Date: Jul 2005
Posts: 15
Reputation:
Solved Threads: 0
again need help guys
trying to calculate average of all numbers in previous program
here is what i did but it gives average = 77
Code tags added. -Narue
what am i doing wrong here?
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)
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; int main() { ifstream fin("inFile.txt"); ofstream fout("outFile.txt"); if (fin.fail()) { cout<<"Input file opening failed.\n"; exit(1); } if (fout.fail()) { cout<<"output file opening failed.\n"; exit(1); } int min, max, val; double sum = 0; int count = 0, next ; if ( fin >> val ) { min = max = val; while ( fin >> val ) { if ( val < min ) { min = val; } if ( val > max ) { max = val; } } sum = sum+val; count=count++; } sum=sum/count; fout<<"average score is:"<< sum << "\n"; cout<<"average score is:"<< sum << "\n"; fout << "Largest value is " << max << ", smallest value is " << min << "\n"; cout << "Largest value is " << max << ", smallest value is " << min << "\n"; fin.close(); fout.close(); return 0; }
what am i doing wrong here?
![]() |
Similar Threads
- First year assigment on reading file, sorting and outputting invoice (C++)
- Error reading in file in binary mode (C++)
- Error reading zip (PHP)
- Error Message Concerning Reading File From A Drive (C++)
- reading a file into code (Java)
- Error occured during the file system check. (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: using stringstream for tokenizing a string
- Next Thread: Help! error LNK2001 & error LNK1120
| Thread Tools | Search this Thread |
ace_thread api array based binary bitmap borland c++ c/c++ calling char class classes code coding compile console conversion count delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion reference reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






