do whileを用いた文で抜け出す方法

このQ&Aのポイント
  • 初歩的なのですが、do while文を使って条件に合致した時に抜け出すプログラムを作成したいです。
  • 入力した値が条件に合致した場合に抜けることができるプログラムを作成したいです。
  • iとjにそれぞれ1を入力するとプログラムが終了するようにしたいです。
回答を見る
  • ベストアンサー

初歩的なのですが・・・

do whileを用いた文で抜け出したくてコマンドを入力しても抜けれず  抜けてはいけないときに抜けてしまいます・・ プログラムは・・ #include<iostream> #include<Windows.h> #include<MMSystem.h> #pragma comment(lib, "winmm.lib") using namespace std; int main (){ sndPlaySound("start",SND_SYNC); double i, j, ido, keido; do { cout <<"i="; cin >>i; cout <<"j="; cin >>j; if (i==138.8607033 && j==35.07979167) sndPlaySound("port",SND_SYNC); else if (i==138.797575 && j==35.12242833) sndPlaySound("hara",SND_SYNC); else if (i==138.795445 && j==35.15679333) sndPlaySound("tokai",SND_SYNC); else if (i==138.8604367 && j==35.09611333) sndPlaySound("numadus",SND_SYNC); else if (i==138.855455 && j==35.080332167) sndPlaySound("senbon",SND_SYNC); else if (i==138.88019 && j==35.06325) sndPlaySound("kinen",SND_SYNC); else if (i==138.8958317 && j==35.02811333) sndPlaySound("awasima",SND_SYNC); else if (i==138.8679883 && j==35.140775) sndPlaySound("inter",SND_SYNC); else if (i==138.9153667 && j==35.1227) sndPlaySound("misimas",SND_SYNC); else if (i==138.9214167 && j==35.11716167) sndPlaySound("misimat",SND_SYNC); else if (i==138.9149367 && j==35.11563667) sndPlaySound("rakujuen",SND_SYNC); else cout<<"まだ途中です\n"; } while (i==1 && j==1); if (i==1 && j==1){ sndPlaySound("goal",SND_SYNC); } return 0; です。i,jにそれぞれ1を入れると抜けれるようにしたいのですが・・ どなたかわかりますでしょうか?

質問者が選んだベストアンサー

  • ベストアンサー
  • nes1129
  • ベストアンサー率27% (8/29)
回答No.2

連投ですが… >iとjが1の時に抜けたいのであれば >while (i!=1 && j!=1); >どちらか片方が1の時に抜けたいのであれば >while (i!=1 || j!=1); >とする必要があります。 これ逆ですね。 iとjが1の時に抜けたいのであれば while (i!=1 || j!=1); どちらか片方が1の時に抜けたいのであれば while (i!=1 && j!=1); でした。

その他の回答 (2)

回答No.3

老婆心ながら2点ほど。 double値の比較は幅を持たせませんと精度の関係で一致しない場合があります。 #例えば、138.8604367との一致で比較するのではなく138.8604と138.8605の範囲で比較するとか。 また、「i,jにそれぞれ1を入れると抜けれる」を言葉通りに実装するなら、 if (i == 1 && j == 1) { break; } の方が自然に読めるのではないかと思います。 #こちらは、整数値との比較ですから一致比較でもまぁ、大丈夫でしょう。

  • nes1129
  • ベストアンサー率27% (8/29)
回答No.1

>while (i==1 && j==1); この場合iとjが1の時に繰り返すという構文になります。 iとjが1の時に抜けたいのであれば while (i!=1 && j!=1); どちらか片方が1の時に抜けたいのであれば while (i!=1 || j!=1); とする必要があります。

yu042122
質問者

お礼

ありがとうございます。 見事抜けれました。 これを応用しGPSの値を利用し 到着地点を作りその場についたらループが消えるというものにしたいと思います。

