简体   繁体   中英

using gotoxy positioning in a “for loop”

int strtnumber,endnumber,remainder,i;
    gotoxy(2, 0);
    printf("Enter Start value: "); //enters start number
    cin >> strtnumber;
    gotoxy(2, 1);
    printf("Enter End Value: ");    //enters end number
    cin >> endnumber;
    cout << "==================================================="<< endl;
    for (; strtnumber <= endnumber; strtnumber++) {
        remainder = strtnumber % 2;

            if (remainder == 1) {

                    cout << strtnumber << endl;
            }

        if (remainder == 0) {

            cout << strtnumber << endl;
        }

    }

I would like to separate the odd and even numbers using gotoxy This is the output I would like to achieve:

         ODD    |   EVEN
          1     |     2
          3     |     4
          5     |     6

I would like to separate the odd and even numbers using gotoxy This is the output I would like to achieve:

Almost every system with which I have worked has ansi terminal's available. Even the embedded systems would be connected (via RS232) to VT100's or equivalent.

PC's come equipped with ansi term emulators, or the emulators can be trivially downloaded and installed.


Note: The comments provided are clear, Ansi i/o is not part of C++.

But you can easily find example code to invoke the ansi terminal functions you might want.

Here is a useful part of my own class which has worked on every version of Ubuntu Linux, and multiple Linux terminal emulators, and vxWorks, etc. I currently use an emulator installed with Lubuntu. Have not found the name, but the help references Qt_io:

#include <iostream>
using std::cout, std::endl;

#include <string>
using std::string, std::to_string;


class Ansi_t   // provide access to features of ansi-compatible terminal
{
public:

   string clrscr (void)
   { return '\033' + string("[H") + '\033' + string("[2J"); }

   //              --v------v-  C++ 2d array indexing is 0 based
   string gotoRC(int r, int c) {
      return  ('\033' + string("[") +
               to_string(r+1) + ';' + to_string(c+1) + 'H');
   } //                  0 ^                    0 ^
   //                      1,1 is top left of ansi screen

   // tbr - add other ansi functions here

}; // class Ansi_t

// usage: declare an instance,  Ansi_t ansi;
// invoke method:    std::cout << ansi.clrscr() << std::endl;
// invoke method:    std::cout << ansi.gotoRC() << std::endl;
// to specify a named escape sequence  ^^^^^^^^
// Ubuntu gnome-terminal ansi term cursor locations are
// 1-based with  origin 1,1 at top left corner


class Hello_t  // demo executable
{
   Ansi_t ansi;  // declare an instance 

public:
   Hello_t()  { cout << ansi.clrscr() << ansi.gotoRC(9, 18) << "Hello "; }
   ~Hello_t() { cout << " world!\n\n\n" << endl; }
   int operator()() { cout << "C++"; return 0; }   // functor entrance
};

int main(int , char* * ) { return (Hello_t()()); }

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