T
T
Tesla4o2018-05-01 21:14:49
C++ / C#
Tesla4o, 2018-05-01 21:14:49

How to exit from input loop to string array?

Help to make an output from a loop of input of lines in an array. Let's say if the array is 200 long, and I only need to enter 5 cells, how can I interrupt it? break doesn't help or am I doing something wrong.
here is the code

#include <iostream>
#include <windows.h>
#include <string>
#include <vector>


using namespace std;

#define SIZE_VEC 5

class Summ
{
public:
  void setSumm();
  void getSumm();
private:
  string *name = new string[SIZE_VEC];  //---надо сделать изменяемый размер массива
  //vector<string> name;
};

void Summ::setSumm()
{
  for (int i = 0; i < SIZE_VEC; ++i)
  {
    char en{ 'e' };         //
    if (i == en)            //------>Эта срань не работает
      break;              //
    cout << i + 1 << " - ";
    getline(cin, name[i]);
    
  }
}

void Summ::getSumm()
{
  for (int j = 0; j < SIZE_VEC; ++j)
  {
    cout << "Введите цену на " << name[j] <<": "; 
    float cost; 
    cin >> cost;
    cout << name[j] << " - " << cost << endl;
  }
}

int main()
{
  SetConsoleCP(1251);
  SetConsoleOutputCP(1251);

  Summ sum;
  sum.setSumm();
  sum.getSumm();

  system("pause");
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Codebaker, 2018-05-01
@Tesla4o

The operator getline(cin, name[i]);
you are reading a string from the user into the i-th element of the name array.
And to exit, use if (i == en), obviously, you need to compare like this: if (0 == name[i].compare(en))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question