gpt4 book ai didi

c - If/else-if 语句的问题 - 基于文本的冒险 [C 编程]

转载 作者:行者123 更新时间:2023-11-30 14:50:01 25 4
gpt4 key购买 nike

Text Based Adventure issue/question for lines (78-100)

目前,在我的第一堂编程课中,我们被要求做一个项目和一个小组项目。我的团队选择的项目是基于文本的冒险。我正在做大部分工作或进行研究,但遇到了麻烦,无法用谷歌搜索我的问题。我的问题是,在第 78-100 行中,我必须在 if/else 语句中使用 _(下划线)算作空格。有没有办法让任何玩这个游戏的人都可以输入“Look around”而不是“Look_around”?我发现总是使用下划线来回答包含多个单词的问题可能非常不方便。任何帮助和/或建议将不胜感激。

我的代码如下:

// ConsoleApplication2.cpp : Defines the entry point for the console 
application.
//

#include "stdafx.h"
#include <conio.h> // Allows the getch function //
#include <string.h> // Allows string/char functions //
#include <stdlib.h>
#include <stdio.h>
#define getch() _getch() // Allows you to use getch()
using namespace std;

int main()
{
char user_input[25];
char name[50];
// user_input = (char)toupper(user_input); // This is example of what
perhaps getting user_input to print capital letters //


printf("What is your name, weary traveler?\n\n"); // Introduction //
scanf("%s", name);
printf("You wake up to find yourself on the ground, in a dimmly lit room. \n");
printf("You stand up and look around, only to see a shadowy figure, it speaks, ");
printf("\nYou see %s, you're caught in a place that is both awake and dormant.\n", name);
printf("In this place, it's your fears that come to life, ones that'll haunt you,\n");
printf("in short, you're in your own nightmare. ");
printf("It's best you find a way to wake up soon or else you'll be trapped here forever. . .\n\n\n");


printf("------------------------------Press any key to continue-------------------------"); // End of introduction //
getch(); // Grabs any key hit and allows it to go to the next line of code //

system("cls"); // Clears screen so you can begin the game //
printf("\n\n\n----------------------Chapter 1: Nightmare on 2325 Chester Blvd-----------------\n\n\n"); // Begins Chapter 1 //
printf("You turn away from the shadowy figure to make sense where you are, ");
printf(" but when you turn back around to ask the figure where you are, you ");
printf("notice no one is there, you ask yourself \"Where am I?\"\n\n\n");
printf("You figure, to get the answers you seek, you must explore.\n");
printf("Where do you go? (Options: North/Up, East/Right, South/Down, West/Right)\n\n\n");
scanf("%s", user_input);


if ((strcmp(user_input, "North") == 0) || (strcmp(user_input, "north") == 0) || (strcmp(user_input, "Up") == 0) || (strcmp(user_input, "up") == 0)) { // This intends that you go North, expand onto this If statement to specify what happens if you go North //
printf("\nYou are going up.\n");
}
if ((strcmp(user_input, "East") == 0) || (strcmp(user_input, "east") == 0) || (strcmp(user_input, "Right") == 0) || (strcmp(user_input, "right") == 0)) { // This intends that you go East, expand onto this If statement to specify what happens if you go East //
printf("\nYou are going right.\n");
}
if ((strcmp(user_input, "South") == 0) || (strcmp(user_input, "south") == 0) || (strcmp(user_input, "Down") == 0) || (strcmp(user_input, "down") == 0)) { // This intends that you go South, expand onto this If statement to specify what happens if you go South //
printf("\nYou are going down.\n");
}
if ((strcmp(user_input, "West") == 0) || (strcmp(user_input, "west") == 0) || (strcmp(user_input, "Left") == 0) || (strcmp(user_input, "left") == 0)) { // This intends that you go West, expand onto this If statement to specify what happens if you go West //
printf("\nYou are going left.\n");
}
if ((strcmp(user_input, "Quit") == 0)) { // Without this if statement, the fourth If would only right "You're going left." Thus, this eliminates that so you can continue to have the if statement followed by the else statement.
exit(0);
}


// Begin doing if statements for North and what inputs/scenarios would go into that "if" state

if ((strcmp(user_input, "North") == 0) || (strcmp(user_input, "north") == 0) || (strcmp(user_input, "Up") == 0) || (strcmp(user_input, "up") == 0)) { // This intends that you go North, expand onto this If statement to specify what happens if you go North //
printf("\nYou walk down the hallway in front of you, you find several classroom doors shut, except one which seems to be cracked open, ");
printf("however, you still haven't reached the end of the hall\n.");
printf("\nWhat do you want to do? Where do you want to go? (Options: Straight/Forward or Classroom)\n\n");
scanf("%s", user_input);
}

if ((strcmp(user_input, "Straight") == 0) || (strcmp(user_input, "straight") == 0) || (strcmp(user_input, "Forward") == 0) || (strcmp(user_input, "forward") == 0)) { // Can only use one word to do the printf line, meaning straight works but go straight doesn't //
printf("You continue");
}

if ((strcmp(user_input, "Classroom") == 0) || (strcmp(user_input, "classroom") == 0)) {

printf("You open up the half shut door of the classroom and begin to enter, only to see it's pitch black inside.");
// You enter the classroom, it's dark. //
}

printf("\n\nWhat do you do?\n\n"); // Asks the user where they want to go, down the hallway or in the classroom //
scanf("%s", user_input);

if ((strcmp(user_input, "Look_around") == 0)) {// While loop that'll force users to have to look for a light source //

printf("It's too dark to see, maybe there's a light source around here.");
printf("\n\nWhat do you do?\n\n"); // loops back up //
scanf("%s", user_input);
printf("\n");
}

if ((strcmp(user_input, "Find_lightswitch") == 0)) {
printf("The classroom light turns on. ");
printf("You glance around the room and find that the desks are pushed against the back windows. \n");
printf("You notice that the desks can be moved, but you don't know if you should.");
}

printf("\n\nWhat do you wish to do?\n\n");
scanf("%s", user_input);
printf("\n\n\n");

if ((strcmp(user_input, "Leave_classroom") == 0) || (strcmp(user_input, "Leave_the_classroom") == 0) || (strcmp(user_input, "Leave") == 0) || (strcmp(user_input, "Leave_room") == 0) || (strcmp(user_input, "Leave_the_room") == 0)) {
printf("You attempt to leave the classroom, but find that the door is locked.");
}
else if ((strcmp(user_input, "Move_desks") == 0)) {
printf("Moving the desks reveals, \"You are not dreaming\", is written in green on the window."); // \" and \" gives "and" //
printf("You write on the window \"Yes I am\" and turn around and begin walking away, you hear a splash.\n\n");
printf("\n\n\n");
}

printf("The Window - \"Yes you are.\"\n\n");
printf("\t\t\t\"No I'm not, wait are you just trying to confuse me?\" - %s \n\n", name);
printf("The Window - \"Yes, you seem like you could be fooled, so I fooled you\"\n\n");
printf("\t\t\t\"Well, I know I'm dreaming and that's that.\" - %s \n\n", name);
printf("The Window - \"Since I'm unable to fool you, so you say,");
printf(" you must answer these questions to be able to leave the room.\"\n\n");
return 0; //"You write on the window \"Yes I am\" and turn around and begin walking away, you hear a splash.\n\n");

}

最佳答案

如果您不知道用户将输入多少个单词,最好使用 fgets() 而不是 scanf()fgets() 将继续读取最大字符数,直到遇到换行符。因此,您可以读取整个用户输入,然后弄清楚如何使用空格作为分隔符将其分解为单词(strtok() 在这种情况下可能会正常工作)。

# define MAX_INP 256

int main()
{
char userInp[MAX_INP];

if (fgets(userInp, MAX_INP, stdin) == NULL) {
// handle error
}

printf("%s\n", userInp);

// now break userInp into individual words
}

您的输入缓冲区是 user_inputMAX_INP 应等于输入缓冲区的大小,在上面的示例中,该大小为 25。在这种情况下,在大多数 24 个字母将被读取并存储在 user_input 中,并以 \0 结尾。 stdin 表示您希望从标准输入读取,而不是从文件读取。

我会查找 fgets() 的联机帮助页以获取更多详细信息。如果您了解 scanf() 的工作原理,那么理解 fgets() 应该不难。

关于c - If/else-if 语句的问题 - 基于文本的冒险 [C 编程],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49244610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com