I'm trying to make a football tournament in C++, in which the user inputs the name of the teams(8 teams) and then each team has to play with the other one 1 time. Firstly, I don't know how to read the team names, I mean I tried to use .getline or just cin of a char array, but then I need to put the teams into the matrix and after the final game my program should print the table. So there's the first question: how to read the names and basically make the program think they are numbers or does it work with just with names, no need to use int? And then the users inputs the result for every game, but here comes the hard part. After all the results have been introduced, the matrix rotates cyclic and then the result stored in the variables(you will see in code victory/losses) overwrites themselves, so at the end, I cannot print the right table. So that's the second question: How can I make them store to the right 'team' while they rotate? Sorry if I didn't quite explain very well how it works, hope you understand it. Cheers!
// FOOTBALL TOURNAMENT
int map[2][4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << "map[" << i << "][" << j << "]= ";
cin >> map[i][j];
}
}
cout << "The map looks like this:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
map[0][0] = 1;
int temp = 0, temp2 = 0, temp3 = 0, temp4 = 0, temp5 = 0, temp6 = 0;
int a, b, c, d, e, f, g, h, round = 0;
int victory_m00(0), losses_m10(0), victory_m10(0), losses_m00(0), victory_m01(0), losses_m11(0), victory_m11(0), losses_m01(0);
int victory_m02(0), losses_m12(0), victory_m12(0), losses_m02(0), victory_m03(0), losses_m13(0), victory_m13(0), losses_m03(0);
do
{
// Insert result for every game
cout << "Enter the result of the first game between " << map[0][0] << " vs. " << map[1][0] << endl;
cin >> a >> b;
if (a > b) {
victory_m00++;
losses_m10++;
}
else if (a < b)
{
victory_m10++;
losses_m00++;
}
cout << "Enter the result of the first game between: " << map[0][1] << " vs. " << map[1][1] << endl;
cin >> c >> d;
if (c > d) {
victory_m01++;
losses_m11++;
}
else if (c < d)
{
victory_m11++;
losses_m01++;
}
cout << "Enter the result of the first game between: " << map[0][2] << " vs. " << map[1][2] << endl;
cin >> e >> f;
if (e > f) {
victory_m02++;
losses_m12++;
}
else if (e < f)
{
victory_m12++;
losses_m02++;
}
cout << "Enter the result of the first game between: " << map[0][3] << " vs. " << map[1][3] << endl;
cin >> g >> h;
if (g > h) {
victory_m03++;
losses_m13++;
}
else if (g < h)
{
victory_m13++;
losses_m03++;
}
round++;
// Map switching
temp = map[1][0];
map[1][0] = map[0][1];
temp2 = map[1][1];
map[1][1] = temp;
temp3 = map[1][2];
map[1][2] = temp2;
temp4 = map[1][3];
map[1][3] = temp3;
temp5 = map[0][3];
map[0][3] = temp4;
temp6 = map[0][2];
map[0][2] = temp5;
map[0][1] = temp6;
// Table calculating and printing ~ also this has to be outside the loop (but at first i wanted to print the table after every 'round'
cout << "This is how the table looks like after the " << round << " round: \n";
cout << map[0][0] << " has: " << victory_m00 << " victory(-ies) and " << losses_m00 << " loss-es!\n";
cout << map[0][1] << " has: " << victory_m01 << " victory(-ies) and " << losses_m01 << " loss-es!\n";
cout << map[0][2] << " has: " << victory_m02 << " victory(-ies) and " << losses_m02 << " loss-es!\n";
cout << map[0][3] << " has: " << victory_m03 << " victory(-ies) and " << losses_m03 << " loss-es!\n";
cout << map[1][0] << " has: " << victory_m10 << " victory(-ies) and " << losses_m10 << " loss-es!\n";
cout << map[1][1] << " has: " << victory_m11 << " victory(-ies) and " << losses_m11 << " loss-es!\n";
cout << map[1][2] << " has: " << victory_m12 << " victory(-ies) and " << losses_m12 << " loss-es!\n";
cout << map[1][3] << " has: " << victory_m13 << " victory(-ies) and " << losses_m13 << " loss-es!\n";
cout << endl;
cout << endl;
} while (map[0][1] != 2);
```
I am currently using a table to find corresponding fuel levels in individual tanks in relation to the total amount of fuel the vessel is holding. The vessel currently has 8 tanks and the fuel is distributed accordingly to the amount of total fuel the vessel is carrying. When given a total fuel amount I need to find or populate approximately where the fuel is at in the 8 tanks at that total fuel amount.
I have tried searching through the 2d array to find the corresponding fuel tanks at a given time but with the chart (2d array), I realized I need to use an index for the left most column, (which is the total fuel) and then find the lower bound row and upper bound row. Afterwards I need to find the average of the two rows each individual elements (fuel tanks). I was able to make if statements and hardcore the math, but I know there is a recursive way to search the left column, find the upper and lower bond row of a given fuel amount and find the amount in each fuel tank. So if I was given a target amount of fuel of ie. 194568, I need to find approximately where the fuel is at in the 8 tanks.
int main()
const int NUM_ROWS = 22;
const int NUM_COLUMNS = 9;
int arr[NUM_ROWS][NUM_COLUMNS] = {
{80000,4500,7500,11700,11700,0,0,0,9200},
{90000,4500,7500,13800,13800,0,0,0,10800 },
{100000,4500,7500,15900,15900,0,0,0,12400},
{110000,4500,7500,18200,18200,0,0,0,13200},
{120000,4500,7500,20400,20400,0,0,0,14400},
{130000,4500,7500,22000,23000,0,0,0,16000},
{140000,4500,7500,22000,26200,0,0,0,19600},
{150000,4500,7500,22000,29400,0,0,0,23200},
{160000,4500,7500,22000,32700,0,0,0,26600},
{170000,4500,7500,23200,35400,0,0,0,28800},
{180000,4500,7500,25300,37500,0,0,0,30400},
{190000,4500,7500,27400,39600,0,0,0,32000},
{200000,4500,7500,29500,41700,0,0,0,33600},
{210000,4500,7500,31800,44300,0,0,0,33800},
{220000,4500,7500,31800,44300,0,0,10000,33800},
{230000,4500,7500,31800,44300,0,3000,15000,35800},
{240000,4500,7500,31800,44300,0,8000,15000,40800},
{250000,4500,7500,31800,44300,0,8000,25000,40800},
{260000,4500,7500,31800,44300,0,8000,35000,40800},
{270000,4500,7500,31800,44300,0,8000,45000,40800},
{280000,4500,7500,31800,44300,0,14000,45000,44800},
{290000,4500,7500,31800,44300,0,18800,46000,49000}, };
int key;
cout << "Enter Fuel Amount:";
cin >> key;
if (key >= 80000 && key < 90000)
{
cout << "L/R_Ext = " << (arr[0][1] + arr[1][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[0][2] + arr[1][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[0][3] + arr[1][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[0][4] + arr[1][4]) / 2 << endl;
cout << "Forward = " << (arr[0][5] + arr[1][5]) / 2 << endl;
cout << "Center = " << (arr[0][6] + arr[1][6]) / 2 << endl;
cout << "Mid = " << (arr[0][7] + arr[1][7]) / 2 << endl;
cout << "Aft = " << (arr[0][8] + arr[1][8]) / 2 << endl;
}
else if (key >= 90000 && key < 100000)
{
cout << "L/R_Ext = " << (arr[1][1] + arr[2][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[1][2] + arr[2][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[1][3] + arr[2][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[1][4] + arr[2][4]) / 2 << endl;
cout << "Forward = " << (arr[1][5] + arr[2][5]) / 2 << endl;
cout << "Center = " << (arr[1][6] + arr[2][6]) / 2 << endl;
cout << "Mid = " << (arr[1][7] + arr[2][7]) / 2 << endl;
cout << "Aft = " << (arr[1][8] + arr[2][8]) / 2 << endl;
}
else if (key >= 100000 && key < 110000)
{
cout << "L/R_Ext = " << (arr[2][1] + arr[3][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[2][2] + arr[3][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[2][3] + arr[3][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[2][4] + arr[3][4]) / 2 << endl;
cout << "Forward = " << (arr[2][5] + arr[3][5]) / 2 << endl;
cout << "Center = " << (arr[2][6] + arr[3][6]) / 2 << endl;
cout << "Mid = " << (arr[2][7] + arr[3][7]) / 2 << endl;
cout << "Aft = " << (arr[2][8] + arr[3][8]) / 2 << endl;
}
cout << "First row:\n";
for (col = 0; col < NUM_COLUMNS; col++) {
cout << arr[0][col] << '\t';
}
cout << "Second row:\n";
for (col = 0; col < NUM_COLUMNS; col++) {
cout << arr[1][col] << '\t';
}
cout << "\n\nSecond column:\n";
for (row = 0; row < NUM_ROWS; row++) {
cout << arr[row][1] << '\n';
}
cout << "\n\nThird column:\n";
for (row = 0; row < NUM_ROWS; row++) {
cout << arr[row][2] << '\n';
}
cout << "\n\nForth column:\n";
for (row = 0; row < NUM_ROWS; row++) {
cout << arr[row][3] << '\n';
}
/*etc....
I expect the outcome when given a target of 125648 it will search the array and return the fuel in each tank for
int LR_Ext;
int LR_Outbrd;
int L1R4;
int L2R3;
int Forward;
int Center;
int Mid;
int Aft;
and perhaps assign them to a variable.
Instead of repeating the entire cout statements in each if case, you could first determine an index which you can use in display code:
int index = 0;
if (key >= 80000 && key < 90000)
index = 0;
else if (key >= 90000 && key < 100000)
index = 1;
else if (key >= 100000 && key < 110000)
index = 2;
else
// You should do some check if an invalid key is provided
// Now display
cout << "L/R_Ext = " << (arr[index][1] + arr[index+1][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[index][2] + arr[index+1][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[index][3] + arr[index+1][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[index][4] + arr[index+1][4]) / 2 << endl;
cout << "Forward = " << (arr[index][5] + arr[index+1][5]) / 2 << endl;
cout << "Center = " << (arr[index][6] + arr[index+1][6]) / 2 << endl;
cout << "Mid = " << (arr[index][7] + arr[index+1][7]) / 2 << endl;
cout << "Aft = " << (arr[index][8] + arr[index+1][8]) / 2 << endl;
I'm programming a checkers game as a form of practice for object-oriented design. The error I'm getting is the following:
First the code:
#include <iostream>
#include <stdio.h>
using namespace std;
class Piece {
public:
int ypos, xpos, index;
string color, kind;
int getY(void)
{
return ypos;
}
int getX(void)
{
return xpos;
}
int adjustY(int change)
{
ypos = ypos + change;
}
int adjustX(int change)
{
xpos = xpos + change;
}
};
int form(void)
{
string p[24];
for (int i = 0; i <= 23; ++i)
{
if (i < 4)
{
Piece p[i];
p[i].color = "white";
p[i].ypos = 7;
p[i].xpos = i * 2;
p[i].index = i;
p[i].kind = "peon";
}
else if (i < 8)
{
int l;
l = i - 4;
Piece p[i];
p[i].color = "white";
p[i].ypos = 6;
p[i].xpos = 1 + (l * 2);
p[i].index = i;
p[i].kind = "peon";
}
else if (i < 12)
{
int m;
m = i - 8;
Piece p[i];
p[i].color = "white";
p[i].ypos = 5;
p[i].xpos = (m * 2);
p[i].index = i;
p[i].kind = "peon";
}
else if (i < 16)
{
int n;
n = i - 12;
Piece p[i];
p[i].color = "black";
p[i].ypos = 0;
p[i].xpos = 1 + (n * 2);
p[i].index = i;
p[i].kind = "peon";
}
else if (i < 20)
{
int pp;
pp = i - 16;
Piece p[i];
p[i].color = "black";
p[i].ypos = 1;
p[i].xpos = (pp * 2);
p[i].index = i;
p[i].kind = "peon";
}
else
{
int q;
q = i - 20;
Piece p[i];
p[i].color = "black";
p[i].ypos = 2;
p[i].xpos = 1 + (q * 2);
p[i].index = i;
p[i].kind = "peon";
}
}
}
char matrix[8][8];
int printt(void)
{
for (int i = 0; i = 7; ++i)
{
for (int j = 0; j = 7; ++j)
{
matrix[i][j] = '_';
}
}
for (int c = 0; c <= 23;++c)
{
int a, b;
a = p[c].ypos;
b = p[c].xpos;
switch(p[c].kind)
{
case "peon":
switch(p[c].color)
{
case "white":
matrix[a][b] = 'o';
break;
case "black":
matrix[a][b] = 'x';
break;
}
break;
case "damen":
switch(p[c].color)
{
case "white":
matrix[a][b] = 'O';
break;
case "black":
matrix[a][b] = 'X';
break;
}
break;
}
}
cout << " 0|1|2|3|4|5|6|7| X Position (column)(j)" << endl;
cout << endl;
cout << "0 " << matrix[0][0] << "|" << matrix[0][1] << "|" << matrix[0][2] << "|" << matrix[0][3] << "|" << matrix[0][4] << "|" << matrix[0][5] << "|" << matrix[0][6] << "|" << matrix[0][7] << endl;
cout << "0 " << matrix[1][0] << "|" << matrix[1][1] << "|" << matrix[1][2] << "|" << matrix[1][3] << "|" << matrix[1][4] << "|" << matrix[1][5] << "|" << matrix[1][6] << "|" << matrix[1][7] << endl;
cout << "0 " << matrix[2][0] << "|" << matrix[2][1] << "|" << matrix[2][2] << "|" << matrix[2][3] << "|" << matrix[2][4] << "|" << matrix[2][5] << "|" << matrix[2][6] << "|" << matrix[2][7] << endl;
cout << "0 " << matrix[3][0] << "|" << matrix[3][1] << "|" << matrix[3][2] << "|" << matrix[3][3] << "|" << matrix[3][4] << "|" << matrix[3][5] << "|" << matrix[3][6] << "|" << matrix[3][7] << endl;
cout << "0 " << matrix[4][0] << "|" << matrix[4][1] << "|" << matrix[4][2] << "|" << matrix[4][3] << "|" << matrix[4][4] << "|" << matrix[4][5] << "|" << matrix[4][6] << "|" << matrix[4][7] << endl;
cout << "0 " << matrix[5][0] << "|" << matrix[5][1] << "|" << matrix[5][2] << "|" << matrix[5][3] << "|" << matrix[5][4] << "|" << matrix[5][5] << "|" << matrix[5][6] << "|" << matrix[5][7] << endl;
cout << "0 " << matrix[6][0] << "|" << matrix[6][1] << "|" << matrix[6][2] << "|" << matrix[6][3] << "|" << matrix[6][4] << "|" << matrix[6][5] << "|" << matrix[6][6] << "|" << matrix[6][7] << endl;
cout << "0 " << matrix[7][0] << "|" << matrix[7][1] << "|" << matrix[7][2] << "|" << matrix[7][3] << "|" << matrix[7][4] << "|" << matrix[7][5] << "|" << matrix[7][6] << "|" << matrix[7][7] << " Y Position (line)(i)" << endl;
cout << endl;
}
int main()
{
form();
printt();
cout << "End of Programm" << endl;
system ("pause");
return 0;
}
Then the error:
In function 'int printt()':
Line 130 Col 7 [Error] 'p' was not declared in this scope
I assume the problem is that the objects were created in an outside function. However, using extern creates more problems than I already have, and creating them outside a function is not possible, for the "for (int i=0;i<=23;++i)" statement needs to happen inside a function.
My question is: How do I call these objects (Piece p[0], Piece p[1], and so on) to the printt() function ?
Thank you very much, sorry if the question is dumb, I am pretty new to programming.
You should put this definition:
Piece p[24];
at the global scope, i.e. for instance just before the definition of f:
Piece p[24];
int form(void)
{
for (int i = 0; i <= 23; ++i)
The way you are referencing these objects in the printt function is correct.
Also, you should remove the
Piece p[i];
statements from the int from() implementation.
Also, you may want to implement a default constructor for your Piece class, to ensure that instances' int fields will be reasonably initialized on construction.