How the task sounds:

Write a program "directory". Create two one-dimensional arrays. One array stores ICQ numbers, the second - phone numbers. Implement user menu:

1. Sort by ICQ numbers

2. Sort by phone numbers

3. List users

4. Exit

Question:

Everything that I wanted to realize was done, but I don’t like goto and I would like to understand how to do without it.

The code itself:

#include <iostream> #include <ctime> using namespace std; int main() { setlocale(LC_ALL, "Russian"); srand(time(0)); const int ICQ = 10; const int N = 10; int arr1[ICQ] = {}; int arr2[N] = {}; int num = 0; tryAgain2: for (int i = 0; i < ICQ; i++) { arr1[i] = rand() % 101; } for (int i = 0; i < N; i++) { arr2[i] = rand() % 90000000 + 9999999; } tryAgain: cout << "№\t Номер телефона \t Номер ICQ" << endl; cout << "1.\t " << arr2[0] << "\t\t " << arr1[0] << endl; cout << "2.\t " << arr2[1] << "\t\t " << arr1[1] << endl; cout << "3.\t " << arr2[2] << "\t\t " << arr1[2] << endl; cout << "4.\t " << arr2[3] << "\t\t " << arr1[3] << endl; cout << "5.\t " << arr2[4] << "\t\t " << arr1[4] << endl; cout << "6.\t " << arr2[5] << "\t\t " << arr1[5] << endl; cout << "7.\t " << arr2[6] << "\t\t " << arr1[6] << endl; cout << "8.\t " << arr2[7] << "\t\t " << arr1[7] << endl; cout << "9.\t " << arr2[8] << "\t\t " << arr1[8] << endl; cout << "10.\t " << arr2[9] << "\t\t " << arr1[9] << endl; cout << "\n1.\t Отсортировать по номерам ICQ" << endl; cout << "2.\t Отсортировать по номерам телефона" << endl; cout << "3.\t Вывести список пользователей" << endl; cout << "4.\t Выход" << endl << endl; cin >> num; switch (num) { case 1: system("cls"); for (int i = 1; i < ICQ; ++i) { int k = i; while (k > 0 && arr1[k - 1] > arr1[k]) { int tmp = arr1[k - 1]; arr1[k - 1] = arr1[k]; arr1[k] = tmp; tmp = arr2[k - 1]; arr2[k - 1] = arr2[k]; arr2[k] = tmp; k -= 1; } } goto tryAgain; case 2: system("cls"); for (int i = 1; i < N; ++i) { int k = i; while (k > 0 && arr2[k - 1] > arr2[k]) { int tmp = arr2[k - 1]; arr2[k - 1] = arr2[k]; arr2[k] = tmp; tmp = arr1[k - 1]; arr1[k - 1] = arr1[k]; arr1[k] = tmp; k -= 1; } } goto tryAgain; case 3: system("cls"); goto tryAgain2; break; case 4: return 0; } } 
  • If your task is to arr1[ICQ] arrays (for ICQ numbers) and arr2[N] (for phone numbers) to be of the same size (otherwise the table printing will be nonsense), then specify the size of these arrays by one constant (for example , N_USERS ), and not 2 as you have now. - avp 1:06 pm

2 answers 2

goto alone is easy to clean. For a more elegant program, you need to somehow rework the algorithm, and just an equivalent conversion to for will look like this:

 #include <iostream> #include <ctime> using namespace std; constexpr int ICQ = 10; constexpr int N = 10; void Reinit(int arr1[], int arr2[]) { for (int i = 0; i < ICQ; i++) { arr1[i] = rand() % 101; } for (int i = 0; i < N; i++) { arr2[i] = rand() % 90000000 + 9999999; } } int main() { setlocale(LC_ALL, "Russian"); srand((unsigned)time(0)); int arr1[ICQ]; int arr2[N]; int num = 0; Reinit(arr1, arr2); for (;;) { cout << "№\t Номер телефона \t Номер ICQ" << endl; cout << "1.\t " << arr2[0] << "\t\t " << arr1[0] << endl; cout << "2.\t " << arr2[1] << "\t\t " << arr1[1] << endl; cout << "3.\t " << arr2[2] << "\t\t " << arr1[2] << endl; cout << "4.\t " << arr2[3] << "\t\t " << arr1[3] << endl; cout << "5.\t " << arr2[4] << "\t\t " << arr1[4] << endl; cout << "6.\t " << arr2[5] << "\t\t " << arr1[5] << endl; cout << "7.\t " << arr2[6] << "\t\t " << arr1[6] << endl; cout << "8.\t " << arr2[7] << "\t\t " << arr1[7] << endl; cout << "9.\t " << arr2[8] << "\t\t " << arr1[8] << endl; cout << "10.\t " << arr2[9] << "\t\t " << arr1[9] << endl; cout << "\n1.\t Отсортировать по номерам ICQ" << endl; cout << "2.\t Отсортировать по номерам телефона" << endl; cout << "3.\t Вывести список пользователей" << endl; cout << "4.\t Выход" << endl << endl; cin >> num; switch (num) { case 1: system("cls"); for (int i = 1; i < ICQ; ++i) { int k = i; while (k > 0 && arr1[k - 1] > arr1[k]) { int tmp = arr1[k - 1]; arr1[k - 1] = arr1[k]; arr1[k] = tmp; tmp = arr2[k - 1]; arr2[k - 1] = arr2[k]; arr2[k] = tmp; k -= 1; } } break; case 2: system("cls"); for (int i = 1; i < N; ++i) { int k = i; while (k > 0 && arr2[k - 1] > arr2[k]) { int tmp = arr2[k - 1]; arr2[k - 1] = arr2[k]; arr2[k] = tmp; tmp = arr1[k - 1]; arr1[k - 1] = arr1[k]; arr1[k] = tmp; k -= 1; } } break; case 3: system("cls"); Reinit(arr1, arr2); break; case 4: return 0; } } } 

Minor notes:

1) For srand you need to do time_t castes.

2) It is not necessary to initialize the arrays during the declaration, they will still be initialized later.

3) After goto no need to put a break .

  • Thank you very much. Thanks to you, I learned about the existence of "constexpr", I read about it in more detail today. I will take into account the remarks. Be so kind as to explain this: void Reinit (int arr1 [], int arr2 []) Reinit (arr1, arr2); Also did not quite understand about the "time_t" how and where to use? - Blacit
  • @Blacit, - 1) Reinit - I just put a piece of code into a function to remove the second label. Otherwise, two intersecting blocks were obtained, and this did not fit into a simple cycle. 2) time_t - the time function returns a result of the time_t type, and srand requires an unsigned int argument. Without explicit caste, the compiler issues a warning. - freim pm
  • You can not tell how to add more usernames moved with phones and icq. And how can you save the original information on the arrays, so that by pressing the 3 button - we would have the original table, which was created from the very beginning. - Blacit 5:53 pm
  • Here I have saved the code (the very first (actual)): link - Blacit
  • @Blacit, well, I can’t rewrite your program, I just don’t have time, but I can tell you the direction. You need to use C ++ (surprise :). First, define a struct that will describe the user — name, phone, everything. Then create one single vector that will hold these structures. And for sorting, use the std::sort algorithm. The program will be better, simpler, and four times less. - freim 6:03 pm

for(;;) - this is an infinite loop. Banal answer, but still.

  • Thank you very much :) Above provided the entire code. - Blacit