[英]In c or c++ how can i give input to a program without enter or space
i want to write a program in which i will give the input as 109876543 and the program will calculate the sum by adding each digit in the given input (1+0+9+8+7+6+5+4+3). 我想编写一个程序,在该程序中我将输入作为109876543,程序将通过将给定输入中的每个数字相加来计算总和(1 + 0 + 9 + 8 + 7 + 6 + 5 + 4 + 3)。 so how to separate these digit and add them and which data type to choose to store this input??
那么如何分隔这些数字并添加它们,以及选择哪种数据类型来存储此输入呢?
Have a look at ncurses (you might also like the Wikipedia entry ). 看一下ncurses (您可能也喜欢Wikipedia条目 )。
On Windows there's the equivalent pdcurses . 在Windows上有等效的pdcurses 。
Store the input in an array of character (not necessarily a string). 将输入存储在字符数组(不一定是字符串)中。
[for C] [对于C]
Go for unbuffered I/O: 进行无缓冲的I / O:
int
to hold the sum and initialise it to 0
. int
来保存和并将其初始化为0
。 select()
to test if there is something to read from fileno(stdin)
. select()
测试是否要从fileno(stdin)
读取某些内容。 read()
to read in exactly one char
. read()
精确读取一个char
。 isdigit()
to test whether it's a number and if substract 48
and add the result to the sum. isdigit()
来测试它是否为数字以及是否减去48
,并将结果加到总和上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.