Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: FrnkdOU en 10 Septiembre 2020, 20:28 pm



Título: Ingresar números en un vector hasta que se ingrese "0"
Publicado por: FrnkdOU en 10 Septiembre 2020, 20:28 pm
¡Buenas! Tengo una duda. Necesito que en mi código el usuario pueda ingresar la cantidad de números que desee en un array, hasta que este ingrese "0". Mi pregunta es de qué manera podría hacerlo.

Este es mi código:

Código
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.    setlocale(LC_CTYPE, "Spanish");
  8.    int numeros[10], aux;
  9.  
  10.    cout << ">> Ingrese la cantidad de numeros que desee" << endl;
  11.    cout << ">> Ingrese 0 para terminar" << endl <<endl;
  12.  
  13.    for (int i = 0; i<10; i++)
  14.    {
  15.        cout << "- ";
  16.        cin >> numeros[i];
  17.  
  18.        if (numeros[i] !=0)
  19.        {
  20.            cout << "- ";
  21.            cin >> numeros[i];
  22.        }
  23.        else
  24.        {
  25.            break;
  26.        }
  27.    }
  28.    cout << "" << endl;
  29.    cout << "Los numeros ingresados fueron: ";
  30.    for (int i = 0; i<10; i++)
  31.    {
  32.        cout << numeros[i];
  33.        if(i !=9)
  34.        {
  35.            cout << ", ";
  36.        }
  37.    }
  38.  
  39.    for (int i = 0; i<9; i++)
  40.    {
  41.        for (int j=0; j<9; j++)
  42.        {
  43.            if (numeros[j] > numeros[j+1])
  44.            {
  45.                aux = numeros[j];
  46.                numeros[j] = numeros[j+1];
  47.                numeros[j+1] = aux;
  48.            }
  49.        }
  50.    }
  51.    cout << "" << endl << endl;
  52.    cout << "Los numeros ingresados ordenados de menor a mayor: ";
  53.    for (int i = 0; i<10; i++)
  54.    {
  55.        cout << numeros[i];
  56.        if(i !=9)
  57.        {
  58.            cout << ", ";
  59.        }
  60.    }
  61.    return 0;
  62. }

MOD: Etiqueta GeSHi agregada.


Título: Re: Ingresar números en un vector hasta que se ingrese "0"
Publicado por: @XSStringManolo en 10 Septiembre 2020, 22:17 pm
En pseudocódigo:
Código
  1. while(mantenerseEnBucle == true) { //bool
  2.  
  3.  std::cout << "pon numero" << std::endl;
  4.  std::cin >> numero; //int
  5.  
  6.  if (numero == 0) { //si el cin es 0
  7.    mantenerseEnBucle = false;
  8.  } else {
  9.    vector.push(numero); //std::vector<int> vector
  10.  }
  11.  
  12. }