| | |
If Else: Logic Jam?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hello:
Something as seemingly simple as an If\ Else statement is causing me much trouble and I cannot figure out where the error lies. I believe it to be in:
Any hints, help, or shove in the right direction would be surely appreciated.
Regards
sharky_machine
Something as seemingly simple as an If\ Else statement is causing me much trouble and I cannot figure out where the error lies. I believe it to be in:
- The variable scope
- The use of the static variable
- Or, the misplacement of curly brackets (something that has a tendency to trip me up at times)
Any hints, help, or shove in the right direction would be surely appreciated.
Regards
sharky_machine
#include <ctime>
#include <cstdlib>
#include <iostream>
#include "Look.h"
using namespace std;
int checkFlag ;
int* pTest;
Look::Look() {
}
Look::~Look() {
}
void Look::printShipPos()
{
if (checkFlag > 0) {
static int shipPos = 0;
//-------------------------Pointer Test
//pTest = &shipPos;
//cout << pTest << endl;
//cout << *pTest << endl; // Works!
//-------------------------Pointer Test
cout << shipPos << "_shipPos 1"<<endl;
shipPos = shipPos + 228;
cout << shipPos << "_shipPos added"<< endl;
}
//{
//RECHECK all values below
if (shipPos == 228) {
std::cout << "French Polynesia [Out of Transmission Range]"<<endl;
else if (shipPos == 456){
std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl;
}
/* else if ((shipPos >= 456) && (shipPos < 684)){
std::cout << "Pacific Ocean (open waters) [Out of Transmission Range] 4320 miles from Tampa, Florida"<< endl;
}
else if ((shipPos >= 684) && (shipPos < 912)) {
std::cout << "Pacific Ocean (open waters) [In Transmission Range] 3240 miles from Tampa, Florida"<< endl;
} */
.
.
.
//---------------------counter for fly-over access
checkFlag++;
cout <<checkFlag<< endl;
return;
// }
} I think the problem is because when you declare a variable static it affects the lifetime of it. You declare it inside the If brackets { }, so it will be destroyed at the end of the if statement. But, the static variable probably doesn't want to go away. It probably gets filled with information that really is another variable or a pointer, but your debugger still watches that variable as shipPos. Then, every time that function is called, it creates a new shipPos. My knowledge of compilers only goes so far, but I think that might confuse it.
I noticed too that you declare shippos inside an if block { } and try to refer to it in the next if statement, which is in a different block of code. That is probably why it prints out as 0+228 correctly, but then after that block, it is released. Your compiler might give more precedence to { } blocks than to the static variables. Compiler people trying to protect from memory leaks...
One more suggestion -- format your code properly and wayward braces will no longer be a problem. Don't indent 3 tabs then next line 1 tab, then 2 tabs.
Your first brace should be far left and every line after indented 4 spaces. Each and every { indent 4 more spaces sterting with the next line. When you get to a } unindent 4 spaces, then add the } on a line by itself.
Then your code is easier to read:
Your first brace should be far left and every line after indented 4 spaces. Each and every { indent 4 more spaces sterting with the next line. When you get to a } unindent 4 spaces, then add the } on a line by itself.
Then your code is easier to read:
C++ Syntax (Toggle Plain Text)
void Look::printShipPos() { if (checkFlag > 0) { static int shipPos = 0; cout << shipPos << "_shipPos 1"<<endl; shipPos = shipPos + 228; cout << shipPos << "_shipPos added"<< endl; } if (shipPos == 228) { std::cout << "French Polynesia [Out of Transmission Range]"<<endl; else if (shipPos == 456){ std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl; } //---------------------counter for fly-over access checkFlag++; cout <<checkFlag<< endl; return; }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Nanodano & WaltP:
Thank-you again for all of your help and suggestions; I followed everything you told me of and the problem I have been having is fixed.
I appreciate it a lot. Solved.
sharkey_machine
Thank-you again for all of your help and suggestions; I followed everything you told me of and the problem I have been having is fixed.
I appreciate it a lot. Solved.
sharkey_machine
![]() |
Other Threads in the C++ Forum
- Previous Thread: Recursion II
- Next Thread: edit C++ window
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






.
