Basic program for calculating money [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I have a basic assignment and can't get the program right. The assignment is to make a program that displays the minimum amount of banknotes and coins necessary to pay.
#include <iostream>
using namespace std;
int main()
{
int pari;
cin >> pari;
switch (pari)
{
case 1: cout << pari/5000 << "*5000" << endl;
break;
case 2: cout << pari/1000 << "*1000" << endl;
break;
case 3: cout << pari/500 << "*500" << endl;
break;
case 4: cout << pari/100 << "*100" << endl;
break;
case 5: cout << pari/50 << "*50" << endl;
break;
case 6: cout << pari/10 << "*10" << endl;
break;
case 7: cout << pari/5 << "*5" << endl;
break;
case 8: cout << pari/2 << "*2" << endl;
break;
case 9: cout << pari/1 << "*1" << endl;
break;
default: cout << "WRONG";
}
return 0;
}
For example:
Input:
54321
Output:
10x5000
4x1000
0x500
3x100
0x50
2x10
0x5
0x2
1x1
I tried with switch case, with if statements, but nothing works.

To get the kind of output you have shown, use logic that looks more like this instead:
#include <iostream>
using namespace std;
int main()
{
int pari;
cin >> pari;
cout << pari/5000 << "*5000" << endl;
pari %= 5000;
cout << pari/1000 << "*1000" << endl;
pari %= 1000;
cout << pari/500 << "*500" << endl;
pari %= 500;
cout << pari/100 << "*100" << endl;
pari %= 100;
cout << pari/50 << "*50" << endl;
pari %= 50;
cout << pari/10 << "*10" << endl;
pari %= 10;
cout << pari/5 << "*5" << endl;
pari %= 5;
cout << pari/2 << "*2" << endl;
pari %= 2;
cout << pari/1 << "*1" << endl;
return 0;
}
Online Demo
Which can be simplified if you put the banknotes in an array and loop through it, eg:
#include <iostream>
using namespace std;
int main()
{
const int bankNotes[] = {5000, 1000, 500, 100, 50, 10, 5, 2, 1};
const int numBankNotes = sizeof(bankNotes)/sizeof(bankNotes[0]);
int pari;
cin >> pari;
for (int i = 0; i < numBankNotes; ++i) {
cout << pari/bankNotes[i] << "*" << bankNotes[i] << endl;
pari %= bankNotes[i];
}
return 0;
}
Online Demo

I have written several versions for you here.
Hopefully it will help you to understand the procedure.
Version 1
This is a navie version. This is how we would do it if we were doing it by hand.
int main()
{
int input_value = 0;
std::cin >> input_value; // First we get the input.
// We start with the highest value banknote.
int value = input_value;
int const number_of_5000_notes = value / 5000; // How many of these notes do
// we need?
value = value % 5000; // Now calculate the rest.
int const number_of_1000_notes = value / 1000; // How many of these notes do
// we need?
value = value % 1000; // Now calculate the rest.
int const number_of_500_notes = value / 500;
value = value % 500;
int const number_of_100_notes = value / 100;
value = value % 100;
int const number_of_50_notes = value / 50;
value = value % 50;
int const number_of_10_notes = value / 10;
value = value % 10;
int const number_of_5_notes = value / 5;
value = value % 5;
int const number_of_2_notes = value / 2;
value = value % 2;
int const number_of_1_notes = value;
// At the end we write the output
std::cout << "Input: " << input_value << std::endl;
std::cout << "Output:" << std::endl;
std::cout << number_of_5000_notes << " x 5000" << std::endl;
std::cout << number_of_1000_notes << " x 1000" << std::endl;
std::cout << number_of_500_notes << " x 500" << std::endl;
std::cout << number_of_100_notes << " x 100" << std::endl;
std::cout << number_of_50_notes << " x 50" << std::endl;
std::cout << number_of_10_notes << " x 10" << std::endl;
std::cout << number_of_5_notes << " x 5" << std::endl;
std::cout << number_of_2_notes << " x 2" << std::endl;
std::cout << number_of_1_notes << " x 1" << std::endl;
return 0;
}
Version 2
This is a more advanced version
int main()
{
int value = 0;
std::cin >> value; // Get input
// Check input
if (value == 0)
{
std::cout << "No value or 0 has been entered";
return 0;
}
// Output on the fly
std::cout << "Input: " << value << std::endl;
std::cout << "Output:" << std::endl;
// loop over a sorted list of banknotes.
for (auto note_value_ent : {5000, 1000, 500, 100, 50, 10, 5, 2, 1})
{
int const number_of_notes = value / note_value_ent;
value %= note_value_ent;
std::cout << number_of_notes << " x " << note_value_ent << std::endl;
}
return 0;
}
Both versions give the same result (except in the case of an invalid entry).

Related

Globalisation of variables within code block

I am having trouble with using the array/matrix which was created within the first if statement (if (choice == 1)) at a later point within the code. I would like to edit the array (when I mention if (choice == 2), but the array created within the first "if" code block is localised. How can I get around this?
#include "pch.h"
#include <iostream>
#include <vector>
int main()
{
if (choice == 1)
{
std::cout << "Choose a letter from A to C, where A = 1" << std::endl;
short matNamechoice;
std::cin >> matNamechoice;
if (matNamechoice == 1)
{
bool matAlive = true;
std::vector<int> matrixA(0);
std::cout << "How many elements would you like to add to Matrix" << matNamechoice << "?" << std::endl;
short matAelements;
std::cin >> matAelements;
std::cout << "For the " << matAelements << "elements that you have created, please choose \
each element value: " << std::endl;
for (int n = 0; n < matAelements; ++n)
{
std::cout << "ELEMENT NO " << n << ":" << std::endl;
int elementValue = 0;
std::cin >> elementValue;
matrixA.push_back(elementValue);
}
std::cout << matAelements << " elements have been appended to matrixA." << std::endl;
}
if (matNamechoice == 2)
{
bool matBLive = true;
std::vector<int> matrixB(0);
std::cout << "How many elements would you like to add to Matrix " << matNamechoice << "?" << std::endl;
short matBelements;
std::cin >> matBelements;
std::cout << "For the " << matBelements << "elements that you have created, please choose \
each element value: " << std::endl;
for (int n = 0; n < matBelements; ++n)
{
std::cout << "ELEMENT NO " << n << ":" << std::endl;
int elementValue = 0;
std::cin >> elementValue;
matrixB.push_back(elementValue);
}
std::cout << matBelements << " elements have been appended to matrixB." << std::endl;
}
if (matNamechoice == 3)
{
bool matCLive = true;
std::vector<int> matrixC(0);
std::cout << "How many elements would you like to add to Matrix" << matNamechoice << "?" << std::endl;
short matCelements;
std::cin >> matCelements;
std::cout << "For the " << matCelements << "elements that you have created, please choose \
each element value: " << std::endl;
for (int n = 0; n < matCelements; ++n)
{
std::cout << "ELEMENT NO " << n << ":" << std::endl;
int elementValue = 0;
std::cin >> elementValue;
matrixC.push_back(elementValue);
}
std::cout << matCelements << " elements have been appended to matrixC. \n \n" << std::endl;
}
}
if (choice == 2 && (matAlive == true))
std::cout << "LOADING MATRICIES..." << std::endl;
for (int n = 10; n <= 100; n += 10)
{
std::cout << n << "%" << std::endl;
}
std::cout << "MATRIX DATA LOAD SUCCESSFUL..." << std::endl;
std::cout << "Enter 1 to edit a Matrix. Enter 2 to print a Matrix. Enter 3 to clear a Matrix." << std::endl;
short userChoice = 0;
std::cin >> userChoice;
}
int x = 0;
if (x < 100)
{
int y = 1;
}
return 0;
}
Move the variable outside of inner block:
bool matAlive = false;
std::vector<int> matrixA(0);
if (choice == 1) {
if (matNamechoice == 1) {
matAlive = true;
// ...
}
}
// ...
if (choice == 2 && (matAlive == true)) {
// ...
}

calculating scores and total questions asked

I'm trying to output the score for each addition, subtraction, multiplication, division in the end test function. and the total of the question asked.
**Any help would be appreciated
BTW this is only using local functions and is passing the scores by reference. IT's supposed to output the number of answers that you answer correctly.
Also is there a way where you can prevent the program from letting you select the choices more than once?
here's what I got so far:
#include < iostream >
#include < iomanip >
#include < stdlib.h >
#include < time.h >
#include < stdlib.h >
using namespace std;
int addition(int addscore);
int subtraction(int subscore);
int multiplication(int multiscore);
int division(int divscore);
int endtest(int & addscore, int & subscore, int & multiscore, int & divscore);
main() {
int end_final = 0;
do {
int addscore, subscore, multiscore, divscore;
char choice;
cout << "A- " << "Addition\n";
cout << "B- " << "Subtraction\n";
cout << "C- " << "Multiplication\n";
cout << "D- " << "Division\n";
cout << "E- " << "End of Test\n";
cin >> choice;
cout << endl << endl;
switch (choice) {
case 'A':
case 'a':
addition(addscore);
break;
case 'B':
case 'b':
subtraction(subscore);
break;
case 'C':
case 'c':
multiplication(multiscore);
break;
case 'D':
case 'd':
division(divscore);
break;
case 'E':
case 'e':
endtest(addscore, subscore, multiscore, divscore);
break;
}
}
while (end_final != 1);
return 0;
}
int addition(int addscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
int total = 0;
for (int i = 1; i <= 5; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
addscore++;
} else {
cout << "Incorrect \n";
}
}
}
int subtraction(int subscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen subtraction\n";
int randnum1, randnum2;
int total = 0;
while (total != 5) {
randnum1 = rand() % 20 + 1;
randnum2 = rand() % 20 + 1;
if (randnum1 >= randnum2) {
cout << randnum1 << " " << "-" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 - randnum2) {
cout << endl;
cout << "Correct!\n ";
subscore++;
} else {
cout << "Incorrect\n ";
}
}
}
}
int multiplication(int multiscore) {
int iRandom;
int total = 0;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen Multiplication\n";
int randnum1, randnum2;
for (int i = 1; i <= 5; i++) {
randnum1 = rand() % 20 + 1;
randnum2 = rand() % 20 + 1;
cout << randnum1 << " " << "x" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 * randnum2) {
cout << endl;
cout << "Correct! \n";
multiscore++;
} else {
cout << "Incorrect\n ";
}
}
}
int division(int divscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen Division\n";
int randnum1, randnum2;
int total = 0;
while (total != 5) {
randnum1 = rand() % 13 + 1;
randnum2 = rand() % 13 + 1;
if (randnum1 % randnum2 == 0) {
cout << randnum1 << " " << "/" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 / randnum2) {
cout << endl;
cout << "Correct! \n";
divscore++;
} else {
cout << "Incorrect\n ";
}
}
}
}
int endtest(int & addscore, int & subscore, int & multiscore, int & divscore) {
int total = 0;
cout << endl << endl;
cout << "Addition" << " " << addscore++ << endl;
cout << "Subtraction" << " " << subscore << endl;
cout << "Multiplication" << " " << multiscore << endl;
cout << "Division" << " " << divscore << endl;
cout << "Total" << " " << total << endl;
}
To clarify the code, declare an enum
enum { ADDITION=0, SUBTRACTION, MULTIPLICATION, DIVISION };
You could have global arrays
int used[] = { 0,0,0,0 }; // not used
int total[] = { 0,0,0,0 }; // total questions per operation
int score[] = { 0,0,0,0 }; // score per operation
Then, doing it for 'addition', in the do {
if ( ! used[ADDITION]) cout << "A- " << "Addition\n";
then in the switch
case 'A':
case 'a':
used[ADDITION] = 1; // tells addition was used
addition(addscore);
break;
The addition code becomes
int addition(int addscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
total[ADDITION] = 0; // should not be
score[ADDITION] = 0; // ...necessary (but in case you call it again)
for (int i = 0; i < 5 ; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total[ADDITION]++; // total is incremented for Addition
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
score[ADDITION]++; // score is incremented
} else {
cout << "Incorrect \n";
}
}
}
endtest becomes
int endtest() {
cout << endl << endl;
if (used[ADDITION]) cout << "Addition" << " " << score[ADDITION] << " / " << total[ADDITION] << endl;
// same for SUBTRACTION ...
int grandtotal = total[ADDITION] + ....;
cout << "Grand Total" << " " << grandtotal << endl;
}
endtest is declared, and called like that
int endtest();
...
endtest();
Since this is C++, you could also make a class Operation, then one subclass per operation, holding the name of the operation and overriding a 'execute' method that does the specific operation, then declare an array of the parent class that holds an instance of each operation ...
Based on OP's comments, after the do {
// initialize variables to zero
int addscore=0, subscore=0, multiscore=0, divscore=0;
in the switch
case 'A':
case 'a':
used[ADDITION] = 1; // tells addition was used
addition( &addscore ); // <== gives the pointer to that variable
break;
then in addition code
int addition(int *addscore) { // <== gets a pointer
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
total[ADDITION] = 0; //
score[ADDITION] = 0; // using your way, this is not necessary anymore
for (int i = 0; i < 5 ; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total[ADDITION]++; // total is incremented for Addition
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
// score[ADDITION]++; // score is incremented (see above)
*addscore++; // <== increment variable pointed to by pointer
} else {
cout << "Incorrect \n";
}
}
}

Getting large negative numbers printing arrays

Hello Im getting negative valhes when trying to output some variables to the screen.
Ive looked this up and in most cases its an uninitialized variable but I cant find anything wrong.
Saying I have too much code to text ratio but I really dont know how to reiterate so heres some filler.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int Input;
int WepType;
string Weapon;
string WeaponType;
int Stats[3];
int AtkStats[4];
cout << "Pick your weapon.\n" << endl;
cout << "{===================================================}\n";
cout << "[ ]\n";
cout << "[ Iron Longsword Sharp Fishing Spear ]\n";
cout << "[ 1 ]\n";
cout << "[ ]\n";
cout << "[ Ornate Hunting Bow ]\n";
cout << "[ ]\n";
cout << "{===================================================}\n";
//Weapon Selection
cin >> Input;
if(Input == 1)
{
WepType = 1;
Weapon = "Iron Longsword";
WeaponType = "OneHanded";
Stats[0] = Stats[0] + 10;
Stats[1] = Stats[1] + 0;
Stats[2] = Stats[2] + 0;
AtkStats[0] = AtkStats[0] + 10;
AtkStats[1] = AtkStats[1] + 0;
AtkStats[2] = AtkStats[2] + 0;
AtkStats[3] = AtkStats[3] + 0;
cout << "Weapon = " << Weapon << endl;
cout << "Weapon Type = " << WeaponType << endl;
cout << "Health = " << Stats[0] << endl;
cout << "Physical = " << AtkStats[0] << endl;
cout << "Light = " << AtkStats[1] << endl;
cout << "Dark = " << AtkStats[2] << endl;
cout << "Temporal = " << AtkStats[3] << endl;
}
return 0;
}
The problem is here:
int Stats[3];
int AtkStats[4];
You should do:
int Stats[3] = {0, 0, 0};
int AtkStats[4] = {0, 0, 0, 0};
Or as BlastFurnace pointed out in the comments (which I forgot about):
int Stats[3] = {}; // Initialize all elements to zero.
int AtkStats[4] = {};
In order to initialize the values. Right now they are just random junk, so when you assign, you get errors.

