Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,704 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,461 people online right now. Registration is fast and FREE... Join Now!




Unexpected behavior in IF statement

 
Reply to this topicStart new topic

Unexpected behavior in IF statement, Any ideas??

Piperlester
post 6 Dec, 2005 - 10:57 PM
Post #1


D.I.C Head

Group Icon
Joined: 18 Oct, 2005
Posts: 130



Dream Kudos: 25
My Contributions


Here's the snippet:
while (blah does blah)
{
editIn.FileLine = 1;
if (editIn.FileLine = 1)
{
cout << "in editIn.FileLine = 1" << editIn.FileLine << endl;
cout << editIn.Buffer << endl;
editIn.FileLine++;
}
}


Assume all variables are declared correctly (because they are, this loop works elsewhere with the variable names changed).

If blah does blah should (and does) repeat 10-15 times, depending on what the user has previously entered, this loop should enter the if statement once, then the value of editIn.FileLine should be set to 2, and the while should execute how ever many times, but the if statement should be skipped over, because theoretically it returns false.

My problem lies in that this code enters the if statement, exits, but the variable (editIn.FileLine) is never ++'d. No matter where I put the ++ statement.

The values are declared in the correct areas, and I don't have the editIn.FileLine = 1; in the while (blah does blah) {}, but before it.

Any suggestions? Could be something simple I'm just missing... but.........
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 7 Dec, 2005 - 06:34 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


First, you should validate that FileLine is an integer, and it does hold the value of one. Secondly, I assume you are trying to determine if it does equal one. This statement:
CODE

if (editIn.FileLine = 1)

Is using an assignment operator, where you should be using the equality operator, unless you have overloaded the assignment operator somewhere that is not provided.
CODE

if (editIn.FileLine == 1)
User is online!Profile CardPM

Go to the top of the page

Piperlester
post 7 Dec, 2005 - 07:34 AM
Post #3


D.I.C Head

Group Icon
Joined: 18 Oct, 2005
Posts: 130



Dream Kudos: 25
My Contributions


Wow. I can't believe I missed that ==. I was even staring at a tutorial on IF statements when I posted the question.

struct makeStruct // Declare makeStruct struct type
{
// Declare member types
int FileLine;
int LastPosition;
int fieldCounter;
int Counter;
int lineCounter;
};


That is the only instance of FileLine being declared. The struct being used in the function I'm having problems with is only used in that function.

Thanks for the help, you saved me many more hours of hair pulling.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 09:17AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month