How to make an exit from the loop input in a string array?

0 like 0 dislike
3 views
Help make a way out of the cycle of the input lines to the array. Let's say if the length of the array 200, and I need to enter only 5 cells, how to break it? break does not work or I not correctly do something.
here is the code

#include #include #include #include using namespace std; #define SIZE_VEC 5 class Summ { public: void setSumm(); void getSumm(); private: string *name = new string[SIZE_VEC]; //---you need to make a resizable array //vector name; }; void Summ::setSumm() { for (int i = 0; i < SIZE_VEC; ++i) { en char{ 'e' }; // if (i == en) //------>This shit is not working break; // cout << i + 1 << "- "; getline(cin, name[i]); } } void Summ::getSumm() { for (int j = 0; j < SIZE_VEC; ++j) { cout << "Enter the price" << 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; }
by | 3 views

1 Answer

0 like 0 dislike
The statement getline(cin, name[i]);
you read a string from the user in the i-th element of the array name.
And to exit, use if (i == en), it is obvious that it is necessary to compare like this: if (0 == name[i].compare(en))
by

Related questions

110,608 questions
257,186 answers
0 comments
33,911 users