From: "Chris McKenzie" Newsgroups: comp.os.msdos.djgpp Subject: Newbie DJGPP compiler question Lines: 110 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2919.6600 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 Message-ID: Date: Thu, 15 Jun 2000 13:34:17 GMT NNTP-Posting-Host: 38.37.12.204 X-Complaints-To: abuse AT earthlink DOT net X-Trace: newsread1.prod.itd.earthlink.net 961076057 38.37.12.204 (Thu, 15 Jun 2000 06:34:17 PDT) NNTP-Posting-Date: Thu, 15 Jun 2000 06:34:17 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com The question I have requires a little explanation first. I am taking C++ in college right now--and I was given some excercise program to write, but it was hard to tell if my output was correct because the screen would simply flash the output and then put me back to the code screen. I came up with the idea of putting an extra "cin" command right before my return 0; in "int main()" so that just prior to finishing, the program stops and lets me look at the output. This works just fine. Then I was reading in my textbook about how you can include "conio.h" to look for a keyboard hit. So I wrote a for-loop to replace my cin statement. The for-loop looks for a keyboard hit prior to returning 0, and just waits. I can hit any key--the for loop condition is met, and I return 0 and exit the program. Here is an example: #include #include file://include so that program will maintain file://user window until I press a key on file://the keyboard using namespace std; int dummy; int main() { int test, num, accum; test = 0; accum = 0; cout << "Please enter a number, and I will give you its sum of squares \t"; cin >> num; while(test < num){ test++; accum = accum + (test * test);} cout << "The sum of squares for " << num << " is " << accum << ". \n"; for(dummy = 1; !kbhit(); dummy++); return 0;} This program compiles and executes perfectly. However, the following program illustrates my problem perfectly: #include #include using namespace std; int main() { int integer1, integer2, result; char operation; //simple calculation program cout << "Please enter a number. \n"; cin >> integer1; cout << "\n Please enter an operator (+, -, *, /). \n"; cin >> operation; cout << "\n Please enter a second number \n"; cin >> integer2; //checks to make sure a correct operator is given before doing calculations if (operation) {if (operation == '+') {result = (integer1 + integer2); cout << integer1 << " " << operation << " " << integer2 << " = " << " " << result;} else if(operation == '-') {result = (integer1 - integer2); cout << integer1 << " " << operation << " " << integer2 << " = " << " " << result;} else if(operation == '*') {result = (integer1 * integer2); cout << integer1 << " " << operation << " " << integer2 << " = " << " " << result;} else if(operation == '/') {result = (integer1 / integer2); cout << integer1 << " " << operation << " " << integer2 << " = " << " " << result;} else {cout << "Invalid Operator! No Operations Performed!";} } else {cout << "Invalied Operator! No Operations Performed!";} int i; for(i=0; !kbhit(); i++); //cin >> operation; return 0; } // close main function In the second program--the order of program execution seems to be messed up. It goes through the If-then sequence just fine--but it doesn't cout the output until AFTER the kbhit condition is met. Given that the cout statement is INSIDE the curly braces INSIDE the else-if conditions, this doesn't make any sense to me. I know that Microsoft's Visual C++ has some kind of prioritization between gets() and puts(), cin and cout such that one command always gets executed first regardless of the actual sequence that they occur in the code. I wonder if perhaps DJGPP has some similar kind of compilation algorithm that causes it to execute my for loop before completing the if then structure. Any ideas? Thanks in advance; Chris McKenzie