C 我可以从程序中推送用户输入吗
对不起,标题可能没有什么意义,我不知道该叫什么名字C 我可以从程序中推送用户输入吗,c,C,对不起,标题可能没有什么意义,我不知道该叫什么名字 //history of the 10 previous commands char *history[10][140]; while (1) { printf("Enter command:"); fgets(input, MAX, stdin); //Handle other commands //true if user entered command to call previous command
//history of the 10 previous commands
char *history[10][140];
while (1) {
printf("Enter command:");
fgets(input, MAX, stdin);
//Handle other commands
//true if user entered command to call previous command
if(thisIsTrue){
//strToInt gets number from the user input
int histNum = strToInt(input);
char *nextinput = history[histNum];
//Not sure what to do here
}
}
因此,我能够获得所需的nextinput,但是我不确定如何将其传递到下一个循环中,因为大多数命令都是从输入命令的用户那里获取的,对于这个if语句(如果用户输入了特定命令),需要执行旧的命令。我将旧命令存储在历史记录中,并且能够获得所需的下一个输入,但我不确定如何将其传递到下一个循环中。是否有一种方法可以模拟用户输入,以便FGET拾取下一个输入,或者我将如何执行此操作?(不希望在if语句中复制粘贴所有(//处理其他命令)
Example of program running:
Enter command:command1
Enter command:command2
Enter command:command3
Enter command:command4
Enter command:command5
Enter command:command6
Enter command:command7
Enter command:command8
Enter command:command9
Enter command:command10
Enter command:command11
Enter command:command1
Enter command:hlist
4 command4
5 command5
6 command6
7 command7
8 command8
9 command9
10 command10
11 command11
12 command1
13 hlist
Enter command:!11
command11 //this is the value of new_input
我只是不知道如何将command11推入while循环
char *history[10][140];
这意味着您有一个二维数组10*140,该数组的每个元素不是一个字符而是一个字符串(char*
)。这不是您想要的
chr *nextinput = history[histNum];
您确定它是chr
而不是char
此外,如果它真的是char*nextinput=history[histNum];,那么它是错误的,因为历史是一个2d数组。如果我理解你的要求,它一定是这样的:
char *history[rows][columns];
int user_has_requested_previous_command_flag = 0;
while(1)
{
switch(user_has_requested_previous_command_flag)
{
case 0:
printf("Enter command:");
fgets(input, MAX, stdin);
//Handle other commands
if(thisIsTrue)
{
//strToInt gets number from the user input
int histNum = strToInt(input);
input = history[histNum];
user_has_requested_previous_command_flag = 1; // !!!!!!!!!
}
break;
case 1:
// Do whatever you need to do here with the previous input from the history
user_has_requested_previous_command_flag = 0;
break;
}
}
抱歉,是的char not chr,但是char*nextinput=history[histNum];工作完全正常,我只是不知道如何将其推入fgets(如果可能的话),或者如何将下一个命令推入该程序。您不能将其推入
fgets()
。您需要实现从历史数组获取值的逻辑,而不是调用fgets()
。