Towers of Hanoi with recursion and vectors - c++

I'm doing Towers of Hanoi for school, which will eventually take 7 discs and I cannot figure out how to use cout statements to display the moves that the discs make. For example, "Move disk 7 from peg1 to peg3." Any help on this would be much appreciated. The cout statements would be in the moveDisk() and Hanoi() functions.
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
using namespace std;
struct pegType
{
vector<int> diskStack;
string pegName;
};
/***********************Function Prototypes************************/
void loadDisk(int totalDisks, vector<int>& startPeg);
void printPeg(vector<int> stack, string name);
void moveDisk(vector<int>& startPeg, vector<int>& goalPeg);
int hanoi(int totalDisks, vector<int>& startPeg, vector<int>& goalPeg, vector<int>& tempPeg);
int main()
{
//Local Variables
pegType peg1, peg2, peg3;
peg1.pegName = "Peg1";
peg2.pegName = "Peg2";
peg3.pegName = "Peg3";
const int NUM_DISKS(3);
int totalMoves = 0;
//Welcome Message
cout << "Welcome to the Tower of Hanoi Simulator. This simulation will run with " << NUM_DISKS << " discs." << endl << endl;
loadDisk(NUM_DISKS, peg1.diskStack);
//Check the conditions of the pegs in the beginning
cout << "The Starting Conditions of the three pegs: ";
cout << endl; printPeg(peg1.diskStack, peg1.pegName);
printPeg(peg2.diskStack, peg2.pegName);
printPeg(peg3.diskStack, peg3.pegName);
cout << endl << "Moves required to move " << NUM_DISKS << " discs from " << peg1.pegName << " to " << peg3.pegName << ": " << endl;
totalMoves = hanoi(NUM_DISKS, peg1.diskStack, peg3.diskStack, peg2.diskStack);
//Ending Conditions of the pegs and disks
cout << endl << "The Ending Conditions of the three pegs: " << endl;
printPeg(peg1.diskStack, peg1.pegName);
printPeg(peg2.diskStack, peg2.pegName);
printPeg(peg3.diskStack, peg3.pegName);
cout << endl << "A stack of " << NUM_DISKS << " can be transfered in " << totalMoves << " moves.";
cout << endl;
system("pause");
return 0;
}
/***********************Function Definitions*****************************/
//Load the discs into the vector
void loadDisk(int totalDisks, vector<int>& startPeg)
{
for (int i = totalDisks; i > 0; i--)
{
startPeg.push_back(i);
}
}
//Print the disk Numbers backwards (3, 2, 1) to the user
void printPeg(vector<int> stack, string name)
{
if (stack.size() > 1)
{
cout << name << " has " << stack.size() << " discs: ";
assert(stack.size() > 0);
for (unsigned int i = 0; i < stack.size(); i++)
{
cout << stack[i] << " ";
}
}
else
{
cout << name << " has " << stack.size() << " discs: ";
}
cout << endl;
}
//Moves the bottom disk to the goal
void moveDisk(vector<int>& startPeg, vector<int>& goalPeg)
{
if (startPeg.size() > 0)
{
int temp = startPeg.back();
startPeg.pop_back();
goalPeg.push_back(temp);
}
}
//A recursive function that handles the disk transfer
//Displays the moves made
int hanoi(int totalDisks, vector<int>& startPeg, vector<int>& goalPeg, vector<int>& tempPeg)
{
int count = 0;
if (totalDisks > 0)
{
count = hanoi(totalDisks - 1, startPeg, goalPeg, tempPeg);
moveDisk(startPeg, goalPeg);
count++;
count += hanoi(totalDisks - 1, tempPeg, goalPeg, startPeg);
}
return count;
}