関連するQ&A

  • C++の無限ループを解決してください

    アルゴリズムを勉強するときに以下のソースを書きました; void weighted_quick_union_algorithm() { static const int volume = 10; enum status { terminate_, union_, find_ }; string str; status sta; vector<int> system(volume, 0); vector<int> size(volume, 1); for (int index = 0; index != volume; ++index) { system[index] = index; } do { cout<<"cin"<<endl; cin >> str; for (string::size_type index = 0; index != str.size(); ++index) str[index] = toupper(str[index]); if (str == "UNION") sta = union_; else if (str == "FIND") sta = find_; else if (str == "TERMINATE") sta = terminate_; switch (sta) { case(0): { cout << str << endl; break; } case(1): { cout << str << sta << endl; int p(0), q(0), i(0), j(0); while (cin >> p) { cin >> q; for (i = p; i != system[i]; i = system[i]); for (j = q; j != system[j]; j = system[j]); if (i == j) continue; if (size[i] < size[j]) { system[i] = j; size[j] += size[i]; } else { system[j] = i; size[i] += size[j]; } cout << p << " - " << q << endl; } cout<<"break"<<endl; break; } case(2): { cout << str << sta << endl; break; } } } while (sta); } しかし unionを入力しあと ; でwhile(cin>>p)をブレイクしたら cin break UNION1 cin break Union1 で無限ループ 結構時間かかったが間違いがわかりません ちなみに最少は while(cin>>p>>q)と書いていましたが同じ結果です。 どうかお願いします

  • 配列のプログラムですが

    #include <iostream> using namespace std; int main() { int a[100],b=-9999; int i=0,j; do { cout << "整数値を入力してください\n"; cin >> a[i]; b += a[i]; i++; }while( a[i-1] != 9999); cout << b << '\n'; for(j=0;j<i-1;j=j+1) cout << a[j] * 3 << '\n'; return 0; } このプログラムってどんな計算をしてるんですか?誰か分かる人いますか 整数値を入れてくださいって出るだけなんですが

  • 置換をするプログラム

    visual C++で入力された文字列に対し、#があったら%に置換するプログラムを作っています。insertを使おうと思うのですが、よくわかりません ずっと考えているのですが、ここから1週間進んでいません 教えてください #include<iostream> #include<string> #include<cstdlib> using namespace std; int main(void) { string a,s; cout <<"文字列を入力してください"<<endl; getline(cin,s); int i,j=0; while( j!= s.npos) { i=s.find_first_of("#",j); if(i==s.npos){ cout << s.substr(j) << endl; break; } if( i>0) { cout << s.substr(j, i-j); j=i; } i=s.find_first_not_of("#",j); if( i== s.npos) { a=s.substr(j); j=i; } else{ a=s.substr(j,i-j); j=i; } for(i=0; i<s.length(); i++) { if (s[i]== "#"){ s.insert(i,"%"); } cout<<a.s[i]<<endl; } } return 0; }

  • 改行文字について

    #include<iostream> using namespace std; int main(void) { char c; while (cin.get(c)) if (c == '\n') cout << "[RET]\n"; else cout << c; return(0); } これのif (c == '\n')の改行文字と等しいとは何のことを指すのでしょうか? よろしくお願いします。

  • do-while 文でコンパイルエラーがでます。。

    下記グローバル関数Run内のdo-while文で、 error C2059: syntax error : '}' というコンパイルエラーが起きるのですが、どうしても理由がわかりません; do-whileをコメントアウトしコンパイルしてみたところ正常に動くので、そこがおかしいのは間違いないと思います。 どなたかお力添えをお願いいたします。。 #include <iostream> #include "BinaryTree.cpp" using namespace std; void Run(); int main() { Run(); return 0; } void Run() { BinaryTree<int> *bt; char input; do { cout<<"Menu Display"<<endl; cout<<"---------------------"<<endl; cout<<"1> Enter data"<<endl; cout<<"2> Print Tree"<<endl; cout<<"3> Reverse Tree"<<endl; cout<<"4> Quit"<<endl; cout<<"Enter your choice: "; cin>>input; switch (input) { case '1': int elem; cout<<"Enter data for tree: "; cin>>elem; if (bt == NULL) bt = new BinaryTree<int>(elem); else bt->Insert(elem); break; case '2': if (bt == NULL) cout<<"The tree is empty."<<endl; else { cout<<"Tree size = "<<bt->Size(bt)<<endl; cout<<"---------------------"<<endl; bt->Inorder(bt); } break; case '3': if (bt == NULL) cout<<"The tree is empty."<<endl; bt->Reverse(bt); break; }while (input != '4'); } }

  • 二分法のC++プログラム

    #include<iostream> #include<cmath> using namespace std; int main(){ double x1, x2, c; cout <<"x1:"; cin >> x1; cout <<"x2:"; cin >> x2; while(fabs(x2-x1) > 0.00001){ c=(x1+x2)/2; if(cos(x1/2.0)*cos(c/2)>=0) x1 = c; else x2 = c; } cout << "x=" << x1 <<endl; return 0; } cos(x/2)=0の解となるxを挟んでいないx1,x2を与えると、結果は保証できないですが、それは別にいいんですか?それはダメだとするとどこを訂正すればいいんですか

  • VC++でプログラムの勉強をしています。

    プログラムは最近はじめたばかりです。While文とif文を使ってクイズを作ってみたところ、一個目のsinで入力を求めているところから無限ループになってしまいました。色々調べてcin.cler()とsin.ignore()を入れたりもしてみましたが上手くいきませんでした。どこを間違えているのでしょうか? //クイズ #include <iostream> using namespace std; int main()//cin.clear();cin.ignore();???? { int a; int b; while(1) { cout<<"ネコ型のロボットが出てくるアニメといえば?"<<endl; cout<<"A)ドラえもん B)ドラエもん C)ほりえもん D)サザエさん"<<endl; cin>>a; if(a=='A') { cout<<"ファイナルアンサー?"<<endl; cout<<"Y)Yes N)NO"<<endl; cin>>b; if(b=='Y'){break;} if(b=='N'){cout<<"ゆっくり考えてね!!"<<endl;} if(b!='Y'||'N'){cout<<"正しく入力してね!"<<endl;} } if(a=='B'||'C'||'D') { cout<<"ファイナルアンサー?"<<endl; cout<<"Y)Yes N)NO"<<endl; cin>>b; if(b=='Y'){cout<<"残念!!"<<endl;} if(b=='N'){cout<<"ゆっくり考えてね!!"<<endl;} } if(a!='A'||'B'||'C'||'D'){cout<<"正しく入力してね!"<<endl;} } cout<<"正解!!"<<endl; }

  • icpcの過去問

    問題→ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1147&lang=jp 自分の回答→ #include <iostream> using namespace std; int numbers[105]; int order(int n,int num){ if(num==0){numbers[0]=n;} else{ for(int i=0;i<num;i++){ if((numbers[num-(i+1)])>n){//cout << "here1"; numbers[num-i]=numbers[num-(i+1)];//cout << "here2"; numbers[num-(i+1)]=n;//cout << "here3"; } } } return 0; }//昇順に並べる int main(){ int n; while(cin>>n,n){ int count,answer; for(int i=0;i<n;i++){int Numb; cin >> Numb;if(i==2){cout << "here";} order(Numb,i); count++; }//昇順に要素が並んだ for(int i=1;i<count-1;i++){ answer += numbers[i]; } answer = answer/(count-2); cout << answer << endl; } } 実行結果→ 3 7 6 5 Bus error となってしまいます。最後の入力の時に、cinされずにbuserrorになるのですがなぜなのでしょうか。

  • c++ 整数値の各桁を足し合わせるプログラム

    上記の通りの質問です #include <iostream> using namespace std; int main () { int x; int sum = 0; cout << "正の整数値の各桁を足す。\n " ; do{ cout << "正の整数値:"; cin >> x; }while (x <= 0); cout << "足すと"; while (x > 0){ cout << sum += x % 10; x /= 10; return sum; } cout << "です。\n " ; } とまでは書けましたがうまくいきません。 初心者なので簡単かもしれませんがお願いします。

  • うるう年かどうかを計算するプログラミング

    西暦何年かを入力し、その年がうるう年かそうでないかを 計算するプログラムを作っています。 #include <iostream> using namespace std; int main() { int toshi; cout <<"西暦を入力してください"; cin>>toshi; if (toshi%4==0) {if (toshi%100==0) {if (toshi%400==0) {cout<<toshi<<"年はうるう年です。";} else {cout<<toshi<<"年は非うるう年です。";} else {cout<<toshi<<"年は非うるう年です。";} else {cout<<toshi<<"年は非うるう年です。";}}} return EXIT_SUCCESS; } これを入力したのですが エラーが2つでてしまいます。 どこがちがうのでしょうか?