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;
}
相关推荐
lsx2024064 分钟前
Perl 面向对象编程指南
开发语言
Allen Bright25 分钟前
【Java基础-46.3】Java泛型通配符详解:解锁类型安全的灵活编程
java·开发语言
画个逗号给明天"38 分钟前
C++STL容器之list
开发语言·c++
hrrrrb1 小时前
【Java】Java 常用核心类篇 —— 时间-日期API(上)
java·开发语言
小突突突1 小时前
模拟实现Java中的计时器
java·开发语言·后端·java-ee
七禾页话1 小时前
垃圾回收知识点
java·开发语言·jvm
web137656076431 小时前
Scala的宝藏库:探索常用的第三方库及其应用
开发语言·后端·scala
鱼嘻1 小时前
Linux自学day23-进程和线程
linux·服务器·c语言·进程和线程
煤炭里de黑猫2 小时前
Lua C API :使用 lua_tonumber 函数从 Lua 栈中提取数值
开发语言·lua
闲猫2 小时前
go 反射 interface{} 判断类型 获取值 设置值 指针才可以设置值
开发语言·后端·golang·反射