简体   繁体   中英

C++,console, cout Vector in 2D array

I am relative new to C++ so please have mercy. I am trying to cout the start and end of a "vector" in a 2D array 40*20. but the "b" dont get placed in with the last 2 for loops. 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. 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. But the main problem is that the x is not changed with the 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' . Then you print the contents of dispaly . And then you change the contents of two elements to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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