C中的未知输入数 [英] Unknown number of inputs in C

查看:69
本文介绍了C中的未知输入数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过C或C ++程序接受输入,但我不知道用户给出的输入数量.所有的数字都是数字,但我只知道要读取"n"个字符,我们会运行从i到n的循环,并以"n"个长度的数组读取它们.

I have to take a input either through C or C++ program in which I don''t know the number of inputs given by user. All the numbers are number, but I only know that to read ''n'' number of characters we run a loop from i to n and read them in an array of ''n'' length.

for(i=0;l<n;i++)>
{
   scanf("%d",&arr[i]);
}


其中n是已知的.
如果我不知道n值,该怎么办.


where n is already known.
What if I don''t know ''n'' value.

推荐答案

在99%的情况下,如Sandeep Mewara所说的n未知您的程序.您通常从某处获取n.在您的情况下,您应该运行无限循环(例如,使用for (;;)),并且如果用户输入特殊值(例如-1或零),则应从中中断(使用break关键字).您还应该检查scanf()的返回值是否为1或其他,请查看其文档以了解其返回值.如果没有错误检查,当scanf()失败时,您可能最终会输入垃圾.在无限循环中,输入数字后,您要么立即处理输入的数字,要么算法需要输入项的总数(例如,由于要对数字进行排序),则必须将每个项放入一个动态调整数组的大小(如std::vector),并从无限循环中断后,您具有n,它是数组的大小.名称std::vector可能令人困惑,但是它是一个可以调整大小的数组(该语言提供的普通C数组的大小是固定的,并且其长度不能为零).
Like Sandeep Mewara said in 99% of the cases n is unknown when you write your program. You usually get n from somewhere. In your case you should run an infinite loop (for example with for (;;)) and you should break out from it (with the break keyword) if the user enters a special value (like -1 or zero). You should also check if the return value of scanf() is one or something else, check its documentation about its return value. Without error checking you might end up inputting garbage when scanf() fails. In your infinite loop when a number is entered you either process it immediately as it comes in, or if your algorithm needs the total number of input items (for example because you want to sort the numbers) then you have to put each item into a dynamically resizing array (like std::vector) and after breaking out from the infinite loop you have n which is the size of your array. The name std::vector might be confusing but it is an array you can resize (the normal C array provided by the language are fixed in size and their length can not be zero).


如果我不知道"n"值怎么办
好吧,很早就不知道输入的数量是很普遍的.

通常,第一个问题是您要输入多少输入?"的值将被视为"n"值.定义完毕后,其余代码如上.
What if I don''t know ''n'' value
Well, it''s quite common to not know the number of inputs earlier.

Mostly, the first question would be, ''how many inputs you want to enter?'' Value of this will be taken as ''n'' value. Once this is defined, rest of the code is as above.


使用C ++,您可以使用"new"在目标数组中分配新位置,并使用"delete"将其重新分配,使用while(input!= x)循环,其中x是打破循环的特定输入代码.

扫描文本而不是数字,然后在处理输入之前使用strcmp检查它是否为"EXIT"或您要选择的其他任何关键字(用作循环中的条件),如果不是,请尝试转换输入数字(atoi会有所帮助)...

祝你好运!
Using C++ you can use "new" to allocate new positions in the destination array and "delete" to deallocate them, use a while(input!=x) loop where x would be a specific input code to break the loop.

Scan for text not numbers, before processing the input check it using strcmp and see if it is ''EXIT'' or any other keyword that you would choose (used as the condition in the loop), if it is not, try to convert the input to number (atoi will help)...

Good luck!


这篇关于C中的未知输入数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