Unhandled exception at 0x012B1CA9

I am new to C++ and am trying to build a simple program that with the users input to proceed will generate a random left or right. I had the program working correctly until I added in the array to try and store each item as I have to output them as soon and the user would like to exit the loop. The program seems to compile fine but at run time I receive "Unhandled exception at 0x012B1CA9" Any help would be greatly appreciated.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = '100';
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
for (int i = 1; i < MAX; i++)
{
srand(time(NULL));
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
You have a multi-character constant here... and the behavior doesn't go as expected...
Change this line
const int MAX = '100';
to
const int MAX = 100;
Note the removed single quotes.
And secondly, I will advice you to remove the Seed of the C random generator from the for loop because, you'll likely get the same values from the rand() if you always call it immediately after seeding...
But preferable use the algorithm from C++'s random header
Here is a corrected version of your original code....
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = 100; // <---changed
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
srand(time(NULL)); //< moved to here
for (int i = 0; i < MAX; i++) // <-- modified starting index
{
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
I think that it is basically good idea to read more about C data types and declaration. Your error:
const int MAX = '100' should be const int MAX = 100 without any quotes. C++ does implicit conversion from character literals to int.

Comapring two vector elements and get EXC_BAD_ACCESS(code=1, address = 0x0) error

I am writing a program that needs to compare two elements at a time (of a 9 element vector) to find the lowest element. To do this I created a Sort function that acts like a Bubblesort and at the end of the sort I will take the first element of the vector (Which will be the lowest). My problem is that Xcode is telling me my equation is pointing to NULL. Any ideas?
void ParkingLot::GateA(){
vector<int> AvailSpots (9);
AvailSpots[0] = 40;
AvailSpots[1] = 20;
AvailSpots[2] = 50;
AvailSpots[3] = 30;
AvailSpots[4] = 55;
AvailSpots[5] = 15;
AvailSpots[6] = 20;
AvailSpots[7] = 33;
AvailSpots[8] = 27;
vector<string> Lot (9);
Lot[0] = "A";
Lot[1] = "B";
Lot[2] = "C";
Lot[3] = "D";
Lot[4] = "E";
Lot[5] = "F";
Lot[6] = "G";
Lot[7] = "H";
Lot[8] = "I";
vector<int> Cost (9);
Cost[0] = 25.00;
Cost[1] = 22.50;
Cost[2] = 20.00;
Cost[3] = 22.50;
Cost[4] = 20.00;
Cost[5] = 17.50;
Cost[6] = 20.00;
Cost[7] = 17.50;
Cost[8] = 15.00;
vector<int> Distance (9);
Distance[0] = 10;
Distance[1] = 20;
Distance[2] = 30;
Distance[3] = 20;
Distance[4] = 30;
Distance[5] = 40;
Distance[6] = 30;
Distance[7] = 40;
Distance[8] = 50;
int choice2;
cout << "-------------------------------------------------------------------------" << endl;
cout << setw(50) << "GATE A: Lot Information" << endl;
cout << "-------------------------------------------------------------------------" << endl;
cout << "LOT ID : MAX CAPACITY : AVAILABLE SPOTS : COST($DD.CC) : DISTANCE" << endl;
cout << "-------------------------------------------------------------------------" << endl;
cout << Lot[0] << ": 40 : "<<AvailSpots[0]<<": "<<setw(20)<<" "<<Cost[0]<<" : " <<Distance[0] << endl;
cout << Lot[1] << ": 20 : "<<AvailSpots[1]<<": "<<setw(20)<<" "<<Cost[1]<<" : " <<Distance[1] << endl;
cout << Lot[2] << ": 50 : "<<AvailSpots[2]<<": "<<setw(20)<<" "<<Cost[2]<<" : " <<Distance[2] << endl;
cout << Lot[3] << ": 30 : "<<AvailSpots[3]<<": "<<setw(20)<<" "<<Cost[3]<<" : " <<Distance[3] << endl;
cout << Lot[4] << ": 55 : "<<AvailSpots[4]<<": "<<setw(20)<<" "<<Cost[4]<<" : " <<Distance[4] <<endl;
cout << Lot[5] << ": 15 : "<<AvailSpots[5]<<": "<<setw(20)<<" "<<Cost[5]<<" : " <<Distance[5] <<endl;
cout << Lot[6] << ": 20 : "<<AvailSpots[6]<<": "<<setw(20)<<" "<<Cost[6]<<" : " <<Distance[6] << endl;
cout << Lot[7] << ": 33 : "<<AvailSpots[7]<<": "<<setw(20)<<" "<<Cost[7]<<" : " <<Distance[7] << endl;
cout << Lot[8] << ": 27 : "<<AvailSpots[8]<<": "<<setw(20)<<" "<<Cost[8]<<" : " <<Distance[8] << endl;
cout << " Total : 290 : "<<TotalAvailSpots<<":" << endl;
cout << "Select a criteria to allot a parking lot :" << endl;
cout << "1. Based on Cost - Cheapest Parking Lot" << endl;
cout << "2. Based on Distance - Closest to Stadium" << endl;
cout << "0. EXIT" << endl;
cout << "Enter Option (1-2): " << endl;
cin >> choice2;
if (choice2 == 1) {
ParkingLot::Sort();
}
}
int ParkingLot::Sort(){
int cost;
for (int i = 0; i < 8; i++){
if (Cost[i] > Cost[i + 1]) { /////////////ERROR APPEARS HERE Thread1:EXC_BAD_ACCESS(code=1, address=(0x0)
ParkingLot::Swap(i, i+1);
}
}
cout << "Cost of parking lot " << Cost.front() << endl;
cin >> cost;
return cost;
}
void ParkingLot::Swap(int a, int b){
int tmp = Cost[a];
Cost[a] = Cost[b];
Cost[b] = tmp;
}
The variables you define in GateA are local to the function and hiding any member variables with the same names.
So the Cost vector you're using in Sort and Swap is empty because it is a different one from the one you built in GateA.
Remove the line
vector<int> Cost (9);
from GateA and instead call Cost.resize(9) before assigning the elements, or build the vector using push_back rather than direct indexing.