简体   繁体   English

C ++,控制台,cout矢量(二维数组)

[英]C++,console, cout Vector in 2D array

I am relative new to C++ so please have mercy. 我是C ++的新手,所以请留意。 I am trying to cout the start and end of a "vector" in a 2D array 40*20. 我正在尝试在2D数组40 * 20中指出“向量”的开始和结束。 but the "b" dont get placed in with the last 2 for loops. 但是“ b”不会放在最后2个for循环中。 In the end i want to display 2 vectors with the second starting at [0][0] and going the same direction as the one typed in by the user. 最后,我想显示2个矢量,第二个矢量从[0] [0]开始,并与用户输入的方向相同。 I also don't know how to, get the b's to be from the starting point of the line to the end point of the line and not just start and ending point. 我也不知道如何使b从线的起点到线的终点,而不仅仅是起点和终点。 But the main problem is that the x is not changed with the b. 但是主要的问题是x不会随b改变。

Thanks for your help. 谢谢你的帮助。

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int vectorsx;
int vectorsy;
int vectorendx;
int vectorendy;
char display[40][40];
char row;
char column;

typedef struct
{
   double x;
   double y;
}punkt;

typedef struct
{
   punkt PA;
   punkt PE;
}vector;


void query()
{
   cout << "Startpunkt x:";
   cin >> vectorsx;
   cout << "Startpunkt y:";
   cin >> vectorsy;

   cout << "Endpunkt x:";
   cin >> vectorendx;
   cout << "Endpunkt y:";
   cin >> vectorendy;

}

int main()
{
   query();

   vector v;

   v.PA.x = vectorsx;
   v.PA.y = vectorsy;
   v.PE.x = vectorendx;
   v.PE.y = vectorendy;

   vector v2;

   v2.PA.x = 0;
   v2.PA.y = 0;
   v2.PE.x = v.PE.x - v.PA.x;
   v2.PE.y = v.PE.y - v.PA.y;



   for (int row = 0; row < 20; row++)
   {
      for (int column = 0; column < 40; column++)
      {
         display[row][column] = 'x';
      }
   }


   for (int row = 0; row < 20; row++)
   {
      for (int column = 0; column < 40; column++)
      {
         cout << display[row][column];
      }                 
      cout << endl;
   }


   for (row = 0; row < 20; row++)
   {
      for(column = 0; column < 40;column++)
      { 
         display[vectorsx][vectorsy] = 'b';

      }
   }

   for (row = 0; row < 20; row++)
   {
      for (column = 0; column < 40; column++)
      {
         display[vectorendx][vectorendy] = 'b';
      }
   }

   system("Pause");
   return 0;
}

You set all elements of display to 'x' . 您将所有display元素设置为'x' Then you print the contents of dispaly . 然后,打印dispaly的内容。 And then you change the contents of two elements to b . 然后将两个元素的内容更改为b You don't print the array again. 您无需再次打印阵列。

Do the printing after you change the arrays. 更改阵列进行打印。


You also don't need the loops for the changes, you are changing the same element 800 times. 您也不需要循环进行更改,而是将同一元素更改了800次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM