473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

searching word in textfile?

5
Hi, i'm a beginner of C++ and would like some help figuring out some things, any suggestions would be appreciated.

I'm trying to search a word in an array and if it's found, the output would just put found and if not, then not found.

Here is what i came up with.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.   int i=10000;
  9.  
  10.   string filename;
  11.   string s;
  12.   string word[i];
  13.  
  14.   cout << "Enter a filename: "; 
  15.   cin >> filename;
  16.  
  17.   ifstream infile3(filename.c_str());
  18.   if(infile3.is_open()) {
  19.       cout << "Enter a word: "; 
  20.       cin >> word[i];           //prompts the user to enter a word into an array
  21.       while (infile3>>s && !infile3.eof()){
  22.     if (s==word[i]){                       // if the word input == text word
  23.        cout<<"found"<<endl;
  24.            break;
  25.                     }
  26.         else 
  27.        cout<<"not found"<<endl;
  28.            break;
  29.                                           }
  30.         infile3.close();
  31.                         }
  32.   return 0;
  33. }
  34.  
It does compile but it keeps on telling me not found, which means that it never ever finds the word even when the word is in there. I can't figure it out, any suggestions would be great :)! thanks
Feb 28 '07 #1
4 1447
horace1
1,510 Expert 1GB
you had a few errors - this now checks every word printing not found - if the word is found it prints found and stops you need to tidy it up
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.   int i=10000;
  9.  
  10.   string filename;
  11.   string s;
  12.   string word;
  13.  
  14.   cout << "Enter a filename: "; 
  15.   cin >> filename;
  16.  
  17.   ifstream infile3(filename.c_str());
  18.   if(infile3.is_open()) {
  19.       cout << "Enter a word: "; 
  20.       cin >> word;           //prompts the user to enter a word into an array
  21.       while (infile3>>s && !infile3.eof()){
  22.   cout << "word " << s << endl;
  23.     if (s==word){                       // if the word input == text word
  24.        cout<<"found"<<endl;
  25.            break;
  26.                     }
  27.         else 
  28.        cout<<"not found"<<endl;
  29.            //break;
  30.                                           }
  31.  
  32.                         }
  33.         infile3.close();
  34.                                 system("pause");
  35.   return 0;
  36. }
  37.  
Feb 28 '07 #2
Growl
5
you had a few errors - this now checks every word printing not found - if the word is found it prints found and stops you need to tidy it up
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.   int i=10000;
  9.  
  10.   string filename;
  11.   string s;
  12.   string word;
  13.   bool found = false;
  14.  
  15.  
  16.   cout << "Enter a filename: "; 
  17.   cin >> filename;
  18.  
  19.   ifstream infile3(filename.c_str());
  20.   if(infile3.is_open()) {
  21.       cout << "Enter a word: "; 
  22.       cin >> word;           //prompts the user to enter a word into an array
  23.       while (infile3>>s && !infile3.eof()){
  24.   cout << "word " << s << endl;
  25.     if (s==word){                       // if the word input == text word
  26.        cout<<"found"<<endl;
  27.            found = true;
  28.            break;
  29.                          }
  30.                                                    }
  31.         if (!found){
  32.            cout<<"not found"<<endl;
  33.                      }
  34.         infile3.close();
  35.                               }
  36.   return 0;
  37. }
  38.  
is there a way to do that so that it wouldn't output like fifty thousand "not found" before found?

I tried using a bool found, and set it so that when the word input is the same as the text, it's true and would cout found, and when it's not true, it would print out not found, but got the same result as the first time, which just prints out "not found" and also, what is a system "pause" do?

Thanks again,
Growl
Feb 28 '07 #3
horace1
1,510 Expert 1GB
move your print "not found" out of the while loop so you only test when you have finished reading the file, e.g.
Expand|Select|Wrap|Line Numbers
  1.       }
  2.        if (!found){
  3.            cout<<"not found"<<endl;
  4.         infile3.close();
  5.  
system("pause"); halts the program until you press enter key - some IDEs automatically close the stdout window when the program terminates so this halts the program so you can see the output before the window closes.
Feb 28 '07 #4
Growl
5
move your print "not found" out of the while loop so you only test when you have finished reading the file, e.g.
Expand|Select|Wrap|Line Numbers
  1.       }
  2.        if (!found){
  3.            cout<<"not found"<<endl;
  4.         infile3.close();
  5.  
system("pause"); halts the program until you press enter key - some IDEs automatically close the stdout window when the program terminates so this halts the program so you can see the output before the window closes.

Ohhh! thanks a lot O_O!
Feb 28 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Eric Linders | last post by:
Hi, I have customer contact information in a MySQL DB. The standard stuff: first name, last name, city, state, zip, etc. I'd like to write a PHP script that will take a customer ID range...
4
by: Paul | last post by:
Hi, (First apologies if this is not the most relevant place to post this but I wasn't sure of where was and I am writing my app in VB.) I'm attempting to parse a binary file for which I have...
5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
7
by: Ep | last post by:
I need to put some text (and numbers) in Word and Excel. How do I put that into them? Also how can I use the "hot keys" to navigate in each. Thanks Ep
4
by: Michi | last post by:
I was wondering what the best solution is for making large numbers of TEXT (or BLOB?) fields searchable. For example, if I have a forum, what is the best way to be able to search for specific...
7
by: pbd22 | last post by:
Hi. I am somewhat new to this and would like some advice. I want to search my xml file using "keyword" search and return results based on "proximity matching" - in other words, since the search...
2
by: lekshminair | last post by:
U]can u solve it I am trying to take a specific word in textfile. for example: new .txt "Please follow these guidelines when posting questions. Submitting clear and concise questions allows...
1
by: =?Utf-8?B?UmFm?= | last post by:
Hello, I have to write an application that scans textfiles for certain words. I'm talking about approximately 5000 words. The only way I can think of to do this is to scan each textfile for each...
5
by: Sevla | last post by:
hello guys i set this code , and i would like to know if its possible set "quotes" on the searched word. Thank You #word searching program within a txt file #!C:/perl/bin/perl.exe start:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.