2016-12-16 8 views
-3

私は質問を検索しましたが、私の答えを得ることができなかったか、適切な用語を使用していませんでした。C++プログラムを使って直接オブジェクトを自動的に作成する方法

if(choice == 2){ 
    string tempName, tempAddress; int tempNic,tempContact; 
    cout << "\n\t\t*\tWelcome to Our Sponsor Registeration Section\t*\n\n"; 
    cout << "Please enter your name : "; cin>>tempName; 
    cout << "Please enter your National Identity Card Number : "; cin>>tempNic; 
    cout << "Please enter your Contact Number : "; cin>>tempContact; 
    cout << "Please enter your Address : "; cin>>tempAddress; 
    // prototype Sponsor(string n, string add, int nic_n, int phone) constructor 
    Sponsor (Constructor goes here) // how to make many objects now? 
} 

コードをここに貼り付けられhttps://codeshare.io/aVxl42

チェック私は1つのオブジェクトを追加することができますが、私は、このような何をすればいいの私は、このことで、値を追加するコンストラクタを使用するつもりだライン69と、そのプログラムを使用している人は、それ以上のオブジェクトを追加したいですか?

私は61と70の間に何かをカプセル化する必要があることを知っています。 どうすればこの問題を解決するのか教えてください。

+0

*関連するコードを質問本体に直接入力してください。特別な行がある場合は、それをマークしてください。コメント。リンクがなくなるとどうなるか考えてみてください。そうすれば、質問はまったく役に立たなくなります。 [良い質問をする方法を読む](http://stackoverflow.com/help/how-to-ask)を読んで、[最小限の完全で検証可能な例](http:// stackoverflow。 com/help/mcve)。 –

+0

私はあなたが何をしたいのかよく分かりませんが、Lists(http://www.cplusplus.com/reference/list/list/)やベクトル(http://www.cplusplus.com/reference/vector/vector/vector /)が役立つかもしれません。 – Lehue

+0

ユーザ入力要求をループに入れ、作成したスポンサーをベクトルに追加します。 – DrPepperJo

答えて

0

私はそれをループにしたいと思っていますか? whileループをお勧めします。 私は何か間違いをするかもしれないので、永遠に(ベクトルを禁止しています)ベクターを使用していませんが、全体的なポイントを得るでしょう。

bool stop = false; //This is to check after each loop if it should continue or not 
char contChoice; 
vector<Sponsor> sponsors; 

while(!stop){ 
    if(choice == 2){ 
     string tempName, tempAddress; int tempNic,tempContact; 
     cout << "\n\t\t*\tWelcome to Our Sponsor Registeration Section\t*\n\n"; 
     cout << "Please enter your name : "; cin>>tempName; 
     cout << "Please enter your National Identity Card Number : "; cin>>tempNic; 
     cout << "Please enter your Contact Number : "; cin>>tempContact; 
     cout << "Please enter your Address : "; cin>>tempAddress; 
     // prototype Sponsor(string n, string add, int nic_n, int phone) constructor 
     sponsors.push_back(Sponsor(tempName, tempAddress, tempContact, tempNic)); 
     //Add whatever other arguments you want to pass in, in whatever order 
     cout << "Do you want to continue? [Y/N]: "; cin>>contChoice; 
     if(contChoice == 'N' || contChoice == 'n') 
       stop = true; 
     else stop = false; //This isn't really necessary since it is false by default 
    } 
} 

しかし、少なくともスポンサーではセットメンバー関数を作成することをお勧めします。動的配列を使用して展開することもできます。これは実際にはベクトルよりもトリッキーです。

+0

ループを配置する場所を知っています。オブジェクトをベクトルに追加することは私にとって何か新しいことでした。エンドユーザーがメニューからオプションを選択すると、実際に定義済みのコード化されたオブジェクトではなくユーザーを追加するときに新しいオブジェクトを追加する場所が固まっていました。 –

+0

@BaqarHussainあなたの質問に答えましたか?それとも、まだあなたが疑問に思っていることはありますか? –

関連する問題