0
在這裏呆了幾個小時,無法弄清楚如何以我想要的方式得到這個。將光標定位在屏幕上
我需要它在中心這樣的控制檯輸出文本...
*******************************************
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 :: setw'不工作了你(或重複空格)?設置光標位置似乎有點不必要,但我想你不能說控制檯在標準C++中有多寬。 – chris
請注意,您如何將文本定位在您的預期輸出中。您只需事先輸入足夠的空格。 –