Searching Problem!

Hello every one! Following is my code which do:
It takes sentence from user and also picks a sentence from a input.txt file and
compares both whether file has sentence in it or not.
There are bugs in my problem. Can anyone help me to identify them?
-------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	char abc[50],txt[50];
	bool flag=false;
	cout<<"Please enter sentence to be searched\n";
	cin>>abc;
	ifstream fin;
	fin.open("input.txt");
	fin.getline(txt,50);
        fin.close();
	int len_abc=0,len_txt=0;
	len_abc=strlen(abc);
	len_txt=strlen(txt);
	if(len_abc>=len_txt)
	{
		for(int i=0,j=0; i<len_txt; i++,j++)
		{
			if(abc[i]!=txt[j])
			{
				j=0;
				i--;
			}
			if(i==len_txt-1)
				flag=true;
		}
	}
	else if( (flag==false) ||(len_abc<len_txt) )
		cout<<"Sentence is not present\n";
	if(flag==true)
		cout<<"Sentence is present\n";
	return 0;
}

------------------------------------------------------------------------------[c
Last edited on
a) there is strcmp() function in cstring
b) there is std::string type in string which can be compared using operator ==
Some suggestions before diving in to find bugs.

- you are just doing one getline. What about if the file has many lines ?
- always make your programs modular. So, you should have a function to compare the string and pass the strings to that function for comparison. This will have the advantage that the code would be more readable and easy to find bugs.

btw, what are the issues you are seeing ?
writetonsharma
Ans1: File has just one line(in my case, if it has two much lines then i will do next time. But for time being just with one line)
.
.
.
Ans2:It just don't work! My logic is correct and program is written according to it. But still i can't find bugs! If i could then i might solve it my self! Please help!
Line 30: checking if flag == false is redundant: flag will be always false at this point (since only place where in could be set to true is in another if branch)
In fact if iisn't needed at all, because it will always be true.

There is a problem in you for loop as well: it either will be endless or flag will be set to true at the end of the loop.

I cannot even understand, what are you doing here. Are syou searching a subsring here?
If so, there is a string.find() function
http://en.cppreference.com/w/cpp/string/basic_string/find
Last edited on
MiiNiPaa i appreciate what you're saying. . .
I will make you understand by giving my logic:
LOGIC:
There is a file named "input.txt" along my .cpp file in which i have written a line "Peopeople are unforgiveable sometimes"
.
Next i get a sentence(it can be a word) from user lines-7,87 store it into
an array named abc[50];
Also pick line from txt file "input.txt" and store it to another array named txt[50] lines- 10,11,12,13 .
Then it takes lengths of both arrays and made a check if length of txt is greater than print "present" else vice versa.
Interior loop is supposed to be doing the following work:
------------------------------------------------------------------------------
If input.txt has this line:
peopeople are unforgiveable sometimes.
and user enters this
people are unfor
then it should say "Present" but it don't say!
-----------------------------------------------------------------------------
Help!
Last edited on
i purposely made if condition on line 21 to check if j==len_txt-1 then it will signal that user entered word is completely presented in the file if not then j will never be equal to len_txt-1.
which will, as a result, keep flag=false resulting in true of below if condition printing result.
You're almost saying the same! But i am not finding substring but comparing both arrays of characters
1
2
3
4
5
6
7
8
9
10
std::ifstream fin("input.txt");
std::cout << "Please enter sentence to be searched\n";
std::string input, fileinput;
std::getline(std::cin, input);
std::getline(fin, fileinput);
if (fileinput.find(input) != std::string::npos)
    std::cout << "present";
else
    std::cout << "not present";
std::cout << std::endl;

Will it work for you?

Main problems with your code:
1) If abc will contain, say, "aaa" and txt will contain "bbb", you will have an infinite loop.
2) If you won't run into infinite loop, flag will always be true
3) Looks like you are trying to check if txt have sentence from abc, and judging from what you said it should be other way.
thanks i fixed it. . . :)
Topic archived. No new replies allowed.