对于作业,我的任务是创建一个非常简单的文本编辑器。我有一个2d字符串,有5行预设文本。 编辑必须(按顺序):
我的问题是,一旦我要求用户再次编辑,我编辑的文本行不会更新,因此不会显示在最近打印的2d字符串数组中。我认为将1d数组传回2d数组有问题吗?
我的代码中的函数是必需的,其中的参数不能以任何方式更改。除了函数'editAgain'之外,该函数可以随意更改,也允许使用新函数。
我知道这会编译一两个错误,但它与我将1d字符串'original'传回2d字符串'buffer'的问题有关。
提前致谢!
int replaceEntireString(char original [],char substring [],char replace []);
int editAgain(char buffer [] [COLS],char substring [],char replace []);
int main(void){
char editDecision = '?';
int editOperation = 0;
char buffer[ROWS][COLS];
char substring[] = "?";
char replace[] = "?";
int editedLine = 0;
strcpy(buffer[0],"university of guelph");
strcpy(buffer[1],"university of guelph");
strcpy(buffer[2],"university of guelph");
strcpy(buffer[3],"university of guelph");
strcpy(buffer[4],"university of guelph");
printf("\nWelcome to this very rudimentary text editor!\n");
while(editDecision != 'Y' && editDecision != 'y' && editDecision != 'N' && editDecision != 'n'){
printf("\nWould you like to edit text? If not you will be exited: Y/N\n");
scanf("%c", &editDecision);
getchar();
switch(editDecision){
case 'y':
case 'Y':
while(editedLine >= 0){
printf("Choose a line to edit: 0, 1, 2, 3, 4\n");
printf("0: %s\n", buffer[0]);
printf("1: %s\n", buffer[1]);
printf("2: %s\n", buffer[2]);
printf("3: %s\n", buffer[3]);
printf("4: %s\n", buffer[4]);
scanf("%d", &editedLine);
getchar();
if(editedLine > 4){
printf("Invalid Entry!\n");
}
if(editedLine <= 4){
break;
}
}
while(editOperation != 1 && editOperation != 2){
printf("Choose an operation: 1. Replace entire line. 2. Replace a substring.\n");
scanf("%d", &editOperation);
getchar();
switch(editOperation){
case 1:
replaceEntireString(buffer[editedLine], substring, replace);
break;
case 2:
replaceInString(buffer[editedLine], substring, replace);
break;
default:
printf("Invalid Entry!\n");
break;
}
}
break;
case 'n':
case 'N':
printf("Unedited text:\n");
printf("0: %s\n", buffer[0]);
printf("1: %s\n", buffer[1]);
printf("2: %s\n", buffer[2]);
printf("3: %s\n", buffer[3]);
printf("4: %s\n", buffer[4]);
printf("Goodbye for now!\n");
break;
default:
printf("Invalid Entry!\n");
break;
}
}
return 0;
}
int editAgain(char buffer [] [COLS],char substring [],char replace []){
char editDecision = '?';
int editOperation = 0;
int editedLine = 0;
while(editDecision != 'Y' && editDecision != 'y' && editDecision != 'N' && editDecision != 'n'){
printf("\nWould you like to edit text? If not you will be exited: Y/N\n");
scanf("%c", &editDecision);
getchar();
switch(editDecision){
case 'y':
case 'Y':
while(editedLine >= 0){
printf("Choose a line to edit: 0, 1, 2, 3, 4\n");
printf("0: %s\n", buffer[0]);
printf("1: %s\n", buffer[1]);
printf("2: %s\n", buffer[2]);
printf("3: %s\n", buffer[3]);
printf("4: %s\n", buffer[4]);
scanf("%d", &editedLine);
getchar();
if(editedLine > 4){
printf("Invalid Entry!\n");
}
if(editedLine <= 4){
break;
}
}
while(editOperation != 1 && editOperation != 2){
printf("Choose an operation: 1. Replace entire line. 2. Replace a substring.\n");
scanf("%d", &editOperation);
getchar();
switch(editOperation){
case 1:
replaceEntireString(buffer[editedLine], substring, replace);
break;
case 2:
replaceInString(buffer[editedLine], substring, replace);
break;
default:
printf("Invalid Entry!\n");
break;
}
}
break;
case 'n':
case 'N':
printf("Unedited text:\n");
printf("0: %s\n", buffer[0]);
printf("1: %s\n", buffer[1]);
printf("2: %s\n", buffer[2]);
printf("3: %s\n", buffer[3]);
printf("4: %s\n", buffer[4]);
printf("Goodbye for now!\n");
break;
default:
printf("Invalid Entry!\n");
break;
}
}
return 0;
} int replaceEntireString(char original [],char substring [],char replace []){
printf("Enter the new string text: \n");
fgets(original, COLS, stdin);
if(original[strlen(original)-1] == '\n'){
original[strlen(original)-1] = '\0';
}
printf("\nThe edited line text is now: %s\n", original);
editAgain(original, substring, replace);
return 0;
}
答案 0 :(得分:0)
问题是由以下行引起的:
main();
replaceEntireString
中的。
您正在调用堆栈中向main
添加另一个调用并再次创建字符串。新输入的main
中的变量与之前main
中的变量没有任何关系。
删除该行。
答案 1 :(得分:0)
在replaceEntireString
功能结束时,您致电 main
而不是回到replaceEntireString
被调用的地方。然后使用 new 变量集创建新的堆栈帧。这些新变量 - 其中的新buffer
- 已初始化,因此您可以重新开始相同的工作,而不是完成之前开始的那些工作。
治愈:删除
main();
来自replaceEntireString
函数的行。