Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++ Coding Isn't Working Properly


  • Please log in to reply
13 replies to this topic

#1 thormus

thormus

  • Members
  • 6 posts
  • OFFLINE
  •  
  • Local time:08:13 AM

Posted 19 August 2008 - 03:46 PM

I had written some programs in C++ a while ago and they no longer function properly, in addition now any new code I write does not seem to want to work properly either. I tested this by writing the following program:

// while loop theory test

#include <iostream>

using namespace std;

int main()
{
char again = 'y';
while ( again = 'y' || 'Y')
{
cout << "run another loop? <Y/N>:";
cin >> again;
}
cout << "End of program";
return 0;
}


the result was that it would start the loop, but no matter what character the user put in it would just go back into the loop. When I added a nested IF statement with the command to break the loop, it wouldn't loop at all.

I do not know weather this has something to a recent Vista update, or if it was caused by something akin to a virus. As it is right now, I'm about ready to completly start my computer over again.( A option I would like best to avoid.)

BC AdBot (Login to Remove)

 


#2 M...

M...

  • Members
  • 386 posts
  • OFFLINE
  •  
  • Local time:04:13 AM

Posted 19 August 2008 - 06:55 PM

Hello thormus,

When did this program ever behave in a way other than the way you describe? I don't think Vista updates or viruses have anything to do with the problem(s).

In:

while ( again = 'y' || 'Y')

there is an assignment expression, not a test for equality. Additionally, there appears to be a mangled logical/Boolean operation -- syntactically it is correct, but semantically it makes no sense in the context in which it is being used.

I think you want something like the following:


while ( (again == 'y') || (again == 'Y') )


Edited by M..., 19 August 2008 - 09:42 PM.


#3 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:13 AM

Posted 19 August 2008 - 09:37 PM

while ( again = 'y' || 'Y')


I was going to point out that there is no way that would work, and I have yet to see any malware that effects someone's source code. I have to agree with M... here.

#4 thormus

thormus
  • Topic Starter

  • Members
  • 6 posts
  • OFFLINE
  •  
  • Local time:08:13 AM

Posted 20 August 2008 - 12:33 AM

Hello thormus,

When did this program ever behave in a way other than the way you describe? I don't think Vista updates or viruses have anything to do with the problem(s).

In:

while ( again = 'y' || 'Y')

there is an assignment expression, not a test for equality. Additionally, there appears to be a mangled logical/Boolean operation -- syntactically it is correct, but semantically it makes no sense in the context in which it is being used.

I think you want something like the following:


while ( (again == 'y') || (again == 'Y') )


I cannot pin a date exactly on when this started happening, but my last successful run was about a week ago.

As for my while statement, I had been playing around when I discovered that you could test for multiple events using logic statements as long as they were logically correct.
so
While ( again == 'y' || 'Y' )
works just as well as saying "(again == 'y') || (again== "Y')," just with a little bit less redundancy. You could also go further and say (( again == 'y' || 'Y') || ( again != 'n' || 'N' ))

The first and most important thing to remember about logic statements, is that they don't have to be logical to be logically correct.
My While (again == 'y' || 'Y' ) is my standard for when I am building a user initiated loop.

#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:13 AM

Posted 20 August 2008 - 08:48 AM

While ( again == 'y' || 'Y' ) works just as well as saying "(again == 'y') || (again== "Y'),"


That was not what you wrote in your original post. you code reads:

while ( again = 'y' || 'Y')

Which can not possibly work since a single equal sign is an assignment operator, and not a comparison operator. So if the code you originally provided is what you really used, then that was the problem.

#6 thormus

thormus
  • Topic Starter

  • Members
  • 6 posts
  • OFFLINE
  •  
  • Local time:08:13 AM

Posted 21 August 2008 - 12:37 PM

Well if a Virus or Malware wouldn't cause this problem do you know what could, and if so how to fix it?

While (again = 'y' || 'Y' ) was a typo, when I first posted I typed out the code to basically show how I had structured my program. While the supplied code isn't exactly what I used, I do write bits of codes like that in order to test theories and to check to my coding.

#7 M...

M...

  • Members
  • 386 posts
  • OFFLINE
  •  
  • Local time:04:13 AM

Posted 21 August 2008 - 02:14 PM

so
While ( again == 'y' || 'Y' )
works just as well as saying "(again == 'y') || (again== "Y')," just with a little bit less redundancy.

I could be wrong, but I don't think the above statement is correct.

The expression:

while ( again == 'y' || 'Y' )

is evaluated as:

while ( (again == 'y') || 'Y' )

