| | |
input, search, output
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 2
Reputation:
Solved Threads: 0
Im doing a c++ course at uni and one of my questions is to read in an external file, input a number (like 64) then the program will search the file for the number and output how many times the number the user input comes up in the file.
here is my code;
the input file im using is:
If i input 64 as my input number it shoud tell me that 64 is in the file 4 times but it only outputs 3, and if i input 0 it should come up once but it comes up twice .I havn't got much experience in c++ so I don't really know what im doing 100%.
here is my code;
C++ Syntax (Toggle Plain Text)
#include <stdlib.h> #include <iostream.h> #include <fstream.h> using namespace std; int main() { int input, count=0, hold; ifstream inFile("Problem2.txt"); cout << "Enter a number "; cin >> input; if (!inFile) { cout << "Unable to open file"; return(-1); } int number; inFile >> number; while(!inFile.eof()) { inFile >> number; if(number == input){ count++;} } cout << count; cin >> hold; inFile.close(); }
the input file im using is:
C++ Syntax (Toggle Plain Text)
64 64 92 64 64 55 56 77 75 66 25 35 39 90 19 23 44 45 88 85 15 27 32 44 68 8 0
If i input 64 as my input number it shoud tell me that 64 is in the file 4 times but it only outputs 3, and if i input 0 it should come up once but it comes up twice .I havn't got much experience in c++ so I don't really know what im doing 100%.
The while loop is wrong -- don't need eof()
C++ Syntax (Toggle Plain Text)
int number; while( inFile >> number ) { // blabla }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
C++ Syntax (Toggle Plain Text)
int number; inFile >> number; while(!inFile.eof()) { inFile >> number;
Your second problem is how you're reading your file. The issue with using .eof() to find out if you're at the end of the file is that it only reports an EOF after you've already had an unsuccessful read. So the last line in the file will end up being read twice: the first time correctly, the second time it will read it again and give you an EOF.
So, I recommend using the following method instead:
C++ Syntax (Toggle Plain Text)
while (inFile >> number) { if (number == input) { /* etc */ } }
inFile >> number; , which will solve your first problem. "Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
In one of the last lines I come across the following instruction:
Please remember to also delete the variable declaration of 'hold' (somewhere in the first lines of your code) ...
cin >> hold; If you're putting that instruction there to prevent the program from automatically closing: it's better to use cin.get(); instead ...Please remember to also delete the variable declaration of 'hold' (somewhere in the first lines of your code) ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
>I asked my instructor how to fix it and he told me the .eof was correct.
Well, there's nothing 'wrong' with eof(), you just need to understand that it will only indicate an EOF after an unsuccessful read. So, for example, you could modify your code to look like:
This works because it checks for an EOF after you've tried reading from the file. If there's no EOF, it goes ahead and processes the input, and then attempts another read.
Of course, if your instructor told you that your use of eof() was correct, then they really are an idiot.
Well, there's nothing 'wrong' with eof(), you just need to understand that it will only indicate an EOF after an unsuccessful read. So, for example, you could modify your code to look like:
C++ Syntax (Toggle Plain Text)
inFile >> number; while ( !inFile.eof() ) { if (number == input) count++; inFile >> number; }
This works because it checks for an EOF after you've tried reading from the file. If there's no EOF, it goes ahead and processes the input, and then attempts another read.
Of course, if your instructor told you that your use of eof() was correct, then they really are an idiot.
Last edited by John A; Apr 4th, 2009 at 10:10 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
And to prove that he's an..., try this:
Use this file:
What just got output?
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int main() { char line[80]; ifstream inFile("x.x"); while (!inFile.eof()) { inFile >> line; cout << line << ' '; } inFile.close(); return 0; }
Use this file:
C++ Syntax (Toggle Plain Text)
ONE two Three
What just got output?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Binary Search Array From A File Help (C++)
- Help with 2D Array Output Problem (Java)
- search (PHP)
- JAvascript Search Eninge Script (JavaScript / DHTML / AJAX)
- Need Help for Search function button (VB.NET)
- searching by multiple user input and display output in textbox and picture box (VB.NET)
- sequential search (cannot import string functions) (Python)
- How can i call my FileOutput method to output the array ?? really confused... (Java)
- JTable not calling getValueAt - any ideas? (Java)
- Help with input (C)
Other Threads in the C++ Forum
- Previous Thread: Gui, Visual Studio 05, Loop + Events + Stuck
- Next Thread: Exam in C++
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






