私は非常に、非常に新しいコーディングです。 私は約3日間書いてきたC++から始めたいと思います。 自分自身を教えるために、私はSololearn、HackerrankとText-Based Adventure Gameを使っています。C++で奇妙な問題がある
ここに私の問題があります。
私は、プロジェクト内の3つのプレイアイテムのスタートシナリオを書きます。 これらは別々のif文の中にあります。 文字健康を含めるには、このすべてが真実である、単一のwhileループ内であり、一方、健康しかし> = 1
、 私は奇妙な問題を抱えている場合は、「オプション」の1(選択したとき'Orc'の下の入れ子のif文)は、実行されたときに複数のオプションを出力した後、すべてのテキストをキャラクタ作成からそのポイントまで一回繰り返します。
これはif文が文字列の場合にのみ発生します。 "Item"の文字列がなく、 "Bucket"、 "Rug"、および "Bars"をint 1,2,3と設定するようにコードを変更すると、期待通りに機能します。 これはC++の文字列に問題がありますか?
私は、問題を引き起こしていたにも関わらず、数多くのcin文の代わりにcinのgetlineを含め、私が考えることができるすべてを研究しましたが、喜びはありません。
以下のコードを見てください。私は何かを逃したか? また、これをほぼ完全に新しくして、これを行うにはより良い方法がありますか?
最後に、「人間」と「エルフ」が現在無限ループを作成しているという問題をよりよく理解するために実行する必要があると感じる場合は、「Orc」セグメントのみが正しく動作することに注意してください私はまだwhileループを壊すために十分に書いていないからです。
ご協力いただきありがとうございます。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Name Entry
string Name;
cout << "Enter Name: " << endl;
getline (cin, Name);
//Age Entry
int Age;
cout << "Enter Age: " << endl;
cin >> Age;
//Race Entry
string Race;
cout << "Enter Race: " << endl;
cin.ignore();
getline (cin, Race);
//Stats
int Strength;
int Agility;
int Speech;
int Intellect;
int Social;
int Health=1;
//Racial Starts
while (Health >= 1) {
//Human
if (Race == "Human" || Race == "human") {
Strength = 10;
Agility = 10;
Speech = 8;
Intellect = 7;
Social = 20;
Health = 100;
cout << "Human Selected." << endl;
cout << "Starting Game!" << endl << endl;
cout << "You are in a finely furnished bedroom." << endl;}
//Elf
if (Race == "Elf" || Race == "elf") {
Strength = 8;
Agility = 14;
Speech = 12;
Intellect = 13;
Social = 10;
Health = 80;
cout << "Elf Selected." << endl;
cout << "Starting Game!" << endl << endl;
cout << "You are cleaning a finely furnished bedroom." <<endl;}
//Orc
if (Race == "Orc" || Race == "orc") {
Strength = 18;
Agility = 5;
Speech = 6;
Intellect = 3;
Social = 5;
Health = 200;
cout << "Orc Selected" << endl;
cout << "Starting Game!" << endl << endl;
cout << "You are locked in a cell." << endl << "The cell is made up of three stone walls." << endl << "There are no windows." << endl;
cout << "There is a rug on the cobbled stone floor, and a bucket in the corner." << endl << "Iron bars face into a guard's chamber." << endl << endl;
cout << "The guard is sleeping." << endl << "A shortsword rests in a scabbard on the table in fromt of him." << endl;
//Actions in this room
int Item;
cout << "Your escape lies with either the Bucket, the Rug or the Bars." << endl << "Pick an item." << endl;
string Item;
cin >> Item;
if (Item == "bucket" || Item == "Bucket") {
cout << "You place the Bucket on your head." << endl << "The bucket is full of your own excrement." << endl << "You die of disease." << endl;
Health = Health-1000; }
if (Item == "Rug" || Item == "rug") {
cout << "You lie on the rug and fall asleep." << endl << "The guard wakes up." << endl << "His irrational hatred for you causes him to draw his sword, enter your cell and sever your head." << endl;
Health = Health-1000; }
if (Item == "Bars" || Item == "bars") {
cout << "You try the bars." << endl << "You realise that this cell was not built to hold an Orc." << endl << "You bend the bars easily and step out of the cell." << endl; }
}
}
cout << "Game Over";
system("pause");
return 0;
}
あなたはすべてのロジックを 'while'ループに入れているので、すべてが繰り返されます。デバッガを使用してゲームを実行し、実行順序を調べます。 – xxbbcc
「私はSololearn、Hackerrankのミックスを使用し、テキストベースのアドベンチャーゲームを作成しています。」そのミックスに[良い本](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)を追加することを強くお勧めします。 C++は非常に複雑な言語であり、あなたに噛みつくような細かいことがたくさんあり、適切な検索キーワードがわからない限りオンラインで検索することは不可能です。 – user4581301