Answer the question
In order to leave comments, you need to log in
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;
}
#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. в общем интересно узнать мнение о коде, так что если не лень отпишите пожалуйста, насколько всё плохо.Answer the question
In order to leave comments, you need to log in
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.
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.
#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;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question