0

これは、do while ループを使用した C++ を使用した私の完全なプログラムです。最初は正しく動作しますが、ループすると正しく動作しません。

このプログラムは name を配列に格納し、出力します。配列のサイズは 50 なので、配列に 50 個の名前を格納したいと考えています。

どんな助けでも大歓迎です。ありがとう。

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

void start(int c);

string NameArray [50];
char response;

int main() {
  int count=1;  
  do {    
    count = count - 1;
    start(count);
    cout << "do you want to add another name? ";
    cin>> response;     
    count = count + 2;      
    cout<< endl;        
  } while (tolower(response)=='y');

  cout<< "program Ends" <<endl;        
  system ("pause");        
  return 0;     
}    

void start(int count) {         
  cout<< "Enter your First and Last name: ";        
  getline(cin, NameArray[count]);       
  cout<< NameArray[count] <<endl;       
  cout<< endl;          
}
4

4 に答える 4

2

は入力のcin>> response1 文字を取得し、その後の改行文字を入力ストリームに残します。これによりstart()、名前の入力を求められた直後に関数がgetline()戻り、空の入力行が に返されNameArrayます。

代わりに使用getline()して、応答を文字列として取得しchar、行の最初を確認することもできます

    //...
    cout << "do you want to add another name? ";
    std::string line;
    std::getline(std::cin, line);
    response = line[0];
    //...
于 2013-07-24T01:27:35.010 に答える
0

他の人は文体の問題について言及していますが、差し迫った問題は次のとおりです。

  cout << "do you want to add another name? ";
  cin>> response;
  count = count + 2;      
  cout<< endl;        
} while (tolower(response)=='y');

あなたのstart()関数では、完全な行を で正しく読み込んでgetline()いますが、ここcin >> response;では入力バッファに改行とおそらく他のものを残します。代わりにこれを試してください:

std::string response;

//...

  cout << "do you want to add another name? ";
  getline(cin, response);  // Changed here
  count = count + 2;      
  cout<< endl;        
} while (tolower(response[0])=='y'); // And changed here
于 2013-07-24T01:28:38.953 に答える
0

最初に do while ではなく明示的なループとブレークを使用します。これにより、バッファ オーバーフローを回避し、コードを整理できます。jxh の応答からの修正も追加しました。このようなもの。(未テスト)

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

void start(int c);

string NameArray [50];
std::string response;

int main() {
  for(int count=0; count<50; ++count) {
    start(count);
    cout << "do you want to add another name? ";
    std::getline(cin,response);
    if (tolower(response[0])=='y') break;
  }

  cout<< "program Ends" <<endl;        
  system ("pause");        
  return 0;     
}    

void start(int count) {         
  cout<< "Enter your First and Last name: ";        
  std::getline(cin, NameArray[count]);       
  cout<< NameArray[count] <<endl;       
  cout<< endl;          
}

しかし実際には、固定長配列は、代わりにベクトルを使用することで完全に回避したい問題です。その後、カウントを完全に取り除くことができます。コードは次のようになります。

#include <iostream>
#include <string>
#include <vector>

std::string ask_user_for_name();

std::vector<string> NameArray;

int main() {
  do {
    NameArray.push_back( ask_user_for_name() );
    std::string response;
    cout << "do you want to add another name? ";
    std::getline(cin,response);
    if (tolower(response[0])=='y') break;
  } while(true);

  cout<< "program Ends" <<endl;        
  system ("pause");        
  return 0;     
}    

std::string ask_user_for_name() {         
  cout<< "Enter your First and Last name: ";        
  std::string retval;
  getline(cin, retval);
  cout<<retval<<std::endl<<std::endl;
  return retval;       
}
于 2013-07-24T01:20:10.327 に答える