【问题标题】:gotoxy prints wrong in c++gotoxy 在 C++ 中打印错误
【发布时间】:2021-03-19 20:42:20
【问题描述】:

我正在为一家有 4 个选项的商店制作菜单;添加产品,删除产品,查看已创建产品表并退出程序。 我完成了添加产品和退出的选项,它们都工作正常。

现在我必须选择查看产品表,据说我已经完成了,但我发现了一个问题......

我第一次进去看产品表,它正常打印给我,像这样。 enter image description here

但是当我转到菜单并返回产品表时,它会像这样打印它。 enter image description here

就像我说的,它只会在我第一次看到它时正确打印,然后再打印错误。 你知道我该如何解决吗?

这是我打印产品表的代码部分。

void table()
{
    int c = k, m = 7;
    system("cls");
    cout << endl;
    cout << "Table of created products";
    cout << endl;
    cout << endl;
    cout << " number of order ";
    cout << "|     Name of product      |";
    cout << "   Product code    |";
    cout << "  Amount  |";
    cout << "   Unit price   |";
    cout << " Subtotal |";
    cout << "    IVA    |";
    cout << " total price |";
    cout << endl;
    cout << "-------------------------------------------------------------------------------------------------------------------------------------";
    cout << endl;
    for (int L = 2; L <= c; L++)
    {
        cout << "                 ";
        cout << "|                          |";
        cout << "                   |";
        cout << "          |";
        cout << "                |";
        cout << "          |";
        cout << "           |";
        cout << "             |";
        cout << endl;
        cout << "-------------------------------------------------------------------------------------------------------------------------------------";
        cout << endl;

    }

    for (int h = 1; h < c; h++)
    {

        gotoxy(8, m);
        cout << product[h].numor;
        gotoxy(20, m);
        cout << product[h].descr;
        gotoxy(52, m);
        cout << product[h].cod;
        gotoxy(70, m);
        cout << product[h].cant;
        gotoxy(83, m);
        cout << "$" << product[h].preuni;
        gotoxy(96, m);
        cout << "$" << product[h].subt;
        gotoxy(107, m);
        cout << "$" << product[h].IVA;
        gotoxy(119, m);
        cout << "$" << product[h].total;
        m = m + 4;
    }
    cout << "\n\n\n";
    system("pause");
    system("cls");
}

这是我的完整代码(也许我有一个或另一个库太多,因为我一直在试验)。

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<string>
#include<ctype.h>
#include <stdio.h>  
#include <windows.h>  
#include<iomanip>

using namespace std;

struct products
{
    int numor{},
        cant{};
    float preuni{},
        subt{},
        IVA{},
        total{};
    string cod{};
    string descr{};
}product[51];

void add();
void intro_code();
int val_code(char codigo[]);
void table();
void gotoxy(int x, int y);

int i = 1;                //Product No. (counter)
int k = 1;  //Consecutive number (counter)


int main()
{
    char opc;

    cout << "                Welcome ";
    cout << endl;


    cout << "\n1.- Add product.";
    cout << "\n2.- Delete product.";
    cout << "\n3.- Table of created products.";
    cout << "\n4.- Exit";
    cout << endl;
    cout << "\nWhat do you want to do today?: "; cin >> opc;




    switch (opc)
    {
    case '1':
        system("cls");
        cout << "\nAdd product";
        add();
        system("pause");
        system("cls");
        return main();
    case '2':
        cout << "\nDelete product";
        system("cls");
        return main();
    case '3':
        table();
        return main();
    case '4':
        exit(EXIT_SUCCESS);
    default:
        system("cls");
        cout << "Warning: the data entered is not correct, try again.\n\n";
        return main();
    }
    return 0;
}

void add()
{
    int can;

    cout << "\nHow many products do you want to register?: "; cin >> can;

    if (can <= 0 || can > 51)
    {
        system("cls");
        cout << "Warning: The amount of products entered is not valid, try again";
        return add();


    }

    for (int p = 1; p <= can;)
    {
        if (i < 51)
        {
            if (k <= 51)        // In this part, consecutive numbers are generated automatically
            {
                cout << "\nNumber of order: ";
                cout << k;
                cout << endl;
                product[i].numor = k;
                k++;
            }
            cin.ignore();
            cout << "Name of product: ";
            getline(cin, product[i].descr);
            intro_code();
            cout << "Quantity to sell: "; cin >> product[i].cant;
            cout << "unit price: $"; cin >> product[i].preuni;
            product[i].subt = product[i].preuni * product[i].cant;
            cout << "Subtotal: $" << product[i].subt;
            product[i].IVA = product[i].subt * 0.16;
            cout << "\nIVA: $" << product[i].IVA;
            product[i].total = product[i].subt + product[i].IVA;
            cout << "\nTotal price: $" << product[i].total;
            cout << "\n-------------------------------------------------------------------------------";
            cout << endl;
            i++;
        }
        p++;
    }
}
/*In this function the product code is entered, if the user enters a code
less than or greater than 5 characters the program displays an error message and allows the
user try again.*/
void intro_code()
{
    char hello[10];
    int xyz;
    do {
        cout << "Code of product (5 digits): ";
        cin.getline(hello, 10, '\n');
        if (strlen(hello) != 5)
        {
            cout << "The data entered is incorrect, try again ...";
            cout << endl;
            return intro_code();
        }
        else
        {
            xyz = val_code(hello);
        }
    } while (xyz == 0);
    product[i].cod = hello;
}
/*As the requirement for "product code" is that it does not contain letters, special characters or blank spaces
create the function val_code to go through each character entered in the code, check character by character with the isdigit function
to see if the character is numeric or not, if it is not numeric it prints a warning message and allows the user to try again
without leaving the console.*/

