C++ 当数组具有不同的名称时,为什么增量变量会更改数组的值
谁能帮帮我吗。我正努力在代码中找到为什么B列中的最后一个值总是递增1。我已经写了一些代码,因为它的作业今天到期。我也不明白为什么B列中的最后一个值不等于196,因为在重置函数中,它将数组中的所有值都设置为196。如有任何建议,将不胜感激。先谢谢你C++ 当数组具有不同的名称时,为什么增量变量会更改数组的值,c++,arrays,for-loop,while-loop,boolean,C++,Arrays,For Loop,While Loop,Boolean,谁能帮帮我吗。我正努力在代码中找到为什么B列中的最后一个值总是递增1。我已经写了一些代码,因为它的作业今天到期。我也不明白为什么B列中的最后一个值不等于196,因为在重置函数中,它将数组中的所有值都设置为196。如有任何建议,将不胜感激。先谢谢你 #include <iostream> //includes cin cout #include <iomanip> using namespace std; //setting up the environment
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main() { //main starts
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats) {
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
if (i == 0) {
cout << " " << static_cast<char>(j + 65) << " ";
} else {
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++) {
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats) {
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS))) {
if (seats[(seatChoiceNumber)][(letter - 65)] == 82) {
} else {
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats) {
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats) {
printAllSeats(seats);
anyFreeSeats = false;
}
}
} else {
}
}
#include//includes cin cout
#包括
使用名称空间std//建立环境
常量整数行数=3;
const int NUMBER_OF_COLUMNS=3;
void printalseats(int seats[行数][列数];
无效重置(int seats[行数][列数];
void askForUsersSeat(int seats[行数][列数]、int和SeatCountNumber、bool和anyFreeSeats);
bool已满(int seats[行数][列数];
bool isEmpty(int seats[行数][列数];
int main(){//main启动
int maxSeats;
int席位[行数][列数];
int SeatCountNumber=0;
bool-anyfreesets;
重置(座位);
anyfreesets=true;
SeatCountNumber=0;
while(任意自由吃){
打印所有座椅(座椅);
askForUsersSeat(座位、座位编号、任意自由座位);
}
系统(“暂停”);
返回0;
}//主要目的
void printalseats(int seats[行数][列数]){
cout我稍微整理了一下代码。你似乎在注释中找到了答案,所以我只是做了一些缩进。试着在代码中消除空白(请注意,我在这里放的也不是完美的,但你明白了)。干净易读的代码不仅能让您变得更好,而且随着您在行业中的地位越来越高,其他人也开始阅读和编写您的代码,拥有干净易读的代码真的很有帮助:)
#include//includes cin cout
#包括
使用命名空间std;//设置环境
常量整数行数=3;
const int NUMBER_OF_COLUMNS=3;
void printalseats(int seats[行数][列数];
无效重置(int seats[行数][列数];
void askForUsersSeat(int seats[行数][列数]、int和SeatCountNumber、bool和anyFreeSeats);
bool已满(int seats[行数][列数];
bool isEmpty(int seats[行数][列数];
int main()
{
int maxSeats;
int席位[行数][列数];
int SeatCountNumber=0;
bool-anyfreesets;
重置(座位);
anyfreesets=true;
SeatCountNumber=0;
while(任意自由吃)
{
打印所有座椅(座椅);
askForUsersSeat(座位、座位编号、任意自由座位);
}
系统(“暂停”);
返回0;
}//主要目的
void printalseats(int seats[行数][列数])
{
cout我不知道,但我真的不知道如何使用代码块我很抱歉我只是需要一些帮助我会尝试编辑它,这样它更易于阅读@PiotrNycz它有效地摆脱了那个有趣的角色!谢谢you@PiotrNycz当阵列为3x3时,打印出来的矩阵为3x2。我应该更改什么我已经解决了所有问题谢谢@PiotrNycz曾经说过你不需要
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main()
{
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats)
{
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++)
{
if (i == 0)
{
cout << " " << static_cast<char>(j + 65) << " ";
}
else
{
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++)
{
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats)
{
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS)))
{
if (seats[(seatChoiceNumber)][(letter - 65)] == 82)
{
}
else
{
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats)
{
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats)
{
printAllSeats(seats);
anyFreeSeats = false;
}
}
}
else {
}
}