Issues with Integers...

Hello,

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!

(Here's my code)

///////////////
//SOURCE FILE//
///////////////

#include <iostream>
#include <string>
#include <iomanip>

using namespace 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

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;
}

//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

else if (destPort != 3)
{
destPort = destPort;

}//end else if
}

return destination port number
int ShuttleBoat::getDestPort()
{
return destPort;
}

////////////////////
//update current port # to the destination
//currentPort = destPort;
Last edited on
You have a problem in you default constructor.
1
2
3
4
5
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.
There you go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
///////////////
//SOURCE FILE//
///////////////

#include <iostream>
#include <string>
#include <iomanip>

using namespace 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

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;
}

//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

else if (destPort != 3)
{
destPort = destPort;

}//end else if
}

return destination port number
int ShuttleBoat::getDestPort()
{
return destPort;
}

////////////////////
//update current port # to the destination
//currentPort = destPort; 


Ideally, you would indent properly as well...
That makes sense and it worked! I will be sure to use code tags too going forward. Thank you again
Topic archived. No new replies allowed.