Text Based Adventure issue/question for lines (78-100)
目前,在我的第一个编程课程中,我们被要求做一个项目和一个小组项目。我小组选择的项目是基于文本的冒险。我做了大部分工作或进行研究并遇到了麻烦,无法解决我的问题。我的问题是,在第78-100行中,我必须使用_(下划线)来计算if / else语句中的空格。有没有办法我可以把它带到任何玩这个的人可以进入"环顾四周"而不是" 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");
}
答案 0 :(得分:1)
如果您不确切知道用户将输入多少字词,最好使用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()
应该不难。