私が実際に正しい方向に進んでいるかどうか知りたいのですが、私は現在 C++ 言語を学んでおり、Alex Alllain による Jumping into C++ と呼ばれるこの本を読んでおり、構造に関する章の最後に練習問題があります。連絡帳プログラムは、ユーザーが 1 つの構造に入力できるだけでなく、それぞれ別の名前と電話番号を持つ新しいエントリを追加できる必要があります。ユーザーが必要なだけエントリを追加できるようにします。これは簡単ですか? すべてまたは一部のエントリを表示する機能を追加して、ユーザーがエントリのリストを参照できるようにします。
以下は私が行ったことです。ソースコードが実際に正しいかどうか、構造と全体的な c++ についての私の理解を示しているかどうかを知りたいですか?
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
struct user{
string name;
int phone_num;
};
int _tmain(int argc, _TCHAR* argv[])
{
int input, number; // will hold the users input at the beginning of the program
int counter = 0; // keep track of the array position
int const arraySize = 10; // size of the array
user new_username[arraySize]; // will hold the users details
string name; // will hold the users input for the name
cout << "CONTACTS\n";
do{
cout << "+ADD [1] -EXIT[0]";
cin >> input;
if(input == 1){
//cout << counter;
cout << "\nName: ";
cin >> name;
new_username[counter].name += name;
cout << endl << "\nPhone: ";
cin >> number;
new_username[counter].phone_num = number;
counter++;
//set_user(counter);
}
cout << "Name Number\n";
cout << "--------------\n";
for(int j=0; j < arraySize; j++){
cout << new_username[j].name;
cout << " -- ";
cout << new_username[j].phone_num;
cout << "\n";
}
cout << "\n";
}while(input != 0);
cout << "\n";
system("PAUSE");
return 0;
}