Use struct Peg as parameters (instead of std::vector) in functions Hanoii() and MoveDisk() so that the function can correctly know the source and destination names for printing.
struct pegType
{
vector<int> diskStack;
string pegName;
};
/***********************Function Prototypes************************/
void loadDisk(int totalDisks, vector<int>& startPeg);
void printPeg(vector<int> stack, string name);
void moveDisk(pegType& source, pegType& dest);
int hanoi(int totalDisks, pegType& source, pegType& dest, pegType& aux);
int main()
{
//Local Variables
pegType peg1, peg2, peg3;
peg1.pegName = "Peg1";
peg2.pegName = "Peg2";
peg3.pegName = "Peg3";
const int NUM_DISKS(3);
int totalMoves = 0;
//Welcome Message
cout << "Welcome to the Tower of Hanoi Simulator. This simulation will run with " << NUM_DISKS << " discs." << endl << endl;
loadDisk(NUM_DISKS, peg1.diskStack);
//Check the conditions of the pegs in the beginning
cout << "The Starting Conditions of the three pegs: ";
cout << endl; printPeg(peg1.diskStack, peg1.pegName);
printPeg(peg2.diskStack, peg2.pegName);
printPeg(peg3.diskStack, peg3.pegName);
cout << endl << "Moves required to move " << NUM_DISKS << " discs from " << peg1.pegName << " to " << peg3.pegName << ": " << endl;
totalMoves = hanoi(NUM_DISKS, peg1, peg3, peg2);
//Ending Conditions of the pegs and disks
cout << endl << "The Ending Conditions of the three pegs: " << endl;
printPeg(peg1.diskStack, peg1.pegName);
printPeg(peg2.diskStack, peg2.pegName);
printPeg(peg3.diskStack, peg3.pegName);
cout << endl << "A stack of " << NUM_DISKS << " can be transfered in " << totalMoves << " moves.";
cout << endl;
system("pause");
return 0;
}
/***********************Function Definitions*****************************/
//Load the discs into the vector
void loadDisk(int totalDisks, vector<int>& startPeg)
{
for (int i = totalDisks; i > 0; i--)
{
startPeg.push_back(i);
}
}
//Print the disk Numbers backwards (3, 2, 1) to the user
void printPeg(vector<int> stack, string name)
{
if (stack.size() > 1)
{
cout << name << " has " << stack.size() << " discs: ";
assert(stack.size() > 0);
for (unsigned int i = 0; i < stack.size(); i++)
{
cout << stack[i] << " ";
}
}
else
{
cout << name << " has " << stack.size() << " discs: ";
}
cout << endl;
}
//Moves the bottom disk to the goal
void moveDisk(pegType& source, pegType& dest)
{
if (source.diskStack.size() > 0)
{
int temp = source.diskStack.back();
source.diskStack.pop_back();
dest.diskStack.push_back(temp);
std::cout << "Moving disk# " << temp << " from " << source.pegName << " to " << dest.pegName << '\n';
}
}
//A recursive function that handles the disk transfer
//Displays the moves made
int hanoi(int totalDisks, pegType& source, pegType& dest, pegType& aux)
{
int count = 0;
if (totalDisks > 0)
{
count = hanoi(totalDisks - 1, source, aux, dest);
moveDisk(source, dest);
count++;
count += hanoi(totalDisks - 1, aux, dest, source);
}
return count;
}
Output:

Related

Access Violation Violation Location With Low Numbers

