简体   繁体   English

在C或C ++中,我如何在没有输入或空格的情况下为程序提供输入

[英]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:

  • Define an int to hold the sum and initialise it to 0 . 定义一个int来保存和并将其初始化为0
  • Use select() to test if there is something to read from fileno(stdin) . 使用select()测试是否要从fileno(stdin)读取某些内容。
  • Use read() to read in exactly one char . 使用read()精确读取一个char
  • Use isdigit() to test whether it's a number and if substract 48 and add the result to the sum. 使用isdigit()来测试它是否为数字以及是否减去48 ,并将结果加到总和上。
  • Start over. 重来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM