Загрузка...

Help me solve the problem with writing to a file

Thread in C/C++ created by Envil Mar 30, 2023. (bumped Mar 30, 2023) 212 views

  1. Envil
    Envil Topic starter Mar 30, 2023 https://zelenka.guru/threads/5425786/ -изи 500р 97 Sep 1, 2019
    Code
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <locale.h>
    #include <iomanip>
    #include <windows.h>
    using namespace std;
    const int N = 3;
    const char* indexstr;
    int main() {
    SetConsoleCP(1251); // устанавливаем кодировку для ввода
    SetConsoleOutputCP(1251); // устанавливаем кодировку для вывода

    char im[40];
    int i, count2 = 0, index2;
    FILE* file1 = fopen("file1.txt", "w");
    setlocale(LC_ALL, "rus");
    struct tovar {
    string name;
    float kol;
    string price;
    string proiz;



    }tov[N];
    cout << "Введите описание товара" << endl;
    for (int i = 0; i < N; i++) {
    cout << "Введите название товара: ";
    getline(cin, tov[i].name);

    cout << "Введите количество товара: ";
    cin >> tov[i].kol;
    cin.ignore(); // нужно, чтобы считать строку следующего поля

    cout << "Введите цену товара: ";
    getline(cin, tov[i].price);

    cout << "Введите производителя товара: ";
    getline(cin, tov[i].proiz);


    // записываем данные в файл
    fprintf(file1, "%s %f %s %s\n", tov[i].name.c_str(), tov[i].kol, tov[i].price, tov[i].proiz.c_str());

    }

    fclose(file1);

    cout << "|" << setw(44) << setfill('-') << "Таблицы" << setw(20) << "|" << endl;
    cout << "|" << setw(20) << setfill(' ') << "Название товара" << "|" << setw(10) << setfill(' ') << "Кол-во"
    << "|" << setw(15) << setfill(' ') << "Цена" << "|" << setw(15) << setfill(' ') << "Производитель" << "|" << endl;
    for (int i = 0; i < N; i++) {
    cout << "|" << setw(20) << setfill(' ') << tov[i].name << "|"
    << setw(10) << setfill(' ') << tov[i].kol << "|"
    << setw(15) << setfill(' ') << tov[i].price << "|"
    << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl;
    }
    int A;
    while (true)
    {
    cout << "Что вы хотите сделать??" << endl;
    cout << "1. Отсортировать по цене" << endl;
    cout << "2. Отсортировать по имени" << endl;
    cout << "3. Отсортировать по Колличеству" << endl;
    cout << "4. Выход" << endl;
    cin >> A;
    switch (A)
    {
    case 1: {
    for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < N - i - 1; j++) {
    if (tov[j].price < tov[j + 1].price) {
    tovar temp = tov[j];
    tov[j] = tov[j + 1];
    tov[j + 1] = temp;
    }
    }

    }
    for (int i = 0; i < N; i++) {

    cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|"
    << setw(15) << setfill(' ') << tov[i].kol << "|"
    << setw(12) << setfill(' ') << tov[i].price << "|"
    << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl;
    }
    break;
    }

    case 2: {
    for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < N - i - 1; j++) {
    if (tov[j].name > tov[j + 1].name) {
    tovar temp = tov[j];
    tov[j] = tov[j + 1];
    tov[j + 1] = temp;
    }
    }
    }
    for (int i = 0; i < N; i++) {
    cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|"
    << setw(15) << setfill(' ') << tov[i].kol << "|"
    << setw(12) << setfill(' ') << tov[i].price << "|"
    << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl;
    }

    break;
    }

    case 3: {
    for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < N - i - 1; j++) {
    if (tov[j].kol < tov[j + 1].kol) {
    tovar temp = tov[j];
    tov[j] = tov[j + 1];
    tov[j + 1] = temp;
    }
    }
    }
    for (int i = 0; i < N; i++) {
    cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|"
    << setw(15) << setfill(' ') << tov[i].kol << "|"
    << setw(12) << setfill(' ') << tov[i].price << "|"
    << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl;
    }

    break;
    }

    case 4: {
    cout << "Конец работы";

    }




    }


    }

    }


    }[/CODE]
    Вот сам код
    А вот что оно записывает в блокнот[IMG] имя записывает правильно, колличество тоже, но вот что творится после колличества я не понимаю
    [/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/I][/I][/I][/I][/I]
     
  2. Envil
    Envil Topic starter Mar 30, 2023 https://zelenka.guru/threads/5425786/ -изи 500р 97 Sep 1, 2019
  3. renameduser_4957761
    ты бы вставил в код [IMG]
     
    1. View previous comments (3)
    2. Daemon
      Envil, если вы хотите бесплатной помощи, то извольте хотя бы оформить нормально
    3. Envil Topic starter
      Daemon, Постарался ещё раз исправить
    4. Daemon
      Envil, советую прочитать про функцию swap
    5. View the next comments (2)
  4. Daemon
    Daemon Mar 30, 2023 1680 Jan 8, 2021
    По всей видимости с функциями вы не знакомы (в плане самостоятельного их написания), но ваш код можно в значительной степени сократить и без них. Но лучше познакомьтесь, также лучше не смешивать стили C и C++.
    C
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <locale.h>

    #include <iostream>
    #include <string>
    #include <iomanip>

    #include <windows.h>

    using namespace std;
    const int N = 3;
    const char* indexstr;
    int main() {
    SetConsoleCP(1251); // устанавливаем кодировку для ввода
    SetConsoleOutputCP(1251); // устанавливаем кодировку для вывода

    int count2 = 0;

    struct tovar {
    string name;
    float kol;
    string price;
    string proiz;
    }tov[N];

    FILE* file1 = fopen("file1.txt", "w");
    cout << "Введите описание товара" << endl;
    for (int i = 0; i < N; i++) {
    cout << "Введите название товара: ";
    getline(cin, tov[i].name);

    cout << "Введите количество товара: ";
    cin >> tov[i].kol;
    cin.ignore(); // нужно, чтобы считать строку следующего поля

    cout << "Введите цену товара: ";
    getline(cin, tov[i].price);

    cout << "Введите производителя товара: ";
    getline(cin, tov[i].proiz);

    // записываем данные в файл
    fprintf(file1, "%s %f %s %s\n", tov[i].name.c_str(), tov[i].kol, tov[i].price.c_str(), tov[i].proiz.c_str());
    }
    fclose(file1);

    cout << "|" << setw(44) << setfill('-') << "Таблицы" << setw(20) << "|" << endl
    << "|" << setw(20) << setfill(' ') << "Название товара" << "|" << setw(10) << setfill(' ') << "Кол-во"
    << "|" << setw(15) << setfill(' ') << "Цена" << "|" << setw(15) << setfill(' ') << "Производитель" << "|" << endl;

    for (int i = 0; i < N; i++)
    cout << "|" << setw(20) << setfill(' ') << tov[i].name << "|"
    << setw(10) << setfill(' ') << tov[i].kol << "|"
    << setw(15) << setfill(' ') << tov[i].price << "|"
    << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl;


    while (true) {

    cout << "Что вы хотите сделать??" << endl
    << "1. Отсортировать по цене"
    << "2. Отсортировать по имени" << endl
    << "3. Отсортировать по Колличеству" << endl
    << "4. Выход" << endl;

    int A;
    cin >> A;
    for (int i = 0; i < N - 1; i++)
    for (int j = 0; j < N - i - 1; j++)
    switch (A) {
    case 1:
    if (tov[j].price < tov[j + 1].price)
    swap(tov[j], tov[j + 1]);
    break;

    case 2:
    if (tov[j].name > tov[j + 1].name)
    swap(tov[j], tov[j + 1]);
    break;

    case 3:
    if (tov[j].kol < tov[j + 1].kol)
    swap(tov[j], tov[j + 1]);
    break;

    case 4: cout << "Конец работы"; cin.get(); return 0;
    }

    for (int i = 0; i < N; i++)
    cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|"
    << setw(15) << setfill(' ') << tov[i].kol << "|"
    << setw(12) << setfill(' ') << tov[i].price << "|"
    << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl;
    }
    }
     
Top
Loading...