The Logic of This Code is Skewed Perhaps:
Hello:
I am having a bit of problem with logic in a segment (marked in blue) of this code: the indicated code should simply update the variable upon each pass of the function as it runs; the variable value maxes out at 456-- it will not increase by 228 beyond this point of the function's run.
I have not been able to determine why this is.
Any help is very appreciated. Thank-you in advance.
sharky_machine
Look.cpp
#include <cstdlib>
#include <iostream>
#include "Look.h"
using namespace std;
int currPos = 0;
int checkFlag = 0;
static int shipPos1 = 0; // default start position (pos 0)
int shipPos2 = 228;
// 3.80 minutes (228 seconds)
Look::Look() {
}
Look::~Look() {
}
void Look::printShipPos()
{
<strong><strong> if (checkFlag > 1) {
shipPos = shipPos2 + 228;
cout << shipPos << endl;</strong></strong>
}
{
if (shipPos = 228) {
std::cout << "French Polynesia [Out of Transmission Range]"<< endl;
}
else if ((shipPos > 228) && (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;
}
<strong>.
.
.</strong>
//---------------------counter for fly-over access
checkFlag++;
cout <<checkFlag<< endl;
return;
}
}
mattyd
Posting Maven
2,607 posts since Oct 2006
Reputation Points: 105
Solved Threads: 1
I beleive you wanted to update the static variable shipPos1 instead of the variable shipPos which I don't see defined anywhere.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
WolfPack:
Thank-you for your reply and for pointing out my now obvious mistake.
sharky_machine
mattyd
Posting Maven
2,607 posts since Oct 2006
Reputation Points: 105
Solved Threads: 1
Knock knock... do I see somthing missing...
#include <cstdlib>
#include <iostream>
#include "Look.h"
using namespace std;
int currPos = 0;
int checkFlag = 0;
static int shipPos1 = 0; // default start position (pos 0)
int shipPos2 = 228;
// 3.80 minutes (228 seconds)
Look::Look() {
}
Look::~Look() {
}
void Look::printShipPos()
{
if (checkFlag > 1) {
shipPos = shipPos2 + 228;
cout << shipPos << endl;
}
{
// ouch
if (shipPos = 228) {
std::cout << "French Polynesia [Out of Transmission Range]"<< endl;
}
else if ((shipPos > 228) && (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;
}
<strong>.
.
.</strong>
//---------------------counter for fly-over access
checkFlag++;
cout <<checkFlag<< endl;
return;
}
}
[/quote]
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Knock knock... do I see somthing missing...
#include <cstdlib>
#include <iostream>
#include "Look.h"
using namespace std;
int currPos = 0;
int checkFlag = 0;
static int shipPos1 = 0; // default start position (pos 0)
int shipPos2 = 228;
// 3.80 minutes (228 seconds)
Look::Look() {
}
Look::~Look() {
}
void Look::printShipPos()
{
if (checkFlag > 1) {
shipPos = shipPos2 + 228;
cout << shipPos << endl;
}
{
// ouch
if (shipPos = 228) {
std::cout << "French Polynesia [Out of Transmission Range]"<< endl;
}
else if ((shipPos > 228) && (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;
}
<strong>.
.
.</strong>
//---------------------counter for fly-over access
checkFlag++;
cout <<checkFlag<< endl;
return;
}
}
~S.O.S.~ :
Yes, missing "=" in "==" -- thank-you for pointing this out to me.
sharkey_machine
mattyd
Posting Maven
2,607 posts since Oct 2006
Reputation Points: 105
Solved Threads: 1