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?
-------------------------------------------------------------------------------
- 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.
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 == falseis 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.
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!
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.
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.