C++ 在屏幕上定位光标
我已经在这里呆了好几个小时了,不知道怎样才能得到我想要的 我需要它在控制台输出的文本中心像这样C++ 在屏幕上定位光标,c++,C++,我已经在这里呆了好几个小时了,不知道怎样才能得到我想要的 我需要它在控制台输出的文本中心像这样 ******************************************* ABC Industries Report ******************************************* 而是像这样出来的 ********************************** ABC Industries
*******************************************
ABC Industries
Report
*******************************************
而是像这样出来的
**********************************
ABC Industries
Report
**********************************
这是我到目前为止得到的,任何帮助都将非常感谢
#include <string>
#include <iomanip>
#include <iostream>
#include <windows.h>
#include <cctype>
using namespace std;
class Heading
{
private:
string companyName;
string reportName;
public:
Heading();
void placeCursor(HANDLE, int, int);
void printStars(int);
void getData(HANDLE, Heading&);
void displayReport(HANDLE, Heading);
};
int main()
{
Heading display;
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
display.getData(screen, display);
display.displayReport(screen, display);
cin.get();
return 0;
}
Heading::Heading()
{
companyName = "ABC Industries";
reportName = "Report";
}
void Heading::placeCursor(HANDLE screen, int row, int col)
{
COORD position;
position.Y = row;
position.X = col;
SetConsoleCursorPosition(screen, position);
}
void Heading::printStars(int n)
{
for(int star=1; star<=n; star++)
cout << '*';
cout <<endl;
}
void Heading:: getData(HANDLE screen, Heading &input)
{
string str;
placeCursor(screen, 2, 5);
cout <<"Enter company name";
placeCursor(screen, 2, 26);
getline(cin, str);
if(str!="")
input.companyName = str;
placeCursor(screen, 4, 5);
cout<<"enter report name";
placeCursor(screen, 4, 26);
getline(cin, str);
if(str!="")
input.reportName = str;
}
void Heading::displayReport(HANDLE screen, Heading input)
{
int l;
placeCursor(screen, 8, 5);
printStars(69);
string str=input.companyName;
l= str.length();
l=39-l/2;
placeCursor(screen, 9, 1);
cout << str;
str=input.reportName;
l=str.length();
l=39-l/2;
placeCursor(screen, 10, 1);
cout << str;
placeCursor(screen, 11, 5);
printStars(69);
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
班级标题
{
私人:
字符串公司名称;
字符串报告名;
公众:
标题();
void placeCursor(句柄,int,int);
空打印星(int);
无效获取数据(句柄、标题和);
无效显示报告(句柄、标题);
};
int main()
{
航向显示;
手柄屏幕=GetStdHandle(标准输出手柄);
显示。获取数据(屏幕,显示);
显示。显示报告(屏幕、显示器);
cin.get();
返回0;
}
标题::标题()
{
companyName=“ABC工业”;
reportName=“Report”;
}
无效标题::placeCursor(句柄屏幕、整数行、整数列)
{
协调地位;
位置Y=行;
位置X=col;
设置控制台位置(屏幕、位置);
}
空标题::printStars(int n)
{
因为(intstar=1;star你会踢自己.)
注意,在Heading::displayReport中,您计算公司名称的长度,然后将其除以2,再从39中减去。然后将光标定位到9,1(九,一)
您要做的是将光标位置设置为9,l(9,el)
然后对reportName字段也重复该操作
所以,让这两个改变
// placeCursor(screen, 9, 1);
placeCursor(screen, 9, l);
及
<代码> STD::SEtW商店< /代码>不适合你(或重复的空间)?设置光标位置似乎有点不必要,尽管我猜你不能告诉控制台的标准C++有多宽。注意你如何将文本定位在你预期的输出中。你只需预先输入足够的空间。
// placeCursor(screen, 10, 1);
placeCursor(screen, 10, l);