I'm working on an assignment for school. The code is supposed to read form a file and create an array, then sort the values of the array to output certain info. It works just fine as long as I have 3+ lines of info in the file. If not, I get the following error:
First-chance exception at 0x01305876 in Homework11.exe: 0xC0000005: Access violation reading location 0xcd71b288.
Unhandled exception at 0x01305876 in Homework11.exe: 0xC0000005: Access violation reading location 0xcd71b288.
I can't figure out why, any help would be appreciated. Here's the code:
#include <iostream> //calls the information needed
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std; //sets all unmarked commands to std::
const int ARRSIZE = 1000;
struct Student
{
string firstName;
string lastName;
string id, temp;
double gpa;
};
int readArray(ifstream& ifile, Student arr[]);
void swapElements(Student arr[], int i, int j);
void sortArray(Student arr[], int numberInTheArray);
int main()
{ // Declares the needed variables
double sought, min, max;
int i, ival, returnvar, count = 0, mincount, maxcount;
string filename;
ifstream ifile;
Student arr[ARRSIZE];
cout << "Input File Name: ";//requesting the file name
cin >> filename;
ifile.open(filename.c_str());//opening the file
if (!ifile)//checking if it opened or not
{
cout << endl << "That file does not exist!" << endl;//informing the user it did
return 1;//not open and returning 1
}
cout << "Which number do you want to return? ";//requesting the desired number
cin >> ival;
i = ival - 1;
cout << endl;
returnvar = readArray(ifile, arr);
min = arr[0].gpa;
max = arr[0].gpa;
sought = arr[0].gpa;
while (count < returnvar)
{
if (arr[count].gpa < min)
{
min = arr[count].gpa;
mincount = count;
}
if (arr[count].gpa > max)
{
max = arr[count].gpa;
maxcount = count;
}
if (count == i)
{
sought = arr[count].gpa;
}
count++;
}
if (count == 0)
{
cout << "The file is empty!" << endl;
return 1;
}
cout << "Before Sort:" << endl;
cout << " Min GPA is " << min << " for " << arr[mincount].lastName << "." << endl;
cout << " Max GPA is " << max << " for " << arr[maxcount].lastName << "." << endl;
if (returnvar < ARRSIZE)
{
cout << " WARNING: Only " << returnvar << " numbers were read into the array!" << endl;
}
if (i >= returnvar)
{
cout << " There aren't that many numbers in the array!" << endl << endl;
}
else if (i > ARRSIZE)
{
cout << " " << i << " is bigger than " << ARRSIZE << "!" << endl << endl;
}
else if (i < returnvar)
{
cout << " Value " << ival << " is " << sought << " for " << arr[i].lastName << "." << endl << endl;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sortArray(arr, returnvar);
count = 0;
while (count < returnvar)
{
if (arr[count].gpa < min)
{
min = arr[count].gpa;
mincount = count;
}
if (arr[count].gpa > max)
{
max = arr[count].gpa;
maxcount = count;
}
if (count == i)
{
sought = arr[count].gpa;
}
count++;
}
cout << "After Sort:" << endl;
cout << " Array[0] GPA is " << min << " for " << arr[0].lastName << "." << endl;
cout << " Array[" << (returnvar - 1) << "] GPA is " << max << " for " << arr[(returnvar - 1)].lastName << "." << endl;
if (returnvar < ARRSIZE)
{
cout << " WARNING: Only " << returnvar << " numbers were read into the array!" << endl;
}
if (i >= returnvar)
{
cout << " There aren't that many numbers in the array!" << endl << endl;
}
else if (i > ARRSIZE)
{
cout << " " << i << " is bigger than " << ARRSIZE << "!" << endl << endl;
}
else if (i < returnvar)
{
cout << " Value " << ival << " is " << sought << " for " << arr[i].lastName << "." << endl << endl;
}
return 0;
}
int readArray(ifstream& ifile, Student arr[])
{
int counter = 0;
while ((ifile) && (counter <= ARRSIZE))
{
ifile >> arr[counter].firstName;
ifile >> arr[counter].lastName;
ifile >> arr[counter].id;
ifile >> arr[counter].gpa;
counter++;
}
return (counter - 1);
}
void sortArray(Student arr[], int numberInTheArray)
{
for (int i = 0 ; i < numberInTheArray - 1; i++)
{
for (int j = 0 ; j < numberInTheArray - 1; j++)
{
if ( arr[j].gpa > arr[j + 1].gpa)
{
swapElements(arr, j, j+1);
}
}
}
}
void swapElements(Student arr[], int i, int j)
{
Student temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Please ignore the insanity and comments. Like I said, for an entry level course.
Try replacing counter <= ARRSIZE with counter < ARRSIZE (a rule of thumb in C is: never use <= in operations related to container sizes).
EDIT: also in your main(), you must check that i < ARRSIZE (equivalently, return error if i >= ARRSIZE). At present you seem to accept the case i == ARRSIZE, which is also wrong. And finally, readArray should return counter (that is, one more than the last written index).

vector insert segmentation fault second iteration

I'm actually trying to implement the floyd's warshall algorithm but I met a hard-point. I got a segmentation fault while i'm trying to find the shortest-way in my matrix of sequence. I follow this tutorial:
floyd's warshall algorithm
Everything work well except at the line 124:
chemin.insert(it,sequenceTest[v1-1][temp-1]);
where i get the segfault (You can skip the first part except if you want to know how I implement the algorithm butt the problem is in the second part):
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
int i,j,k;
int sequenceTest [4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
int tempSequenceTest [4][4];
int distanceTest [4][4] = {{0,2,4,100000},{2,0,1,5},{4,1,0,3},{100000,5,3,0}};
int tempDistanceTest[4][4];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<sequenceTest[i][j]<<" ";
}
cout<< endl;
}
cout<< endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout << distanceTest[i][j] << " ";
}
cout<< endl;
}
cout<< endl;
for(k=0;k<3;k++)
{
cout <<endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout << "i: " << i << " j: " << j << endl;
if(i == k)
{
tempSequenceTest[i][j] = sequenceTest[i][j];
tempDistanceTest[i][j] = distanceTest[i][j];
cout << " D " << tempDistanceTest[i][j] << " S " << tempSequenceTest[i][j];
}
if(j == k)
{
tempSequenceTest[i][j] = sequenceTest[i][j];
tempDistanceTest[i][j] = distanceTest[i][j];
cout << " D " << tempDistanceTest[i][j] << " S " << tempSequenceTest[i][j];
}
cout<< endl;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
//cout<< "i: " << i << " j: " << j << " k:" << k << endl;
if(i!=k && j!=k)
{
if(distanceTest[i][j]> distanceTest[i][k] + distanceTest[k][j])
{
tempDistanceTest[i][j] = distanceTest[i][k] + distanceTest[k][j];
cout << distanceTest[i][j] << " > " << distanceTest[i][k] << " + " << distanceTest[k][j] << " = " << tempDistanceTest[i][j] << " " << i << j << endl;
tempSequenceTest[i][j] = k+1;
}
else
{
tempDistanceTest[i][j] = distanceTest[i][j];
tempSequenceTest[i][j] = sequenceTest[i][j];
}
}
}
}
cout<< endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
distanceTest[i][j] = tempDistanceTest[i][j];
sequenceTest[i][j] = tempSequenceTest[i][j];
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<tempSequenceTest[i][j]<<" ";
}
cout<< endl;
}
cout<< endl;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout << tempDistanceTest[i][j] << " ";
}
cout<< endl;
}
}
int v1, v2;
v1 = 1;
v2 = 4;
vector <int> chemin;
vector <int>::iterator it;
chemin.push_back(v1);
chemin.push_back(v2);
int temp = v2;
cout << sequenceTest[0][2];
while(sequenceTest[v1-1][temp-1] != v2)
{
it = chemin.begin() + 1;
cout << "V1: " << v1 << " V2: " << v2 << " Temp: " << temp << endl;
chemin.insert(it,sequenceTest[v1-1][temp-1]);
for(i=0;i<chemin.size();i++)
{
cout << chemin[i];
}
cout << endl;
temp = sequenceTest[v1-1][temp-1];
}
}
Thanks for your attention and for taking the time to help me!
chemin.insert(it,sequenceTest[v1-1][temp-1]);
This invalidates the iterator it, but you keep reusing it in the iterations that follow. If you want to continue inserting at the same position, capture the return value.
it = chemin.insert(it,sequenceTest[v1-1][temp-1]);

