Entering and printing a vector's data

1

I have this code

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <cstring>

using namespace std;

int i, Num1;

int main()
{int Columna;
int ValorIngresado;
int FilaA [9],a;

    for (a=0;a<9;a++)
     {
       FilaA[a]=0;
     }

    printf("Digite la columna:\n");
    scanf("%d", &Columna);
   printf("Digite un valor entre 1-9 que desea ingresar en la posicion [%d]: \n\t", Columna);
   scanf("%d", &ValorIngresado);
   FilaA[Columna]=ValorIngresado;
  printf("%d  %d  %d  %d  %d  %d  %d  %d  "), FilaA[0], FilaA[1], FilaA[2], FilaA[3], FilaA[4], FilaA[5], FilaA[6], FilaA[7], FilaA[8];}

Where in summary, I need to initialize all the values of the vector to 0, and when asking for the column data (The vector box) and the entered value, so that 0 of the "Column" box changes to that data of "ValueIngressed", but in the final impression it gives something that I do not see sense, they change 4 values instead of only the one that was entered the data, What am I doing wrong? I remain attentive to the answers, thanks in advance

    
asked by Shiro 29.05.2017 в 22:26
source

2 answers

1
  

I need to initialize all vector values at 0

int FilaA[9] = {0};

Better to go programming a loop, right?

By the way, in C ++ (and already in C99 and later) it is legal to declare variables within the loops:

for( int i=0; i<9; i++ )
//   ^^^
  

and ask for the column data

No need to use temporary variables to recover keyboard values:

// Esto...
scanf("%d", &ValorIngresado);
FilaA[Columna]=ValorIngresado;

// ... queda asi mas limpio ...
scanf("%d",&FilaA[Columna]);

Although, on the other hand, you are programming in C ++ ... so I recommend using the C ++ own syntax:

#include <iostream>

std::cout << "Digite la columna:\n";
std::cin >> Columna;
std::cout << Digite un valor entre 1-9 que desea ingresar en la posicion [" << Columna << "]: \n\t";
std::cin >> FilaA[Columna];

And, to finish off the move, consider contemplating if the data entered by the user is correct (regardless of whether you use version C or the C ++ version):

#include <limits>

do
{
  std::cout << "Digite la columna:\n";
  std::cin >> Columna 
  if( std::cin.fail() ) // No se ha introducido un numero
  {
    // Descartar lo que haya en el buffer de entrada
    std::cin.ignore(std::numeric_limits<int>::max());

    // Limpiar los flags de error
    std::cin.clear();
  }
}
while ( Columna < 0 || Columna > 8 );

std::cout << Digite un valor entre 1-9 que desea ingresar en la posicion [" << Columna << "]: \n\t";
std::cin >> FilaA[Columna];
  

but in the final impression gives something that I do not see sense, change 4 values instead of only the one that was entered the data

printf("%d  %d  %d  %d  %d  %d  %d  %d  "), FilaA[0], FilaA[1], FilaA[2], FilaA[3], FilaA[4], FilaA[5], FilaA[6], FilaA[7], FilaA[8];
//                                       ^ AQUI

This parenthesis ends the function printf . As you do not pass any parameters and you are having good luck, the function is limited to printing garbage. The quick solution is to move that parenthesis to the end of the line:

printf("%d  %d  %d  %d  %d  %d  %d  %d  ", FilaA[0], FilaA[1], FilaA[2], FilaA[3], FilaA[4], FilaA[5], FilaA[6], FilaA[7], FilaA[8]);

Although you could also get a more manageable code and less prone to errors if you use loops:

for( int i=0; i<9; i++ )
  printf("%d ",FilaA[i]);

And it would be even better if you use the functionality of C ++:

for( int i=0; i<9; i++ )
  std::cout << FilaA[i] << ' ';
    
answered by 30.05.2017 в 09:12
0

your problem is that you closed the function printf before putting the data

printf("%d  %d  %d  %d  %d  %d  %d  %d  ", FilaA[0], FilaA[1], FilaA[2], FilaA[3], FilaA[4], FilaA[5], FilaA[6], FilaA[7], FilaA[8]);
    
answered by 29.05.2017 в 23:02