printf(" -----------------------------------\n");
printf("| *** E - Liquid Calculator *** |\n");
printf(" -----------------------------------\n");
printf("| Zutaten |\n");
printf(" -----------------------------------\n");
printf("| Aroma: %% Nikotin: mg/ml |\n");
printf("| Nikotinshot: mg/ml |\n");
printf("| In total: ml |\n");
printf(" -----------------------------------\n");
printf("| Ergebnis |\n");
printf(" -----------------------------------\n");
printf("| Aroma: ml Nikotin: ml |\n");
printf("| Base: ml |\n");
printf(" -----------------------------------\n");
ive创建了一个小项目,我希望用户在表中输入4个值,然后计算成分。但是,如果用户在实际输入值之前按“ Enter”,则光标将跳到新行,然后破坏整个程序的布局。
我尝试了以下代码来防止任何意外的“换行/回车”
do
{
POSITION (6,10);
scanf("%c", &spacebar);
if (spacebar == '\n')
{
POSITION (18,0);
printf("Eingabe erfordelich!\n");
}
} while (spacebar == '\n');
clearBuffer();
问题是,一旦我实际输入了一个值,程序便会跳过输入的值,而在将其存储到变量中之前,我需要再次实际输入它。
这是“防止换行符”功能和第一个读取用户输入的“香气”功能的外观:
do //prevent the user from pressing enter before actually typing a number
{
POSITION (6,10);
scanf("%c", &spacebar);
if (spacebar == '\n')
{
POSITION (18,0);
printf("Eingabe erfordelich!\n");
}
} while (spacebar == '\n');
clearBuffer();
POSITION (18,0);
CLEAR_LINE;
do
{
POSITION (6,10);
Scan_Erg = scanf("%f", &Aroma);
clearBuffer();
if (Scan_Erg == 0)
{
POSITION (16,0);
FORECOLOR_RED;
printf("Please only enter numbers!");
FORECOLOR_YELLOW;
POSITION (6,14);
CLEAR_LINE;
POSITION (6,10);
printf(" %% Nikotin: mg/ml |");
}
else
{
POSITION (6,10);
CLEAR_LINE;
POSITION (6,10);
printf("%4.1f%% Nikotin: mg/ml |", Aroma);
}
} while (Scan_Erg == 0);
有人有没有想法,当没有输入值时如何阻止输入只是换行符?任何建议或帮助,将不胜感激。我是编程新手:-)
您不能通过scanf()
和printf()
完成大多数简单的输入/输出。
您可以构建自己的系统来读取用户输入。
但是您怀疑,这个问题并不新奇,一些聪明的人提供了一种广泛使用的解决方案:curses
库。
根据您的开发和/或目标系统,您可能希望搜索ncurses
或pdcurses
及其文档。也将有一些教程。