一堆空格字符也会输出,并设置背景颜色以在终端中显示一个实心的颜色块。
int i;
for (i=0;i<21;i++) { // for loop to print a load of empty lines
cout << " \n";
} // end of for loop
起初一切都正常,但在第十次更改之后(即按下r、b、g或o,然后按下ENTER),颜色的实心块就会溢出\n字符。
有人能告诉我为什么会这样吗?
我还期望int main()中的else语句会处理任何与预期r、b、g、o或x不匹配的输入。对于单个字符,它可以工作,但如果输入超过一个字符,它就会变得混乱不堪,连续滚动且不停止输入。
为什么会这样呢?
完整代码(使用MinGW g++.exe编译,通过NppExec运行于Win7命令提示符):
#include <windows.h>
#include "iostream"
using namespace std;
/*******FUNCTION PROTOTYPES*********/
void TermClr(int ClrHeight);
void SetColor(int value);
void PrintOut(int output, int col1, int col2);
/*******MAIN PROGRAM****************/
int main() {
int output = 0; // variable for current colour being output, 0-3 for OFF, RED, BLUE, GREEN
char inkey[2]; // console input written to the INKEY variable
PrintOut(0,119,7); // calls printout function, dark grey background for the light, normal grey for text
while(1){ //while loops forever until break
cin.getline(inkey,2); //waits for input, stored to INKEY
if(!strcmp(inkey,"x")){ // string compare whether INKEY matches "x"
cout << "\nProgram Terminated in\n3...";
Sleep(1000);
cout << "\n2...";
Sleep(1000);
cout << "\n1...";
Sleep(1000);
break; //breaks if 'x' is input
} // end if inkey x
else if(!strcmp(inkey,"o")){
PrintOut(0,119,7); // calls PrintOut function, 'output' set to 0, red background for the light, red for text
continue;
} // end of if inkey o
else if(!strcmp(inkey,"r")){
PrintOut(1,204,12);
continue;
} // end of if inkey r
else if(!strcmp(inkey,"b")){
PrintOut(2,153,9);
continue;
} // end of if inkey b
else if(!strcmp(inkey,"g")){
PrintOut(3,170,10);
continue;
} // end of if inkey g
else{
TermClr(30);
printf("Input not recognized\n(x=terminate, o=off, r=red, b=blue, g=green)");
continue;
} // end of else
} //end of while
return 0;
} // end of main
/*******FUNCTIONS*******************/
// function to clear terminal - ClrHeight is the number of new rows, use enough in the function call to clear the terminal
void TermClr(int ClrHeight) {
int i;
for ( i = 0; i < ClrHeight; i++ ) {
putchar('\n');
} // end of for loop
} // end TermClr
// function for changing terminal font colours (run the exe in cmd prompt to see colours, doesn't work in nppexec)
void SetColor(int value){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), value);
} // end SetColor
// function to print the required text to terminal
void PrintOut(int output, int col1, int col2) { // three inputs needed, the 'output' variable 0-3, the light colour text type, and the writing text type
TermClr(5); // calls func to clear teminal, 5 rows
const char *light[4] = {"OFF", "RED", "BLUE", "GREEN"}; // defines the four light colours
SetColor(col1);
int i;
for (i=0;i<21;i++) { // for loop to print a load of empty lines with the background colour 'col1'
cout << " \n";
} // end of for loop
SetColor(7);
cout << "\nColour - ";
SetColor(col2); // calls the function to change the console font colour (shows if run in cmd prompt, but not nppexec console)
cout << light[output];
SetColor(7);
cout << " (Output " << output << ")\n(x=terminate, o=off, r=red, b=blue, g=green)";
} //end PrintOut
编辑后添加:
显示问题的图片
我尝试使用std::endl代替\n,但没有任何区别
进一步编辑:
如果我减少固体颜色的for循环迭代次数,即
int i;
for (i=0;i<3;i++) { // for loop to print a load of empty lines
cout << " \n";
} // end of for loop
通过在每次while循环中打印更少的行,我可以获得更多的更改输出,但在足够多的次数后,输出仍会溢出。编辑3:评论区的马克·兰索姆(Mark Ransom)已经确定了第一个问题的罪魁祸首是cmd窗口。增加缓冲区高度可以允许更多的程序周期而不会出现错误行为。(ALT+SPACE, 属性, 布局选项卡)James Whyte的答案解决了多个键盘输入的问题,尽管我需要做一些工作以使坏输入被忽略而不是被单独解释每个字符。编辑4:如果我使用system("CLS")而不是添加新行来清除屏幕,缓冲区大小就不再重要(猜测每次调用此函数时都会清除)。我知道这是不好的做法,但它起作用了!有没有其他的不是不好的做法?