• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:do - while 文)

do-while文で繰り返し処理する方法とは?

このQ&Aのポイント
  • C++のdo-while文を使用することで、特定の条件が満たされている限り繰り返し処理を実行することができます。
  • do-while文は、まず処理を実行してから条件を判定するため、少なくとも1回は処理が実行されます。
  • 上記のプログラムでは、ユーザーが「y」を入力する限り、do-whileループが繰り返されます。

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

  • ベストアンサー
  • BLUEPIXY
  • ベストアンサー率50% (3003/5914)
回答No.1

>while (cin.get(c)){ のループを終わらせるのに、CTRL+Z とかEOF入力してループを抜けているでしょ? それで、 入力がEOFになっているので、 入力を再び使う前にこれをクリアしてやらないといけません。 適当なところ(ループからでてから次にcin を使うまで)で cin.clear(); を入れてやればいいと思います。

gonntetu
質問者

お礼

望んだ処理が出来ました!!! ありがとうございました。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • 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'); } }

  • 配列の練習問題

    #include<iostream> using namespace std; //count関数の宣言 int count(char str[], char ch); int main() { char str[100]; char ch; cout << "文字列を入力して下さい。\n"; cin >> str; cout << "文字列から探す文字を入力して下さい。\n"; cin >> ch; int c = count(str, ch); cout << str << "の中に" << ch << "は" << c << "個あります。\n"; return 0; } //count関数の定義 int count(char str[], char ch) { int i = 0; int c = 0; while (str[i]) { if (str[i] == ch) c++; i++; } return c; } こんにちは。 この問題の解答のプログラムの意味がイマイチ解らないので良かったら教えて下さい。 確認がてらに質問します。 よろしくお願いします。

  • java eclipse do-while文

    計算問題のプログラムです。No…0を選択したときにwhile文から抜け出したいです。 抜け出せない原因と解決方法(プログラム)を教えていただけるとありがたいです。 package lesson5; import java.util.Random; import java.util.Scanner; public class MentalArithmetic { static Scanner stdIn = new Scanner(System.in); static boolean confirmRetry(){ int cont; do{ System.out.print("もう一度?<Yes・・・1/No・・・0> : "); cont = stdIn.nextInt(); }while (cont != 0 && cont != 1); return cont ==1; } static void mondai1(){ Random rand = new Random(); do{ int p = rand.nextInt(900) + 100; int q = rand.nextInt(900) + 100; int r = rand.nextInt(900) + 100; while(true){ System.out.println(p + "+" + q + "+" + r + " = " ); int k = stdIn.nextInt(); // if(k == p + q + r) // break; System.out.println("違いますよ!!"); } }while (confirmRetry()); } public static void main(String[] args) { System.out.println("暗算力トレーニング!!"); while (true) { mondai1(); } } }

  • 入力に出現する数字文字をカウントするプログラム

    こんにちは、毎度お世話になります。 #include<iostream> using namespace std; int main(void){ char c; int cnt[10] = {0}; while(cin.get(c)){ if(c >= '0' && c <= '9') cnt[c - '0']++; } for (int = 0; i < 10; i++) cout << i << "の出現回数:" << cnt[i] << '\n'; return(0); } [/code] ここで質問なんですが、このプログラムの中で数字文字をカウントしているのはどこでしょうか? そして while(cin.get(c)){ if(c >= '0' && c <= '9') cnt[c - '0']++; } ここの部分が何を表しているのか解りません。 よろしくお願いします。

  • do while 文の使い方

    #include<stdio.h> main() { int i; do{ scanf("%d",&i); }while(i%6==1,2,3,4,5); } だと6の倍数打っても終わらないのですが、 include<stdio.h> main() { int i; do{ scanf("%d",&i); }while(i%6!=0); } } だと6の倍数を入力すれば終了します。 やはり、「1,2,3,4,5」という表現ではだめなのでしょうか? 

  • do while文の条件

    do while文の条件にprintfを使う場合、これはありなんでしょうか? 内容:0が入力されていたら、whileのprintfを実行してからdoに戻る。0以外ならprintfを実行しないで処理を抜ける。 int i; do { printf("0以外の数字を入力してください。\n"); scanf("%d",&i); }while(i == 0 && printf("まじめに入力してください。\n"));

  • 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)と書いていましたが同じ結果です。 どうかお願いします

  • C++ for文からwhile文への変換方法

    まずは単刀直入にこのプログラムをご覧ください。 int two[6][10]; for(int j=0; j<net; j++){ for(int i=0; i<10; i++){ input_file >> two[j][i]; if(two[j][i]==0) break; } } for(int j=0; j<net; j++){ for(int i=0; i<10; i++){ cout << two[j][i] << " "; if(two[j][i]==0) break; } cout << endl; } このfor文をwhile文に置き換えて表示させるやりかたがよくわかりません。 今ゼミの課題でC++のプログラムに取り組んでおり、ファイル内の数字の配列を読み込んでそれを画面に表示させるプログラムを作ってるのですが、この部分がよくわからず苦戦しています。 ちなみに表示させる数字は以下の通りです。 9 11 8 0 1 10 6 0 8 2 4 10 0 5 8 7 2 0 1 3 10 4 0 2 4 12 0 どなたかご教授よろしくお願いします<(_ _)>

  • C++ の while ループ

    C++の基本学習者です。Windows 10 で Visual Studio Community 2015 を使っています。 教本に載っている、機械語のプログラムをC++でシミュレーションする、というものを作ろうと、途中まで下のようなコードを書き込み、そこまで間違いがないかを確かめるために、コンパイルして実行したら、添付の写真のようなエラーメッセージが出てきました。 // ConsoleApplication65.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" #include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> #include <cmath> #include <cstring> using namespace System; using namespace std; int main() { // variables int program = 0; int accumulator = 0; int count = 0; int instructionRegister = 0; int operationCode = 0; int operand = 0; int location[100]; cout << "*** Welcome to Simpletron ***" << endl; cout << "*** Please enter your program ***" << endl; cout << "*** one instruction at a time,***" << endl; cout << "*** after the location number and and the prompt of '?' ***" << endl; cout << "*** To stop entering instructions, ***" << endl; cout << "*** enter -9999 . ***" << endl << endl; cout << setfill('0') << internal; cout << setw(3) << count << " ? "; cin >> program; while (program != -9999) { location[count] = program; count++; } return 0; } while ループに問題がありそうですが、コンパイルはちゃんとできたのに、何が悪いのでしょうか? 詳しい方、どうぞ教えてください。お願いします。

  • while doについて

    九九の表で、forの2重のプログラムは作成できたのですが、whileとdo~whileを使って書き直すやり方がわからないのです。 どうか教えてください。お願いします。 #include <stdio.h> main() { int i,j; for(j = 1; j <= 9; j++){ for(i = 1; i <= 9; i++) printf("%3d", j*i); printf("\n"); } }

このQ&Aのポイント
  • Lenovo L15のモニタードライバのセットアップ方法について説明します。
  • HPからのPDFマニュアルにはWindows10の場合のセットアップ方法が記載されていますが、実際にはコントロールパネル内やモニタのドライバ検索からは見つからないため、困っています。
  • モニタードライバのセットアップがうまくいかない場合、特定の手順やソフトウェアのインストールが必要な場合もありますので、解決策を探しています。
回答を見る

専門家に質問してみよう