gpt4 book ai didi

c++ - 向下移动数组值 1

转载 作者:行者123 更新时间:2023-11-28 02:49:37 25 4
gpt4 key购买 nike

给定一个整数值,表示记录的行号(从 0 开始以当前大小 1 结束),删除学生记录,将所有以下记录上移一个在电子表格中向上排列。如果用户输入了一个超出范围的整数(<0 或 >= 大小),提示“没有这样的行。不能删除行??”代替 ??与行号。

我不确定代码有什么问题。它不会将关于它的数组信息向下移动 1;

void drop(string names[], int sections[], int grades[], int size){
int i;
int drop;
cin >> drop;
if (drop > size){
cout << "No such row. Can not drop row" << drop << " /n";

}else{
for (i = 0; i <= drop; i++){
if (i == drop){
names[drop] = {""};
sections[drop] = {};
grades[drop] = {};
for (i = drop; drop < size-1; i++){
names[i] = names[i + 1];
sections[i] = sections[i + 1];
grades[i] = grades[i + 1];
}
}

}

}
}

最佳答案

试试这个:

void drop(string names[], int sections[], int grades[], int size){
int i;
int drop;
cin >> drop;
if (drop >= size){ //NOTE: >=, not >
cout << "No such row. Can not drop row" << drop << " /n";
return;
}
for(i=drop; i<size-1; i++)
names[i] = names[i + 1];
sections[i] = sections[i + 1];
grades[i] = grades[i + 1];
}
}

当然,如果你想改变数组的大小,使用 vector 会更好。

但是,您不能执行以下操作,因为 name_of_array[drop] 是数组中的一个元素,而不是数组中的一个数组。无论哪种情况,这都是非法代码。

names[drop] = {""};       //should be names[drop] = "";
sections[drop] = {}; // '' '' sections[drop] = 0;
grades[drop] = {}; // '' '' grades[drop] = 0;

关于c++ - 向下移动数组值 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com