K
K
Konstantin2019-02-24 20:00:13
C++ / C#
Konstantin, 2019-02-24 20:00:13

What's wrong with the code? How to do it right?

The task is to enter data in Russian. The question is how to do it correctly, and why the error occurs (see images).
problem area of ​​the code:

#include <iostream>

using namespace std;

const unsigned int arr_size = 2; // array size
const unsigned int str_len = 80;
int i;

struct marsh {
  char start[str_len];
  char finish[str_len];
  unsigned int index;
} list[arr_size];


int main() {
  setlocale(LC_ALL, "Rus");

  // input
  for (i = 0; i < arr_size; i++)
  {
    cout << "====================================" << endl
      << "\tдобавление записи " << i + 1 << endl
      << "номер маршрута = ";
    cin >> list[i].index;
    cout << "начальный пункт = ";
    cin.getline(list[i].start, str_len);
    cout << "конечный пункт = ";
    cin.getline(list[i].finish, str_len);
  }
  cout << "====================================" << endl;
}
run result image

снимок1
5c72c9ce82d26056610407.jpeg
снимок2
5c72cb0b551a4934321289.jpeg
complete program code
#include <iostream>

using namespace std;

const unsigned int arr_size = 2; // array size
const unsigned int str_len = 80;
int i;

struct marsh {
  char start[str_len];
  char finish[str_len];
  unsigned int index;
} list[arr_size];


int main() {
  setlocale(LC_ALL, "Rus");

  // input
  for (i = 0; i < arr_size; i++)
  {
    cout << "====================================" << endl
      << "\tдобавление записи " << i + 1 << endl
      << "номер маршрута = ";
    cin >> list[i].index;
    cout << "начальный пункт = ";
    cin.getline(list[i].start, str_len);
    cout << "конечный пункт = ";
    cin.getline(list[i].finish, str_len);
  }
  cout << "====================================" << endl;
  //sorting
  marsh temp;
  for (i = 0; i < arr_size - 1; i++)
    for (int j = 0; j < arr_size - i - 1; j++)
      if (list[j].index > list[j + 1].index) {
        temp = list[j];
        list[j] = list[j + 1];
        list[j + 1] = temp;
      }
  // output
  unsigned int number;
  cout << endl << "введите номер маршрута, который хотите просмотреть" << endl
    << "номер = ";
  cin >> number;
  bool not_in_list = true;
  for (i = 0; i < arr_size; i++)
  {
    if (list[i].index == number)
    {
      cout << "\nмаршрут номер " << list[i].index << " из начального пункта \"" << list[i].start
        << "\" в конечный пункт \"" << list[i].finish << '\"' << endl << endl << endl;
      not_in_list = false;
      break;
    }
  }
  if (not_in_list) cout << "извините, но данного маршрута в записях нет" << endl << endl << endl;
  system("Pause");
  return 0;
}
P. S. в общем интересно узнать мнение о коде, так что если не лень отпишите пожалуйста, насколько всё плохо.

how to enter Russian text on windows 10 without using windows.h?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2019-02-24
@kostyakos52000

The default console encoding in Windows is cp866 (not 1251 as many people think). But you can switch.
For simplicity, save your sources in 866 encoding.
setlocale only works for output. Use cout.imbue for input.
Switching the console encoding from the utility is a bad form. For a student lab, this is still tolerable, but a normal Russian console utility should be able to correctly display text no matter what encoding is set in the console 866 or 1251. For example, almost all native Windows console utilities cope with this successfully.
According to the mind, do something like the following scheme:
1. Sources in UTF8
2. All string constants with the L"string"
prefix 3. Store strings in wchar_t
4. Determine the console encoding for input and output and recode your Unicode strings into the desired encoding and only then output or recode to UTF8 after input. There is everything for transcoding in WinAPI, but you can't do without windows.h.

K
Konstantin, 2019-02-24
@kostyakos52000

I am attaching the corrected code to the answer above (Not perfect, but it answers the question posed. Perhaps someone will read it and be useful to someone). Thanks for the answer.

the code
#include <iostream>

using namespace std;

const unsigned int arr_size = 10; // array size
const unsigned int str_len = 80;
int i;

struct marsh {
  wchar_t start[str_len];
  wchar_t finish[str_len];
  unsigned int index;
} notes[arr_size];

int main() {
  wcin.imbue(locale(".866"));
  wcout.imbue(locale(".866"));

  // input
  for (i = 0; i < arr_size; i++)
  {
    wcout << L"====================================" << endl
      << L"\tдобавление записи " << i + 1 << endl
      << L"номер маршрута = ";
    cin >> notes[i].index;
    wcout << L"начальный пункт = ";
    wcin.ignore();
    wcin.getline(notes[i].start, str_len);
    wcout << L"конечный пункт = ";
    wcin.getline(notes[i].finish, str_len);
  }
  wcout << L"====================================" << endl;
  //sorting
  marsh temp;
  for (i = 0; i < arr_size - 1; i++)
    for (unsigned int j = 0; j < arr_size - i - 1; j++)
      if (notes[j].index > notes[j + 1].index) {
        temp = notes[j];
        notes[j] = notes[j + 1];
        notes[j + 1] = temp;
      }
  // output
  unsigned int number;
  wcout << endl << L"введите номер маршрута, который хотите просмотреть" << endl
    << L"номер = ";
  cin >> number;
  bool not_in_notes = true;
  for (i = 0; i < arr_size; i++)
  {
    if (notes[i].index == number)
    {
      wcout << L"\nмаршрут номер " << notes[i].index << L" из начального пункта \"" << notes[i].start
        << L"\" в конечный пункт \"" << notes[i].finish << '\"' << endl << endl << endl;
      not_in_notes = false;
      break;
    }
  }
  if (not_in_notes) wcout << L"извините, но данного маршрута в записях нет" << endl << endl << endl;
  system("Pause");
  return 0;
}

screenshots of results
5c73007a27e9e777849309.jpeg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question