int val_code(char hello[]) {
    int abc;

    for (abc = 0; abc < strlen(hello); abc++) {

        if (!(isdigit(hello[abc]))) {

            cout << "The data entered is incorrect, try again ...";
            cout << endl;
            return 0;
        }

    }

    return 1;
}

void table()
{
    int c = k, m = 7;
    system("cls");
    cout << endl;
    cout << "Table of created products";
    cout << endl;
    cout << endl;
    cout << " number of order ";
    cout << "|     Name of product      |";
    cout << "   Product code    |";
    cout << "  Amount  |";
    cout << "   Unit price   |";
    cout << " Subtotal |";
    cout << "    IVA    |";
    cout << " total price |";
    cout << endl;
    cout << "-------------------------------------------------------------------------------------------------------------------------------------";
    cout << endl;
    for (int L = 2; L <= c; L++)
    {
        cout << "                 ";
        cout << "|                          |";
        cout << "                   |";
        cout << "          |";
        cout << "                |";
        cout << "          |";
        cout << "           |";
        cout << "             |";
        cout << endl;
        cout << "-------------------------------------------------------------------------------------------------------------------------------------";
        cout << endl;

    }

    for (int h = 1; h < c; h++)
    {

        gotoxy(8, m);
        cout << product[h].numor;
        gotoxy(20, m);
        cout << product[h].descr;
        gotoxy(52, m);
        cout << product[h].cod;
        gotoxy(70, m);
        cout << product[h].cant;
        gotoxy(83, m);
        cout << "$" << product[h].preuni;
        gotoxy(96, m);
        cout << "$" << product[h].subt;
        gotoxy(107, m);
        cout << "$" << product[h].IVA;
        gotoxy(119, m);
        cout << "$" << product[h].total;
        m = m + 4;
    }
    cout << "\n\n\n";
    system("pause");
    system("cls");
}
void gotoxy(int x, int y)
{
    HANDLE hcon;
    hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD dwPos;
    dwPos.X = x;
    dwPos.Y = y;
    SetConsoleCursorPosition(hcon, dwPos);
}

提前非常感谢你:)

【问题讨论】:

  • 你为什么在这里跳4行:m = m + 4;?
  • 请使用tour 并阅读How to Ask。请不要张贴文字图片。改为发布properly formatted text
  • 请以文本形式发布调试会话的结果。哪个陈述导致了这个问题?
  • 您是否尝试过在调试器中单步执行您的程序?
  • @VladFeinstein 是跳线,在表格的下一行打印下一个产品。如果我不放,所有的产品都会打印在同一行

标签: c++ visual-studio switch-statement


【解决方案1】:

出现这个问题的主要原因是控制台大小改变后,你绘制的符号位置也发生了变化。同时cout&lt;&lt;endl;确定的换行也有问题。比如窗口变大后,控制台的行长变大了,但是cout&lt;&lt;endl;还是原来的行长。

但是,没有好的解决方案。我建议你直接设置控制台大小和m=5, m=m+2

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<string>
#include<ctype.h>
#include <stdio.h>  
#include <windows.h>  
#include<iomanip>

using namespace std;

struct products
{
    int numor{},
        cant{};
    float preuni{},
        subt{},
        IVA{},
        total{};
    string cod{};
    string descr{};
}product[51];

void add();
void intro_code();
int val_code(char codigo[]);
void table();
void gotoxy(int x, int y);

int i = 1;                //Product No. (counter)
int k = 1;  //Consecutive number (counter)


