《C prime plus 第六版》程序清单14.16

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define LEN 81
char* s_gets(char* st, int n);
char showmenu(void);
void eatline(void);
void show(void (*fp)(char*), char* str);
void ToUpper(char*);
void ToLower(char*);
void Transpose(char*);
void Dummy(char*);

int main(void)
{
	char line[LEN];
	char copy[LEN];
	char choice;
	void (*pfun)(char*);

	puts("Enter a string (empty line to quit:)");
	while (s_gets(line, LEN) != NULL && line[0] != '\0')
	{
		while ((choice = showmenu()) != 'n')
		{
			switch (choice)
			{ 
				case 'u': 
				pfun = ToUpper;     
				break;
				case 'l': 
				pfun = ToLower;   
				break;
				case 't': 
			    pfun = Transpose;  
				break;
				case 'o': 
				pfun = Dummy;   
				break;
			}
			strcpy_s(copy,LEN,line);
			show(pfun, copy);
		}
		puts("Enter a string (empty line to quit):");
	}
	puts("Bye!");

	return 0;
}

char showmenu(void)
{
	char ans;
	puts("Enter menu choice:");
	puts("u) uppercase          l)lowercase");
	puts("t) transposed case    o)original case");
	puts("n) next string");
	ans = getchar();
	ans = tolower(ans);
	eatline();
	while (strchr("ulton", ans) == NULL)
	{
		puts("Please enter a u,l,t,o, or n:");
		ans = tolower(getchar());
		eatline();
	}

	return ans;
}

void eatline(void)
{
	while (getchar() != '\n')
		continue;
}

void ToUpper(char* str)
{
	while (*str)
	{
		*str = toupper(*str);
		str++;
	}
}

void ToLower(char* str)
{
	while (*str)
	{
		*str = tolower(*str);
		str++;
	}
}

void Transpose(char* str)
{
	while (*str)
	{
		if (islower(*str))
			*str = toupper(*str);
		else if (isupper(*str))
			*str = tolower(*str);
		str++;
	}
}

void Dummy(char* str)
{

}

void show(void(*fp)(char*), char* str)
{
	(*fp)(str);
	puts(str);
}

char* s_gets(char* st, int n)
{
	char* ret_val;
	char* find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');
		if (find)
			*find = '\0';
		else
			while (getchar() != '\n')
				continue;
	}
	return ret_val;
}

为什么报错报错43行未初始化局部变量pfun?原因是什么?

 

第19行:void (*pfun)(char*) = NULL;

第41行:strncpy(copy,line, LEN);

第112-116行:show函数


void show(void(*fp)(char*), char* str)
{
    if (fp != NULL)
    {
        (*fp)(str);
        puts(str);
    }
    else
        puts("fp is NULL\n");
}

 

完整代码

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define LEN 81
char* s_gets(char* st, int n);
char showmenu(void);
void eatline(void);
void show(void (*fp)(char*), char* str);
void ToUpper(char*);
void ToLower(char*);
void Transpose(char*);
void Dummy(char*);
 
int main(void)
{
	char line[LEN];
	char copy[LEN];
	char choice;
	void (*pfun)(char*) = NULL;
 
	puts("Enter a string (empty line to quit:)");
	while (s_gets(line, LEN) != NULL && line[0] != '\0')
	{
		while ((choice = showmenu()) != 'n')
		{
			switch (choice)
			{ 
				case 'u': 
				pfun = ToUpper;     
				break;
				case 'l': 
				pfun = ToLower;   
				break;
				case 't': 
			    pfun = Transpose;  
				break;
				case 'o': 
				pfun = Dummy;   
				break;
			}
			strncpy(copy,line, LEN);
			show(pfun, copy);
		}
		puts("Enter a string (empty line to quit):");
	}
	puts("Bye!");
 
	return 0;
}
 
char showmenu(void)
{
	char ans;
	puts("Enter menu choice:");
	puts("u) uppercase          l)lowercase");
	puts("t) transposed case    o)original case");
	puts("n) next string");
	ans = getchar();
	ans = tolower(ans);
	eatline();
	while (strchr("ulton", ans) == NULL)
	{
		puts("Please enter a u,l,t,o, or n:");
		ans = tolower(getchar());
		eatline();
	}
 
	return ans;
}
 
void eatline(void)
{
	while (getchar() != '\n')
		continue;
}
 
void ToUpper(char* str)
{
	while (*str)
	{
		*str = toupper(*str);
		str++;
	}
}
 
void ToLower(char* str)
{
	while (*str)
	{
		*str = tolower(*str);
		str++;
	}
}
 
void Transpose(char* str)
{
	while (*str)
	{
		if (islower(*str))
			*str = toupper(*str);
		else if (isupper(*str))
			*str = tolower(*str);
		str++;
	}
}
 
void Dummy(char* str)
{
 
}
 
void show(void(*fp)(char*), char* str)
{
    if (fp != NULL)
    {
        (*fp)(str);
        puts(str);
    }
    else
        puts("fp is NULL\n");
}
 
char* s_gets(char* st, int n)
{
	char* ret_val;
	char* find;
 
	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');
		if (find)
			*find = '\0';
		else
			while (getchar() != '\n')
				continue;
	}
	return ret_val;
}

说明你的程序没有在switch里面没有获取到值,检查case语句,是不是应该加一个default

说明你的程序没有在switch里面没有获取到值,检查case语句,是不是应该加一个default,获取说循环嵌套有逻辑错误。

你要写调试代码,把过程打印出来,一步一步查找问题

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632