I'm trying a simple comparison here, assignment doesn't work as i would like... here is the code,
int returnDateIndex(Paragraph para)
{
long firstIndex = 0;
for (int i = 0; i < para.Words.Count; i++)
{
if (para.Words[i].Text == "Second")
{
if (para.Words[i - 1].Text == "First")
{
firstIndex = para.Words[i].FirstSymbolPosition;
}
}
}
return (int)firstIndex;
}
I ran my debugger (In VS) and when that assignment is called, the int on the right was equal to 50, but the int on the left stayed equal to 0. No idea what I'm missing.
This application is using the Abbyy FineReader 9.0 SDK and the documentation for FirstSymbolPosition says it returns a read-only Long
EDIT: the code has been stripped of all features to make it easier for viewers to see where the problem is. I would appreciate answers for the original questions and anything else with the code that is bugging you as a comment please.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.
One obvious error is that you're expecting to return the first case where your conditions match (hence the firstIndex variable name), but you're really returning the last point where they match. This is also bad because it means you keep looking after finding your match. Another is that if the very first word in a sentence is "Second", you'll try to reference a negative index, which is very bad. Try this instead:
int returnDateIndex(Paragraph para)
{
for (int i = 1; i < para.Words.Count; i++)
{
if (para.Words[i - 1].Text == "First" && para.Words[i].Text == "Second")
return (int)para.Words[i].FirstSymbolPosition;
}
return 0; // this is what your original code would have returned in a "not found" scenario
}
This code fixes both errors, and also completely side-steps your assignment problem.
As an aside you should add break
after your assignment of firstIndex
otherwise it could get overwritten by another value if the conditions are met later on in the loop.
In addition to what ChrisF said, this is pretty dangerous:
if (para.Words[i - 1].Text == "First")
There's no check in there to make sure i is greater than or equal to 1, if i is equal to 0 then you will be indexing out of the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With