简体   繁体   中英

gotoxy prints wrong in c++

I am making a menu for a store with 4 options; add product, delete product, see table of created products and exit the program. I finished the option to add products and exit, they both work fine.

Now I have to make the option to see the product table, supposedly I had already finished it but I noticed a problem...

The first time I go in to see the product table, it prints it to me normally, like this. enter image description here

But when I go to the menu and go back to the product table, it prints it like this. enter image description here

Like I said, it only prints it right the first time I see it, then it prints it wrong. Do you know how I can solve it?

This is the part of the code where I print the product table.

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");
}

This is my complete code (maybe I have one or another library too many because I have been experimenting).

#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);
}

Thank you very much in advance:)

The main reason for this problem is that after the size of the console changes, the position of the symbols you draw has also changed. At the same time, the line break determined by cout<<endl; also has a problem. For example, after the window becomes larger, the line length of the console becomes larger, but cout<<endl; is still the original line length.

However, there is no good solution. I suggest you directly set the console size and 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);
}

In addition, it is not recommended that you use cmd to make gui.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM