- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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_input
,MAX_INP
应等于输入缓冲区的大小,在上面的示例中,该大小为 25。在这种情况下,在大多数 24 个字母将被读取并存储在 user_input
中,并以 \0
结尾。 stdin
表示您希望从标准输入读取,而不是从文件读取。
我会查找 fgets()
的联机帮助页以获取更多详细信息。如果您了解 scanf()
的工作原理,那么理解 fgets()
应该不难。
关于c - If/else-if 语句的问题 - 基于文本的冒险 [C 编程],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49244610/
表架构 DROP TABLE bla; CREATE TABLE bla (id INTEGER, city INTEGER, year_ INTEGER, month_ INTEGER, val I
我需要拆分字符串/或从具有以下结构的字符串中获取更容易的子字符串。 字符串将来自 window.location.pathname 或 window.location.href,看起来像 text/n
每当将对象添加到数组中时,我都会尝试更新 TextView ,并在 TextView 中显示该文本,如下所示: "object 1" "object 2" 问题是,每次将新对象添加到数组时,它都会覆盖
我目前正在寻找使用 Java 读取网站可见文本并将其存储为纯文本字符串的方法。 换句话说,我想转换成这样: Hello stupid World进入“ Hello World ” 或者类似的东西 Un
我正在尝试以文本和 HTML 格式发送电子邮件,但无法正确发送正确的 header 。特别是,我想设置 Content-Type header ,但我找不到如何为 html 和文本部分单独设置它。 这
我尝试了上面的代码,但我无法绑定(bind)文本,我怎样才能将资源内部文本 bloc
我刚刚完成了 Space Shooter 教程,由于没有 GUIText 对象,所以我创建了 UI.Text 对象并进行了相应的编码。它在统一播放器中有效,但在构建 Web 应用程序后无效。我花了一段
我有这个代码: - (IBAction)setButtonPressed:(id)sender { NSUserDefaults *sharedDefaults = [[NSUserDefau
抱歉标题含糊不清,但我想不出我想在标题中做什么。无论如何,对于图像上的文本,我使用了 JLabel 文本并将其添加到图标中。 JLabel icon = new JLabel(new Imag
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我在将 Twitter 嵌入到我从 HTML 5 转换的 wordpress 运行网站时遇到问题。 我遇到的问题是推文不是我的自定义字体... 这是我无法使用任何 css 定位的 HTML 代码,我正
我正在尝试找到解决由于使用以下形式的代码而导致的冗余字符串连接问题的最佳方法: logger.debug("Entering loop, arg is: " + arg) // @1 在大多数情况下,
我写了这个测试 @Test public void removeRequestTextFromRouteError() throws Exception { String input = "F
我目前正在创建一个正则表达式来拆分所有匹配以下格式的字符串:&[文本],并且需要获取文本。字符串可能类似于:something &[text] &[text] everything &[text] 等
有没有办法将标题文本从一个词变形为另一个词,同时保留两个词中使用的字母?我看过的许多 css 文本动画大多是视觉的,很少有旋转整个单词的。 我想要做的是从一个词过渡,例如“BEACH”到“CHANGE
总结matplotlib绘图如何设置坐标轴刻度大小和刻度。 上代码: ?
我在容器 (1) 中创建了容器 (2)。你能帮忙如何向容器(1)添加文本吗?下面是我的代码 return Scaffold( body: Padding( padding: c
我似乎找不到任何人或任何人这样做过。我试图限制我们使用的图像数量,并想创建一个带有渐变作为其“颜色”的文本,并在其周围设置渐变轮廓/描边 到目前为止,我还没有看到任何将两者结合在一起的东西。 我可以自
我正在为视频游戏暗黑破坏神 2 使用 discord.py 构建一个不和谐机器人。其中一项功能要求机器人从暗黑破坏神 2 屏幕截图中提取项目的名称和属性。我目前正在为此使用 pytesseract,但
我很难弄清楚如何旋转 strip.text theme 中的属性来自 ggplot2 .我使用的是 R 版本 3.4.2 和 ggplot2 版本 2.2.1。 以下是 MWE 的数据。 > dput
我是一名优秀的程序员,十分优秀!