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. -
#include <iostream>
-
#include <string>
-
#include <fstream>
-
using namespace std;
-
-
int main()
-
{
-
int i=10000;
-
-
string filename;
-
string s;
-
string word[i];
-
-
cout << "Enter a filename: ";
-
cin >> filename;
-
-
ifstream infile3(filename.c_str());
-
if(infile3.is_open()) {
-
cout << "Enter a word: ";
-
cin >> word[i]; //prompts the user to enter a word into an array
-
while (infile3>>s && !infile3.eof()){
-
if (s==word[i]){ // if the word input == text word
-
cout<<"found"<<endl;
-
break;
-
}
-
else
-
cout<<"not found"<<endl;
-
break;
-
}
-
infile3.close();
-
}
-
return 0;
-
}
-
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
4 1447
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 -
#include <iostream>
-
#include <string>
-
#include <fstream>
-
using namespace std;
-
-
int main()
-
{
-
int i=10000;
-
-
string filename;
-
string s;
-
string word;
-
-
cout << "Enter a filename: ";
-
cin >> filename;
-
-
ifstream infile3(filename.c_str());
-
if(infile3.is_open()) {
-
cout << "Enter a word: ";
-
cin >> word; //prompts the user to enter a word into an array
-
while (infile3>>s && !infile3.eof()){
-
cout << "word " << s << endl;
-
if (s==word){ // if the word input == text word
-
cout<<"found"<<endl;
-
break;
-
}
-
else
-
cout<<"not found"<<endl;
-
//break;
-
}
-
-
}
-
infile3.close();
-
system("pause");
-
return 0;
-
}
-
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 -
#include <iostream>
-
#include <string>
-
#include <fstream>
-
using namespace std;
-
-
int main()
-
{
-
int i=10000;
-
-
string filename;
-
string s;
-
string word;
-
bool found = false;
-
-
-
cout << "Enter a filename: ";
-
cin >> filename;
-
-
ifstream infile3(filename.c_str());
-
if(infile3.is_open()) {
-
cout << "Enter a word: ";
-
cin >> word; //prompts the user to enter a word into an array
-
while (infile3>>s && !infile3.eof()){
-
cout << "word " << s << endl;
-
if (s==word){ // if the word input == text word
-
cout<<"found"<<endl;
-
found = true;
-
break;
-
}
-
}
-
if (!found){
-
cout<<"not found"<<endl;
-
}
-
infile3.close();
-
}
-
return 0;
-
}
-
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
move your print "not found" out of the while loop so you only test when you have finished reading the file, e.g. -
}
-
if (!found){
-
cout<<"not found"<<endl;
-
infile3.close();
-
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.
move your print "not found" out of the while loop so you only test when you have finished reading the file, e.g. -
}
-
if (!found){
-
cout<<"not found"<<endl;
-
infile3.close();
-
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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:
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |