简体   繁体   English

请帮助,使用%echo参数

[英]Please help, use of %echo argument

I have this problem of how to use echo to supplement input and tell the program how many output to give. 我有这个问题,如何使用echo补充输入并告诉程序要输出多少输出。

Basically, I have the program logic sorted out, which is to randomly select a subset of x number from y inputs. 基本上,我已经整理出程序逻辑,这是从y输入中随机选择x个数字的子集。 My code is here: 我的代码在这里:

The problem is, how can I forward the argc variable in the main argument to the program execution?? 问题是,如何将main参数中的argc变量转发给程序执行?

The context requires me to call: %echo ABCDEFG | 上下文需要我致电:%echo ABCDEFG | Subset 2, which suppose to print 2 characters at random. 子集2,假定随机打印2个字符。 But I can't do this, the number 2 seems cannot be forwarded inside here. 但是我不能这样做,数字2似乎无法在此处转发。 And the call of %echo command does not seem to work as well. 而且%echo命令的调用似乎也不起作用。 Anybody please help 有人请帮忙

int main(int argc){
RandomizedQueue<char> q;
char input;


while(cin.peek() != '\n'){
    cin >> input;
    q.enqueue(input);
}

for(int k = 0; k < argc; k++)
    cout << q.dequeue() << endl;

return 0;

} }

I think the % in %echo is not something you're supposed to type, but rather signifies the command prompt itself. 我认为%echo中的%echo不是您应该键入的内容,而是表示命令提示符本身。 So you type this: 所以您输入:

echo A B C D E F G | ./Subset 2

Assuming your program is named Subset and is in the current working directory. 假设您的程序名为Subset,并且位于当前工作目录中。 Then, Subset will receive argc==2 and argv=={"./Subset", "2"}. 然后,子集将接收argc == 2和argv == {“ ./ Subset”,“ 2”}。 You'll need to declare main in the standard way (you're missing argv): 您需要以标准方式声明main(缺少argv):

int main(int argc, char* argv[])

Once you do that, argv[1] will be "2" as a string (the program's first argument). 完成后,argv [1]将作为字符串“ 2”(程序的第一个参数)。 You can do something like this to convert it to an int: 您可以执行以下操作将其转换为int:

int count = atoi(argv[1]);

You should read up on the declaration of main, because argc is not the argument as a number, it is the number of arguments. 您应该阅读main的声明,因为argc不是作为数字的参数,而是参数的数量。 And declaring main with argc but not argv is highly atypical. 用argc而不是argv声明main是非常不典型的。

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

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