"Run-Time Check Error #2 - Stack around the variable "arr" was corrupted" when I use a file with many numbers

This is supposed to read at most 1000 numbers from a file into an array and then analyze them. It works flawlessly unless I use a file with a million numbers in it. I probably because I have an infinite loop, but I can't find where. I'm not supposed to go over 1000 elements.
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
const long N=1000;
using namespace std;
//declaring the functions
int readArray(ifstream& ifile, long arr[]);
void sortArray(long arr[], int numberInTheArray);
int main()
{
//variable declaration
int n=0;
int i=0;
int numberInTheArray=0;
long minimum=0;
long maximum=0;
long arr[N]={0};
ifstream ifile;
string strVar;
cout << "Input File Name: ";
cin >> strVar;
cout << "Which number do you want to return? ";
cin >> i;
cout << endl;
ifile.open(strVar.c_str());
if(!ifile)
{
cout << "That file does not exist!" << endl;
return 1;
}
numberInTheArray = readArray(ifile,arr);
if (numberInTheArray == 0){
cout << "The file is empty!" << endl;
}
else{
maximum = arr[n];
minimum = arr[n];
n++;
while (n<=N){
if (arr[n] <= minimum){
minimum = arr[n];
}
if (arr [n] >= maximum){
maximum = arr[n];
}
n++;
}
cout << "Before Sort:\n" << " Min is {" << minimum << "}.\n" << " Max is {"
<< maximum << "}.\n";
if (i>N){
cout << " " << i << " is bigger than 1000!" << endl;
}
else if (numberInTheArray < N){
cout << " WARNING: Only " << numberInTheArray
<< " numbers were read into the array!" << endl;
if (i <= numberInTheArray){
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
else {
cout << " There aren't that many numbers in the array!" << endl;
}
}
else {
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
sortArray(arr,numberInTheArray);
cout << "\nAfter Sort:\n" << " Min is {" << minimum << "}.\n" << " Max is {"
<< maximum << "}.\n";
if (i>N){
cout << " " << i << " is bigger than 1000!" << endl;
}
else if (numberInTheArray < N){
cout << " WARNING: Only " << numberInTheArray
<< " numbers were read into the array!" << endl;
if (i <= numberInTheArray){
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
else {
cout << " There aren't that many numbers in the array!" << endl;
}
}
else {
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
}
return 0;
}
int readArray(ifstream& ifile, long arr[])
{
int n=0;
long num;
while (n<=N && ifile){
ifile >> arr[n];
n++;
}
n=n-1;
return n;
}
void sortArray(long arr[], int numberInTheArray)
{
int a;
int b;
int temp;
for (a=0; a<numberInTheArray; a++)
{
for (b=0; b<numberInTheArray; b++)
{
if (arr[a] < arr[b])
{
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
}
}
The problem is your loop conditions n <= N. Remember that array indexes goes from zero to size minus one, so the condition should be n < N.

Why Do I have an '=' sign output and 2 smiley faces instead of the correct output? C++

this is an update to show chages, details below.
here is a link to snap shot of output
https://dl.dropboxusercontent.com/u/34875891/wrongoutput.PNG
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
using namespace std;
//create HotelRoom class
class HotelRoom
{
private:
char* ptr_guest;
char room_number[3];
int room_capacity;
int occupancy_status;
double daily_rate;
public:
HotelRoom(char roomNumber[], int roomCapacity, double roomRate, char* ptr_name, int occupancyStatus);
~HotelRoom();
void Display_Number();
void Display_Guest();
int Get_Capacity();
int Get_Status();
double Get_Rate();
int Change_Status(int);
double Change_Rate(double);
};
HotelRoom::HotelRoom(char roomNumber[], int roomCapacity, double roomRate, char* ptr_name, int occupancyStatus)
{
strcpy(room_number, roomNumber);
room_capacity = roomCapacity;
daily_rate = roomRate;
ptr_guest = new char[strlen(ptr_name) + 1];
strcpy(ptr_guest, ptr_name);
occupancy_status = occupancyStatus;
}
HotelRoom::~HotelRoom()
{
cout << endl;
cout << "Destructor Executed";
cout << endl;
delete [] ptr_guest;
}
void HotelRoom::Display_Guest()
{
char* temp = ptr_guest;
while(*temp != '\0')
cout << *temp++;
}
void HotelRoom::Display_Number()
{
cout << room_number;
}
int HotelRoom::Get_Capacity()
{
return room_capacity;
}
int HotelRoom::Get_Status()
{
return occupancy_status;
}
double HotelRoom::Get_Rate()
{
return daily_rate;
}
int HotelRoom::Change_Status(int roomStatus)
{
if(roomStatus <= room_capacity )
{
occupancy_status = roomStatus;
return occupancy_status;
}
else
occupancy_status = -1;
}
double HotelRoom::Change_Rate(double newRate)
{
daily_rate = newRate;
return daily_rate;
}
int main()
{
cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);
//Declare variables to hold data
char roomNumber[3] = {'1','3','\0'};
char guestName[20];
double roomRate = 89.00;
int roomCapacity = 4;
int occupancyStatus = 0;
int status;
int checkOut;
int newCustomer;
//Ask for user input
cout << "What is the guest's name: ";
cin.getline(guestName, 20);
cout << endl;
cout << "How many guests will be staying in the room: ";
cin >> status;
HotelRoom HotelRoom1(roomNumber, roomCapacity, roomRate, guestName, status);
//Display Rooom information
cout << endl;
cout << endl;
if(HotelRoom1.Change_Status(status))
{
cout << endl;
cout << "Guest's Name: ";
HotelRoom1.Display_Guest();
cout << endl;
cout << endl;
cout << "The capacity of this room is " << HotelRoom1.Get_Capacity() << endl;
cout << endl;
cout << "There are " << HotelRoom1.Get_Status() << " guests staying in the room";
}
cout << endl;
cout << endl;
cout << "Your room number is " << HotelRoom1.Display_Number();
cout << endl;
cout << endl;
cout << "The rate for this room is " << HotelRoom1.Get_Rate();
cout << endl;
cout << endl;
//chech this guest out?
cout << "Check this guest out? ('1 = yes' '0' = no) ";
cin >> checkOut;
switch(checkOut)
{
case 1:
HotelRoom1.Change_Status(0);
for(int i = 0; i < 3; ++i )
{
cout << endl;
}
cout << "You have checked out of room number " << HotelRoom1.Display_Number();
cout << endl;
cout << endl;
cout << "The capacity of this room is " << HotelRoom1.Get_Capacity();
cout << endl;
cout << endl;
cout << "There are currently " << HotelRoom1.Get_Status() << " occupants";
cout << endl;
cout << endl;
cout << "The rate of this room was " << HotelRoom1.Get_Rate();
break;
}
//check in new guest?
cout << endl;
cout << endl;
cout << "Check in new guest? ('1 = yes' '0' = no) ";
cin >> newCustomer;
for(int i = 0; i < 3; ++i )
{
cout << endl;
}
switch (newCustomer)
{
case 1:
HotelRoom HotelRoom2(roomNumber, roomCapacity, roomRate, guestName, status);
HotelRoom1.Change_Rate(175.00); //Change rate of room
cout << endl;
cout << "What is the guest's name: ";
cin.getline(guestName, 20);
cout << endl;
cout << "How many guests will be staying in the room: ";
cin >> status;
cout << endl;
cout << endl;
//Display new guest information
if(HotelRoom1.Change_Status(status))
{
cout << endl;
cout << endl;
cout << "The capacity of this room is " << HotelRoom1.Get_Capacity() << endl;
cout << endl;
cout << "There are " << HotelRoom1.Get_Status() << " guests staying in the room";
}
cout << endl;
cout << endl;
cout << "Your room number is " << HotelRoom1.Display_Number();
cout << endl;
cout << endl;
cout << "The rate for this room is " << HotelRoom1.Get_Rate();
cout << endl;
cout << endl;
break;
}
cout << endl;
system("PAUSE");
return 0;
}
this is an update to show chages, details below.
here is a link to snap shot of output
https://dl.dropboxusercontent.com/u/34875891/wrongoutput.PNG
char HotelRoom::Display_Guest()
{
cout << ptr_guest;
}
string HotelRoom::Display_Number()
{
cout << room_number;
}
int HotelRoom::Change_Status(int roomStatus)
{
if(roomStatus <= room_capacity )
{
occupancy_status = roomStatus;
return occupancy_status;
}
else
occupancy_status = -1;
}
These functions claim to be returning values. The first two are not, the last is not under certain conditons. Calling the first two is undefined behavior. Calling Change_Status with roomStatus > room_capacity is also undefined behavior.
There may be other problems with the code, but the elephant in the room is the undefined behavior. Any other debugging while you have undefined behavior is theoretically a waste of time.

my baseball program should run but I just can't find the problem why it won't. look program over

This program uses arrays to hold baseball scores for 9 innings. It calculates the high scoring team for each inning and the overall winner of the game.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int n = 9;
void PrintInput(char[], int[], char[], int[]);
void InningWinner(char[], int[], char[], int[]);
int main()
{
int scores1[n];
int scores2[n];
char team1[n], team2[n];
PrintInput(team1,scores1,team2,scores2);
InningWinner(team1,scores1,team2,scores2);
return 0;
}
void PrintInput(char t1[], int s1[], char t2[], int s2[])
{
cout << "\n********************************************************************\n";
cout << "Team 1: " << t1 << " ";
for (int i = 0; i < n; i++){
cout << setw(5) << s1[i];
}
cout << "\n";
cout << "Team 2: " << t2 << " ";
for (int i = 0; i < n; i++){
cout << setw(5) << s2[i];
}
}
void InningWinner(char t1[], int s1[], char t2[], int s2[])
{
for (int i = 0; i < n; i++){
if (s1[i] > s2[i])
cout << endl << t1 << " Wins Inning " << i + 1 << endl;
else if (s2[i] > s1[i])
cout << endl << t2 << " Wins Inning " << i + 1 << endl;
else if (s1[i] == s2[i])
cout << endl << " Inning " << i+1 << " ends in a TIE" << endl;
}
}
All your arrays are used without explicit initialization, which will produced undefined results.
You need to read values into scores1/2 and teams1/2 before you print them or do calculations. You could read from std::cin as in:
std::cout << "Enter " << n << " scores then press enter: ";
int num_scores_read;
for (num_scores_read = 0; std::cin >> scores1[num_scores_read]; ++num_scores_read)
;
if (!std::cin || num_scores_read < n)
{
std::cerr << "error reading score number " << num_scores_read << '\n';
exit(EXIT_FAILURE);
}
(similar for scores2 etc.)
OR, you could read them from a file (similar to above, but use
#include <fstream>
std::ifstream file(filename);
...as above but use "file" in place of "std::cin"...
OR, just hard code some sample values in your program to get you started:
int scores1[n] = { 1, 3, 5, 1, 3, 5, 4, 5, 3 };