c primer plus---例程程序纠正

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdbool.h>

char * s_gets(char* st, int n);

enum spectrum { red, orange, yellow, green, blue, violet };
const char* colors[] = { "red","orange","yellow","green","blue","violet"};

#define LEN 30

int main(void)
{
	char choice[LEN];
	enum spectrum color;
	bool color_is_found = false;

	puts("enter a color (empty line to quit):");
	while (s_gets(choice,LEN) != NULL && choice[0] != '\0')
	{
		//printf("");
		for (color = red; color <= violet; color++)
		{
		printf("%s\n", colors[color]);
		printf("%s\n", choice);
		printf("%d\n", (strcmp(choice, colors[color])));

        if (strcmp(choice, colors[color]) == 0)
             //strcmp()函数比较两个字符串是否一样大
			{
				color_is_found = true;
				break;
			}
		}
		//printf("%d\n", color_is_found);
		if (color_is_found)
		{
			switch (color)
			{
			case red:puts("roses are red.");
				break;
			case orange:puts("poppies are orange.");
				break;
			case yellow:puts("sunflowers are yellow.");
				break;
			case green:puts("grass is green.");
				break;
			case blue:puts("bluebells are blue.");
				break;
			case violet:puts("violets are violet.");
				break;
			}
		}
		else 
		    {
			printf("I don't know about the color %s.\n", choice);
		    }
		color_is_found = false;
		puts("next color,please (empty line to quit):");
	}
	puts("godbye!");
	return 0;
}

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 = '\n';
		}
		else 
			while (getchar() != '\n')
			{
				continue;
			}
	}
	return ret_val;
}

c primer plus中14.15的例程程序,有错,不能显示正确程序效果。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdbool.h>

char * s_gets(char* st, int n);

enum spectrum { red, orange, yellow, green, blue, violet };
const char* colors[] = { "red","orange","yellow","green","blue","violet"};

#define LEN 30

int main(void)
{
	char choice[LEN];
	enum spectrum color;
	bool color_is_found = false;

	puts("enter a color (empty line to quit):");
	while (s_gets(choice,LEN) != NULL && choice[0] != '\0')
	{
		//printf("");
		for (color = red; color <= violet; color++)
		{
		printf("%s\n", colors[color]);
		printf("%s\n", choice);
		printf("%d\n", (strcmp(choice, colors[color])));

        if (strcmp(choice, colors[color]) == 1)
             //strcmp()函数比较两个字符串是否一样大
			{
				color_is_found = true;
				break;
			}
		}
		//printf("%d\n", color_is_found);
		if (color_is_found)
		{
			switch (color)
			{
			case red:puts("roses are red.");
				break;
			case orange:puts("poppies are orange.");
				break;
			case yellow:puts("sunflowers are yellow.");
				break;
			case green:puts("grass is green.");
				break;
			case blue:puts("bluebells are blue.");
				break;
			case violet:puts("violets are violet.");
				break;
			}
		}
		else 
		    {
			printf("I don't know about the color %s.\n", choice);
		    }
		color_is_found = false;
		puts("next color,please (empty line to quit):");
	}
	puts("godbye!");
	return 0;
}

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 = '\n';
		}
		else 
			while (getchar() != '\n')
			{
				continue;
			}
	}
	return ret_val;
}
相关推荐
Qter_Sean33 分钟前
自己动手写Qt Creator插件
开发语言·qt
何曾参静谧37 分钟前
「QT」文件类 之 QIODevice 输入输出设备类
开发语言·qt
爱吃生蚝的于勒2 小时前
C语言内存函数
c语言·开发语言·数据结构·c++·学习·算法
小白学大数据4 小时前
Python爬虫开发中的分析与方案制定
开发语言·c++·爬虫·python
冰芒猓5 小时前
SpringMVC数据校验、数据格式化处理、国际化设置
开发语言·maven
失落的香蕉5 小时前
C语言串讲-2之指针和结构体
java·c语言·开发语言
红中马喽5 小时前
JS学习日记(webAPI—DOM)
开发语言·前端·javascript·笔记·vscode·学习
杜杜的man5 小时前
【go从零单排】Closing Channels通道关闭、Range over Channels
开发语言·后端·golang
java小吕布6 小时前
Java中Properties的使用详解
java·开发语言·后端
versatile_zpc6 小时前
C++初阶:类和对象(上)
开发语言·c++