无法在循环中击中数组/变量的价值
cannot give value to struck array/variable in a loop
我正在尝试将坐标给多边形。arrayx
和arrayy
是包含我的X和Y坐标的数组。问题是,我无法将数组的值提供给polygon
点结构。
#include <iostream>
#include <fstream>
#include <string>
struct Point
{
int x,y;
};
int main()
{
Point polygon[]={};
int n=0, arrayx[1024] = {0} , arrayy[1024] = {0};
//input
ifstream iFile;
iFile.open("in.txt");
while(iFile >> arrayx[n] >> arrayy[n])
{
n = n + 1;
}
iFile.close();
int k=n;
for (int j=0; j<= k; j++)
{
polygon[j] = (Point){arrayx[j],arrayy[j]}; //something wrong here
//output
ofstream myfile;
myfile.open ("out3.txt", ios::app);
myfile <<"k = " <<k << "----" <<j << ") polygon.x = "<< polygon[j].x <<" arrayx = "<<arrayx[j] << " polygon[j].y = "<< polygon[j].y <<" arrayy = "<<arrayy[j] << "n";
myfile.close();
}
}
我还尝试了以下更改代码:
polygon[j].x = arrayx[j];
polygon[j].y = arrayy[j];
输入:
260 673
565 735
780 619
746 408
333 400
输出:
k = 260
j = 673
polygon[j].x = 7209071
arrayx = 0
polygon[j].y = 7471220
arrayy = 0
突然 k
将其值更改为260。循环j
变量转到673,(不j = 0,1,2,3,4,5仅一次倾斜和断裂),即使arrays[673]
是空的,polygon
也获得了一些数字。
我对C 没有太多经验,我很难单独解决它。
您将无法分配任何内容,因为数组为空
Point polygon[]={};
这应该引起编译器错误,因为在标准C 中不允许使用0个大小的数组。
相关文章:
- 在循环中按顺序遍历成员变量
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 与 for 循环中的变量混淆
- 如何在循环之外重新输入变量
- 如何使用将字符串拆分为 for 循环中的变量的程序
- 在变量声明中使用 for 循环
- C++ 和 Boost.Python - 如何将变量公开给 python 并在循环中更新它?
- C++ Code 在 for 循环期间不会累积变量中的总和,仅提供最终迭代值
- C++ 在循环中添加计数器变量并再次初始化其值
- (C++)虽然循环一次不起作用,但我引入了多个变量
- 如何添加循环期间分配的变量?
- C++,每个循环初始化一个新的静态变量
- 在 for 循环中更新两个变量时遇到问题C++
- 循环中的变量被设置为下一个数组的元素始终具有相同的内存地址?
- 需要循环帮助以迭代方式添加到程序集中的总和变量
- 在 c++ 中基于范围的 for 循环中使用引用作为控制变量
- 如何在循环中动态创建变量(c++)
- 变量循环范围会导致返回局部变量的地址引用
- ifstream变量循环不打印任何内容
- c++变量/循环问题