because the precedence of the == operator (equality) is higher than the precedence of the || operator (logical OR).

This then reduces to:

while ( (true_or_false) || true )

The second operand of the || operator is always true because the binary value of the character constant 'Y' is non-zero.

This further reduces to an infinite loop:

while ( true )

Since there are no side effects from the evaluation of either of the operands, the value of the first operand of the || operator is irrelevant when the value of the second operand is a constant value of true.

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:13 AM

Posted 21 August 2008 - 04:52 PM

Actually, it does work, and I was very surprised. I wrote the following little program:
#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
	string s = "2";

	if ( s == "1" || "2"){
		cout << ("s == " + s);
	}else{
		cout << "Expression could not be evaluated.";
	}


	//pause execution so that I can see the results.....
	char hold;
	cin.get(hold);
	return 0;
	
}

The program printed out "s == 2". I was skeptical also, because by my reasoning, the only way the expression would print out the value of 's' was if it was a '1'. While not at all intuitive, it does work. On one note though, I could not get the expression to evaluate properly when using char data types. I'll chalk that up to my lack of practice with C++ syntax.

#9 M...

M...

  • Members
  • 386 posts
  • OFFLINE
  •  
  • Local time:04:13 AM

Posted 21 August 2008 - 06:29 PM

groovicus,

In your test program, change the initialization value of 's' from 2 to 3:

string s = "3"; // Note initial value
and then tell me what happens. My test run, with no change to the program other than that, displayed "s == 3".

I think the same principle I described before applies here. In this case, the second operand of || is a string constant ("2"). The value of a string constant is the address of the first character in the string. That address will never be zero (i.e., always true).

Additionally, the compiler I use was nice enough to emit the following warning:

prog1.cpp(11) : warning C4127: conditional expression is constant
Line 11 in the program is the same as the original:
if ( s == "1" || "2"){
Basically, the compiler is warning me that I coded something equivalent to "if (false)" or "if (true)".

#10 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:13 AM

Posted 21 August 2008 - 07:27 PM

You are right. Good. I was completely flustered because by my reasoning that should not have evaluated properly. Some programming languages have hokey shortcuts, and in spite of the fact that I couldn't find it in any of the documentation (including Stroustrup's book), I just chalked it up to one of those things, and since my test returned a result that proved that it did work, I didn't consider any further.

Your test was better than mine. :thumbsup: And I believe your explanation is spot on. Visual Studio did not throw an error at all. Curious.

Thanks for the lesson.

#11 thormus

thormus
  • Topic Starter

  • Members
  • 6 posts
  • OFFLINE
  •  
  • Local time:08:13 AM

Posted 21 August 2008 - 08:42 PM

Well that's odd indeed, this use to work for me. Removing the OR statement does make it work, I don't know what to say other than that now I'm bugged with the question as to why this worked in the first place. :thumbsup:

#12 M...

M...

  • Members
  • 386 posts
  • OFFLINE
  •  
  • Local time:04:13 AM

Posted 21 August 2008 - 09:51 PM

groovicus and thormus,

I use an older version (12.00.8804) of the Microsoft C++ compiler. I run it in batch (command line) mode, using a script, with the compiler's warning level option set to its most persnickety value of "-W4" (i.e., complaining about absolutely anything that might be incorrect, improperly coded or questionable). Among other things, the script saves the compilation log file and then post-processes that file to eliminate all the unbelievably numerous warnings resulting from the Microsoft/library include files (over which I have no control). I can then (fairly) easily see the warnings pertaining specifically to my program in the "trimmed" compilation log file, and determine what is important and what can be ignored. And note that these are just warnings, not errors -- compilation and execution will proceed even in the presence of those warnings.

I think the old saying is, "The compiler is your friend."

#13 i am not known

i am not known

  • Members
  • 113 posts
  • OFFLINE
  •  
  • Local time:07:13 AM

Posted 22 August 2008 - 04:45 AM

is there a chance u had made a glich and for somereason its takeing down the program (just a guess not trying to be rude)
I hear a boy only has a certain amount of tears one day the pain and dissapointment happen so often the pain dissapers and the boy becomes a man

#14 M...

M...

  • Members
  • 386 posts
  • OFFLINE
  •  
  • Local time:04:13 AM

Posted 22 August 2008 - 08:49 AM

I also tried the test program using "Microsoft Visual Studio .NET 2003".

Evidently, the default compilation warning level is /W3 and the warning ("C4127: conditional expression is constant") is not issued. If you change the warning level to its highest/strictest value (/W4), the warning message is issued:

Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users