|
From: Doug M. <dou...@al...> - 2004-01-23 07:14:44
|
Hi Ken,
It's hard to tell what's going on without any variable declarations or
code comments.
Two things are interesting about your code.
First the hang you are seeing. Do you know that the construct
while (MtgCode0 == "W") MtgType=12;
repeats the assignment 'MtgType=12' over and over, as long as the
expression 'MtgCode0 == "W"' remains true? This line will do one of two
things. It will drop through without doing the assignment if MtgCode0
!= "W". Or, if MtgCode0 == "W", it will hang.
You probably wanted straight-through conditionals rather than loops,
something more like:
switch ((int)*(char *)MtgCode0) {
case 'P':
MtgType=10; TotP++; break;
case 'L':
MtgType=11; TotL++; break;
case 'W':
MtgType=12; TotW++; break;
default: TotOther++;
}
However, if I understand your intent, there may be another problem
lurking in your code snippet. I hope you realize that in the expression
MtgCode0 == "W"
you are checking for equality of the value contained in the variable
MtgCode0 with the _address_ of the literal string "W". This code is not
performing a string match! In C and C++, the runtime value of a quoted
string literal (e.g., "W") is the memory address of its first character
- not the char's value.
But, you may say, I've used code like this before, and the comparison
does work when I've assigned "W" to MtgCode0!
Well, yes, it has happened to work, but only because of a fact about how
the compilers generate code.
I'm guessing that prior to reaching this part of the program, another
part of the code assigned the value "W" to MtgCode0. Right? When you
compiled the program, the compiler noticed that you were using the
string literal "W" in more than one place, and its codegen combined the
first "W" (the one you assigned to MtgCode0) and the second "W" (the one
in the equality test) into the same object. Compilers like to economize.
So, when you earlier assigned "W" to MtgCode0, you were really assigning
its address to MtgCode0. And later, when you checked MtgCode0 against
"W", you were really testing the _address_ contained in MtgCode0 against
the _address_ of [the same code object representing] "W".
This works as long as the compiler likes to economize with its code
generation of string literals, AND is able to do so. That's a risky
assumption to make. Suppose you assign the "W" in one subprogram in one
file, let's call it doubleyoo1.cpp, and test equality with "W" in a
second subprogram in a different file, doubleyoo2.cpp. Suppose further
that you compiled the two at separate times and then linked them.
Dollars to donuts, there will be two copies of "W" in the resulting
binary, and the equality test will fail.
You'd be safer using the single-quoted 'W' (an int value containing the
ASCII code for doubleyoo) rather than "W" (the address of a
compiler-generated, zero-terminated, length-two C string containing the
ASCII code for doubleyoo). If you look back, you'll see that is what I
have done in my suggested replacement for your "while" and "switch" blocks.
Hope this helps,
Doug
> I finally decided to teach myself C++ from "Core C++: A Software Engineering Approach" (2000) by Victor Shtern, "Rescued by C++" 3rd Ed. by Kris Jamsa (which was a Borland Turbo C++ Lite book, and now mostly rely on C/C++ Programmer's Reference 2nd Ed. by Herbert Schildt. And, I finally had a real-world application to write a program. Here's the code excerpt that has me baffled.
>
> for (y=1; y<=i; y++);
> if (Groupset[y].substr(0,3) == Day1.substr)x*3-3,3))
> {
> TotDay[x-1]++;
> while (MtgCode0 == "P") MtgType=10;
> while (MtgCode0 == "L") MtgType=11;
> while (MtgCode0 == "W") MtgType=12;
>
> switch (MtgType)
> {
> case '10': TotP++; break;
> case '11': TotL++; break;
> case '12': TotW++; break;
> default: TotOther++;
> } // terminates Switch
>
> } // terminates if
> }; // terminates for
>
> Up to this point, the program runs fine, but it hangs when it hits "while (MtgCode0 == "W") MtgType=12. In Debug, I set this line and the "switch (MtgType) line as checkpoints. When the "while" stmt is encountered, it turns blue in Debug, and will not go past this point. Debug will not continue; the cursor stays on this line. Note that the Debug screen is not frozen -- I can still set checkpoints, move the cursor, etc. But it will not continue after this point.
>
> What's going on here?
>
> Thx, Ken
> ------------------------------------------------------- The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA. http://www.eclipsecon.org/osdn _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|