int main()
{
    HWND console = GetConsoleWindow();
    RECT r;
    GetWindowRect(console, &r);
    MoveWindow(console, r.left, r.top, 1400, 400, TRUE);
    char opc;

    cout << "                Welcome ";
    cout << endl;


    cout << "\n1.- Add product.";
    cout << "\n2.- Delete product.";
    cout << "\n3.- Table of created products.";
    cout << "\n4.- Exit";
    cout << endl;
    cout << "\nWhat do you want to do today?: "; cin >> opc;




    switch (opc)
    {
    case '1':
        system("cls");
        cout << "\nAdd product";
        add();
        system("pause");
        system("cls");
        return main();
    case '2':
        cout << "\nDelete product";
        system("cls");
        return main();
    case '3':
        table();
        return main();
    case '4':
        exit(EXIT_SUCCESS);
    default:
        system("cls");
        cout << "Warning: the data entered is not correct, try again.\n\n";
        return main();
    }
    return 0;
}

void add()
{
    int can;

    cout << "\nHow many products do you want to register?: "; cin >> can;

    if (can <= 0 || can > 51)
    {
        system("cls");
        cout << "Warning: The amount of products entered is not valid, try again";
        return add();


    }

    for (int p = 1; p <= can;)
    {
        if (i < 51)
        {
            if (k <= 51)        // In this part, consecutive numbers are generated automatically
            {
                cout << "\nNumber of order: ";
                cout << k;
                cout << endl;
                product[i].numor = k;
                k++;
            }
            cin.ignore();
            cout << "Name of product: ";
            getline(cin, product[i].descr);
            intro_code();
            cout << "Quantity to sell: "; cin >> product[i].cant;
            cout << "unit price: $"; cin >> product[i].preuni;
            product[i].subt = product[i].preuni * product[i].cant;
            cout << "Subtotal: $" << product[i].subt;
            product[i].IVA = product[i].subt * 0.16;
            cout << "\nIVA: $" << product[i].IVA;
            product[i].total = product[i].subt + product[i].IVA;
            cout << "\nTotal price: $" << product[i].total;
            cout << "\n-------------------------------------------------------------------------------";
            cout << endl;
            i++;
        }
        p++;
    }
}
/*In this function the product code is entered, if the user enters a code
less than or greater than 5 characters the program displays an error message and allows the
user try again.*/
void intro_code()
{
    char hello[10];
    int xyz;
    do {
        cout << "Code of product (5 digits): ";
        cin.getline(hello, 10, '\n');
        if (strlen(hello) != 5)
        {
            cout << "The data entered is incorrect, try again ...";
            cout << endl;
            return intro_code();
        }
        else
        {
            xyz = val_code(hello);
        }
    } while (xyz == 0);
    product[i].cod = hello;
}
/*As the requirement for "product code" is that it does not contain letters, special characters or blank spaces
create the function val_code to go through each character entered in the code, check character by character with the isdigit function
to see if the character is numeric or not, if it is not numeric it prints a warning message and allows the user to try again
without leaving the console.*/

int val_code(char hello[]) {
    int abc;

    for (abc = 0; abc < strlen(hello); abc++) {

        if (!(isdigit(hello[abc]))) {

            cout << "The data entered is incorrect, try again ...";
            cout << endl;
            return 0;
        }

    }

    return 1;
}

void table()
{
    int c = k, m = 5;
    system("cls");
    cout << endl;
    cout << "Table of created products";
    cout << endl;
    cout << endl;
    cout << " number of order ";
    cout << "|     Name of product      |";
    cout << "   Product code    |";
    cout << "  Amount  |";
    cout << "   Unit price   |";
    cout << " Subtotal |";
    cout << "    IVA    |";
    cout << " total price |";
    cout << endl;
    cout << "-------------------------------------------------------------------------------------------------------------------------------------";
    cout << endl;
    for (int L = 2; L <= c; L++)
    {
        cout << "                 ";
        cout << "|                          |";
        cout << "                   |";
        cout << "          |";
        cout << "                |";
        cout << "          |";
        cout << "           |";
        cout << "             |";
        cout << endl;
        cout << "-------------------------------------------------------------------------------------------------------------------------------------";
        cout << endl;

    }

    for (int h = 1; h < c; h++)
    {

        gotoxy(8, m);
        cout << product[h].numor;
        gotoxy(20, m);
        cout << product[h].descr;
        gotoxy(52, m);
        cout << product[h].cod;
        gotoxy(70, m);
        cout << product[h].cant;
        gotoxy(83, m);
        cout << "$" << product[h].preuni;
        gotoxy(96, m);
        cout << "$" << product[h].subt;
        gotoxy(107, m);
        cout << "$" << product[h].IVA;
        gotoxy(119, m);
        cout << "$" << product[h].total;
        m = m + 2;
    }
    cout << "\n\n\n";
    system("pause");
    system("cls");
}
void gotoxy(int x, int y)
{
    HANDLE hcon;
    hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD dwPos;
    dwPos.X = x;
    dwPos.Y = y;
    SetConsoleCursorPosition(hcon, dwPos);
}

另外,不建议使用cmd制作gui。

【讨论】:

  • @grey 你有更新吗?如果您的案例已经解决,请帮忙标记答案。如果没有,请随时与我们联系。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多