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;
}
相关推荐
茶猫_4 分钟前
力扣面试题 39 - 三步问题 C语言解法
c语言·数据结构·算法·leetcode·职场和发展
初学者丶一起加油6 分钟前
C语言基础:指针(数组指针与指针数组)
linux·c语言·开发语言·数据结构·c++·算法·visual studio
程序员大阳8 分钟前
闲谭Scala(1)--简介
开发语言·后端·scala·特点·简介
直裾9 分钟前
scala图书借阅系统完整代码
开发语言·后端·scala
百流9 分钟前
scala基础学习_运算符
开发语言·学习·scala
百流11 分钟前
scala基础学习(数据类型)-数组
开发语言·学习·scala
Zilliz Planet36 分钟前
Milvus×EasyAi:如何用java从零搭建人脸识别应用
java·开发语言·milvus
天天进步201543 分钟前
Java全栈项目 - 汽车维修服务管理平台
java·开发语言·汽车
qq_430583971 小时前
QT笔记- QTreeView + QFileSystemModel 当前位置的保存与恢复 #选中 #保存当前索引
开发语言·笔记·qt