input, search, output

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

Join Date: Apr 2009
Posts: 2
Reputation: joozi is an unknown quantity at this point 
Solved Threads: 0
joozi joozi is offline Offline
Newbie Poster

input, search, output

 
0
  #1
Apr 4th, 2009
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;

  1. #include <stdlib.h>
  2. #include <iostream.h>
  3. #include <fstream.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int input, count=0, hold;
  9. ifstream inFile("Problem2.txt");
  10.  
  11. cout << "Enter a number ";
  12. cin >> input;
  13.  
  14. if (!inFile) {
  15. cout << "Unable to open file";
  16. return(-1);
  17. }
  18.  
  19. int number;
  20. inFile >> number;
  21. while(!inFile.eof())
  22. {
  23. inFile >> number;
  24. if(number == input){
  25. count++;}
  26. }
  27. cout << count;
  28. cin >> hold;
  29. inFile.close();
  30. }

the input file im using is:

  1. 64
  2. 64
  3. 92
  4. 64
  5. 64
  6. 55
  7. 56
  8. 77
  9. 75
  10. 66
  11. 25
  12. 35
  13. 39
  14. 90
  15. 19
  16. 23
  17. 44
  18. 45
  19. 88
  20. 85
  21. 15
  22. 27
  23. 32
  24. 44
  25. 68
  26. 8
  27. 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%.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: input, search, output

 
0
  #2
Apr 4th, 2009
The while loop is wrong -- don't need eof()
  1. int number;
  2. while( inFile >> number )
  3. {
  4. // blabla
  5. }
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: input, search, output

 
1
  #3
Apr 4th, 2009
  1. int number;
  2. inFile >> number;
  3. while(!inFile.eof())
  4. {
  5. inFile >> number;
You're overwriting the first number in the file. You haven't counted whether the first line in the file is the number that the user entered. That's why your program only reported 3 occurrences of 64.

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:
  1. while (inFile >> number)
  2. {
  3. if (number == input)
  4. {
  5. /* etc */
  6. }
  7. }
Then remove the line before the while loop: 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: input, search, output

 
0
  #4
Apr 4th, 2009
In one of the last lines I come across the following instruction:
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."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 2
Reputation: joozi is an unknown quantity at this point 
Solved Threads: 0
joozi joozi is offline Offline
Newbie Poster

Re: input, search, output

 
0
  #5
Apr 4th, 2009
Thank you, I asked my instructor how to fix it and he told me the .eof was correct. Thanks again, can't believe it was that simple.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: input, search, output

 
0
  #6
Apr 4th, 2009
your instructor is an idiot. Go immediately to another university.
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: input, search, output

 
1
  #7
Apr 4th, 2009
>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:
  1. inFile >> number;
  2. while ( !inFile.eof() )
  3. {
  4. if (number == input)
  5. count++;
  6.  
  7. inFile >> number;
  8. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: input, search, output

 
0
  #8
Apr 5th, 2009
And to prove that he's an..., try this:
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char line[80];
  8. ifstream inFile("x.x");
  9.  
  10. while (!inFile.eof())
  11. {
  12. inFile >> line;
  13. cout << line << ' ';
  14. }
  15. inFile.close();
  16. return 0;
  17. }

Use this file:
  1. ONE
  2. two
  3. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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