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; } }
arr1[ICQ]arrays (for ICQ numbers) andarr2[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