【发布时间】:2015-12-13 13:40:30
【问题描述】:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
char split_text(){
int bytes_las;
int antal_bytes = 40;
char *min_strang;
printf("Enter text: ");
min_strang = (char *) malloc (antal_bytes + 1);
bytes_las = getline (&min_strang, &antal_bytes, stdin);
printf("\n%s",min_strang);
}
int main()
{
printf(" MENU:\n");
printf("1) Split text\n");
printf("2) Upper case to lower case\n");
printf("3) Lower case to upper case\n");
printf("4) Remove character\n");
printf("5) Add character\n");
printf("6) Replace character\n");
printf("7) Statistics\n");
printf("8) Sort text\n");
printf("0) Exit\n");
int i;
char option;
for(i=0; i<5;i++){
printf("I want: "); //assign the option number
option = getchar();
switch(option) { //use function assigned to what option
case '1' :
printf("do for 1\n");
split_text();
break;
case '2' :
printf("do for 2\n");
break;
case '3' :
printf("do for 3\n");
break;
case '4' :
printf("do for 4\n");
break;
case '5' :
printf("do for 5\n");
break;
case '6' :
printf("do for 6\n");
break;
case '7' :
printf("do for 7\n");
break;
case '8' :
printf("do for 8\n");
break;
case '0' :
printf("do for 0\n");
exit(0);
default :
break;
}
}
}
这是我在单独文件中的函数。调用它时,函数会读取并且我得到正确的“输入文本:”输出。但是,没有限制。当我按下 Enter 键时,什么也没有发生,只是继续输入。
我在 Ubuntu 上,只使用 C,没有编译错误。
【问题讨论】: