質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

1342閲覧

C++ if文の動作について

Naohito4869

総合スコア7

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2019/09/04 05:12

編集2019/09/04 05:19

C++のプログラムなのですが、for文の中のif文が想定していた動作と同じ動作をしていません。

この順番でのインプットを想定していて、2回目のturnOrMove, directionが, 't', 'R'となりif(turnOrMove[i] == 't' && direction[i] == 'R')の中の内容が実施されるはずなのですが、毎回else{
cout << "上記以外の動作" << endl;}の内容が実施されてしまいます。

想定しているインプットの一覧は下記の内容です。
2 2
2 1 1 1
8
m F
t R
m F
m F
t B
m L
m L
m B

C++

1#include <bits/stdc++.h> 2#include <string> 3 4using namespace std; 5 6// function to find final position of 7// robot after the complete movement 8 9string askTurnOrMove(string turnOrMove){ 10 cin >> turnOrMove; 11 return turnOrMove; 12} 13 14string askDirection(string direction){ 15 cin >> direction; 16 return direction; 17} 18 19void finalPosition(int initialX, int initialY, int front, int right, int back, int left, int numberOfCommands) { 20 //int numCommands = numberOfCommands.size(); 21 int countUp = 0, countDown = 0; 22 int countLeft = 0, countRight = 0; 23 string turnOrMove, direction; 24 string AngleOfRobot; 25 AngleOfRobot = 'N'; 26 cout << AngleOfRobot << endl; 27 28// traverse the instruction string 'move' 29 for (int i = 0; i < numberOfCommands; i++) { 30 // for each movement increment its respective counter 31 cout << "------" << i << "s loop" << endl; 32 33 turnOrMove = askTurnOrMove(turnOrMove); 34 direction = askDirection(direction); 35 36 cout << "AngleOfRobot in here: " << AngleOfRobot << endl; 37 cout << "Turn or move: " << turnOrMove << endl; 38 cout << "Direction: " << direction << endl; 39 40 if(turnOrMove[i] == 't' && direction[i] == 'R'){ 41 cout << "REACH" << endl; 42 if(AngleOfRobot[i] == 'N'){ 43 AngleOfRobot = 'E'; 44 cout << "Reach to Angle change to E" << endl; 45 } 46 else if(AngleOfRobot[i] == 'E'){ 47 AngleOfRobot = 'S'; 48 } 49 else if(AngleOfRobot[i] == 'S'){ 50 AngleOfRobot = 'W'; 51 } 52 else if(AngleOfRobot[i] == 'W'){ 53 AngleOfRobot = 'N'; 54 } 55 else{ 56 cout << "REACH to else" << endl; 57 } 58 } 59 else{ 60 cout << "上記以外の動作" << endl; 61 } 62 63 // ロボットが北向きの時 64 if (direction[i] == 'F' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'N'){ 65 countUp += front; 66 cout << "Reach to East" << endl; 67 } 68 else if (direction[i] == 'B' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'N'){ 69 countDown += back; 70 } 71 else if (direction[i] == 'L' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'N'){ 72 countLeft += left; 73 } 74 else if (direction[i] == 'R' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'N'){ 75 countRight += right; 76 } 77 78 // ロボットが南向きの時 79 else if (direction[i] == 'F' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'S'){ 80 countDown += front; 81 } 82 else if (direction[i] == 'B' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'S'){ 83 countUp += back; 84 } 85 else if (direction[i] == 'L' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'S'){ 86 countRight += left; 87 } 88 else if (direction[i] == 'R' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'S'){ 89 countLeft += right; 90 } 91 92 // ロボットが東向きの時 93 else if (direction[i] == 'F' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'E'){ 94 countRight += front; 95 cout << "Reach when robot angle is E" << endl; 96 } 97 else if (direction[i] == 'B' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'E'){ 98 countLeft += back; 99 } 100 else if (direction[i] == 'L' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'E'){ 101 countUp += left; 102 } 103 else if (direction[i] == 'R' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'E'){ 104 countDown += right; 105 } 106 107 // ロボットが西向きの時 108 else if (direction[i] == 'F' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'W'){ 109 countLeft += front; 110 } 111 else if (direction[i] == 'B' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'W'){ 112 countRight += back; 113 } 114 else if (direction[i] == 'L' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'W'){ 115 countDown += left; 116 } 117 else if (direction[i] == 'R' && turnOrMove[i] == 'm' && AngleOfRobot[i] == 'W'){ 118 countUp += right; 119 } 120 else{ 121 cout << "上記以外の動作" << endl; 122 } 123 } 124 // required final position of robot 125 cout << "Final Position: (" 126 << initialX + (countRight - countLeft) 127 << ", " << initialY + (countUp - countDown) 128 << ")" << endl; 129} 130 131// Driver program to test above 132int main() { 133 int initialX, initialY; 134 int front, right, back, left; 135 int numberOfCommands; 136 cin >> initialX; 137 cin >> initialY; 138 cin >> front; 139 cin >> right; 140 cin >> back; 141 cin >> left; 142 cin >> numberOfCommands; 143 144 finalPosition(initialX, initialY, front, right, back, left, numberOfCommands); 145 return 0; 146}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cateye

2019/09/04 05:20 編集

インデントがちゃんと分かるように <code>を選んで出てきた```と```の間にソースを貼り付けましょう。 また、環境(OS,開発環境等)を記述しましょう。・・・人があなたと同じ環境を持っていると、想定しないように。
guest

回答2

0

C++

1 cout << "Turn or move: " << turnOrMove << endl; 2 cout << "Direction: " << direction << endl;

の部分をifの式のように

C++

1 cout << "Turn or move: " << turnOrMove[i] << endl; 2 cout << "Direction: " << direction[i] << endl;

と書けば間違っていることに素早く気付けると思います。

あと、3行目の入力データが一つしかないため、turnOrMoveとdirectionが入れ替わってしまうような気がします。

投稿2019/09/04 11:21

編集2019/09/04 11:22
nomuken

総合スコア1627

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

turnOrMove[i]

これはturnOrMove文字列のi番目の文字を取り出す処理だと思いますが、期待した動作になっていますか?

投稿2019/09/04 05:32

takabosoft

総合スコア8356

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Naohito4869

2019/09/04 05:38

1回目のforループのみ期待した動作になっているのですが、2回目以降が毎回elseの方の条件に当てはまってしまいます。2回目のt Rの場合は、if(turnOrMove[i] == 't' && direction[i] == 'R')の内容が実行されると思ったのですが、elseの内容になってしまいます。
takabosoft

2019/09/04 05:40

いや、そういう事を言っているわけではありません。 turnOrMove[i] は turnOrMove文字列のi番目の文字を取り出す処理 と言っています。 あたながしたいことはi番目の文字を取得することですか?違いますよね?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問