I am having an issue getting my output to display as a "standard" integer. Instead I get long negative numbers. Sometimes I just get long positive numbers. The best I got was a positive number 3 digits long... I just would like a "one digit number" :)
After researching this, I have tried char, short, long, signed, unsigned. I have had no luck. I've even tried adding #include <cstdlib> to see if that would help.
If someone could please help me out with this, it would be so much appreciated!
int main()
{
//declare variables
int numOfPax = 0;
int paxOnBoard = 0; //Number of pax on the shuttle
int destPort = 0; //Number of the next port the shuttle will stop
ShuttleBoat twoPortShuttle;
int NUMPORTS = 2; //Number of ports serviced by this shuttle
int MAXPAX = 130; //Max capacity of the shuttle
int currentPort = 0; //Number of the port at which the shuttle is stopped
cout << "\nHello\n";
//calculate current port
twoPortShuttle.calculateCurrentPort(currentPort);
//get current port
currentPort = twoPortShuttle.getCurrentPort();
if (currentPort == 3)
{
currentPort = 1;
}//end if
else if (currentPort != 3)
{
currentPort = currentPort;
}//end else if
cout << "\nCurrent Port number is " << currentPort;
cout << "\n\nHow many passengers will be boarding from Port " << currentPort << "?\n";
//get number of passengers needing to board
twoPortShuttle.getNumOfPax();
//calculate destination port
twoPortShuttle.calculateDestPort(destPort);
//get destination port
destPort = twoPortShuttle.getDestPort();
//board the passengers
paxOnBoard = twoPortShuttle.loadNumOfPax();
cout << "\nLeaving Port " << currentPort << " for Port " << destPort << " with " << paxOnBoard << " passengers on board.\n";
cout << "\nAll Aboard the Shuttle Ferry Boat!\n";
system("pause");
return 0;
}//end of main
///////////////
//HEADER FILE//
///////////////
class ShuttleBoat
{
//Data declaration section
private:
int numOfPax;
int currentPort;
int paxOnBoard;
int destPort;
//Methods declaration section
public:
ShuttleBoat();
int getNumOfPax();
void calculateCurrentPort(int);
int getCurrentPort();
int loadNumOfPax();
void calculateDestPort(int);
int getDestPort();
void moveToPort(int);
//int unloadPax();
};//end class
//Methods implementation section
ShuttleBoat::ShuttleBoat()
{
int NUMPORTS = 2;
int MAXPAX = 130; //Max Capacity
int currentPort = 0;
}
//get number of passengers to board
int ShuttleBoat::getNumOfPax()
{
cin >> numOfPax;
return numOfPax;
}
//determine number of current port
void ShuttleBoat::calculateCurrentPort(int)
{
currentPort += 1;
}
//return current port number
int ShuttleBoat::getCurrentPort()
{
return currentPort;
}
ShuttleBoat::ShuttleBoat()
{ int NUMPORTS = 2;
int MAXPAX = 130; //Max Capacity
int currentPort = 0;
}
You're declaring and initializing local variables which go out of scope when the constructor exits. The members of your class are uninitialzied.
PLEASE USE CODE TAGS (the <> formatting button) when posting code. http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
///////////////
//SOURCE FILE//
///////////////
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
#include "ShuttleBoatHeader.h"
int main()
{
//declare variables
int numOfPax = 0;
int paxOnBoard = 0; //Number of pax on the shuttle
int destPort = 0; //Number of the next port the shuttle will stop
ShuttleBoat twoPortShuttle;
int NUMPORTS = 2; //Number of ports serviced by this shuttle
int MAXPAX = 130; //Max capacity of the shuttle
int currentPort = 0; //Number of the port at which the shuttle is stopped
cout << "\nHello\n";
//calculate current port
twoPortShuttle.calculateCurrentPort(currentPort);
//get current port
currentPort = twoPortShuttle.getCurrentPort();
if (currentPort == 3)
{
currentPort = 1;
}//end if
elseif (currentPort != 3)
{
currentPort = currentPort;
}//end else if
cout << "\nCurrent Port number is " << currentPort;
cout << "\n\nHow many passengers will be boarding from Port " << currentPort << "?\n";
//get number of passengers needing to board
twoPortShuttle.getNumOfPax();
//calculate destination port
twoPortShuttle.calculateDestPort(destPort);
//get destination port
destPort = twoPortShuttle.getDestPort();
//board the passengers
paxOnBoard = twoPortShuttle.loadNumOfPax();
cout << "\nLeaving Port " << currentPort << " for Port " << destPort << " with " << paxOnBoard << " passengers on board.\n";
cout << "\nAll Aboard the Shuttle Ferry Boat!\n";
system("pause");
return 0;
}//end of main
///////////////
//HEADER FILE//
///////////////
class ShuttleBoat
{
//Data declaration section
private:
int numOfPax;
int currentPort;
int paxOnBoard;
int destPort;
//Methods declaration section
public:
ShuttleBoat();
int getNumOfPax();
void calculateCurrentPort(int);
int getCurrentPort();
int loadNumOfPax();
void calculateDestPort(int);
int getDestPort();
void moveToPort(int);
//int unloadPax();
};//end class
//Methods implementation section
ShuttleBoat::ShuttleBoat()
{
int NUMPORTS = 2;
int MAXPAX = 130; //Max Capacity
int currentPort = 0;
}
//get number of passengers to board
int ShuttleBoat::getNumOfPax()
{
cin >> numOfPax;
return numOfPax;
}
//determine number of current port
void ShuttleBoat::calculateCurrentPort(int)
{
currentPort += 1;
}
//return current port number
int ShuttleBoat::getCurrentPort()
{
return currentPort;
}
//load passengers onto shuttle
int ShuttleBoat::loadNumOfPax()
{
paxOnBoard = numOfPax;
return paxOnBoard;
}
//determine number of destination port
void ShuttleBoat::calculateDestPort(int)
{
destPort = currentPort +1;
if (destPort == 3)
{
destPort = 1;
}//end if
elseif (destPort != 3)
{
destPort = destPort;
}//end else if
}
return destination port number
int ShuttleBoat::getDestPort()
{
return destPort;
}
////////////////////
//update current port # to the destination
//currentPort = destPort;