473,772 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

searching word in textfile?

5 New Member
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 1466
horace1
1,510 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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
11621
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 (e.g., customer 10000 - 11000) and stick those into an MS Word document, plain text document, or a PDF (any of the three will work). The key is that each record has to have a page break after it so that
4
6002
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 the format. The format states that the general packet format is as follows Message header Hex Length of whole binary packet
5
7652
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 directory) and count number of times any given word appears in the text. #!/usr/bin/python
7
5064
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
2764
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 words? How about exact phrases? I saw a solution where all words are preindexed in a "dictionary" like table and then another table stores the word matches. That seems really fast, but it has two major problems: 1) it can't do exact matches, and 2)...
7
2621
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 string will often not produce a direct match, the results will be based on proximity (50%, 20% 100%, etc). are there any good examples out there on how to do keyword searches on XML data? How should i set up my xml file so
2
1740
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 those reading to understand your problem and respond easily." I this text file extract with "ing"formatt words ie,posting submitting
1
1172
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 word. This takes a lot of time and a lot of capacity of my pc. Is there another way to do this task? Regards, Raf
5
3554
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
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.